Skip to content

Commit

Permalink
add rest api operations
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Sep 23, 2024
1 parent 17cd11d commit 30584f6
Show file tree
Hide file tree
Showing 22 changed files with 3,651 additions and 2,159 deletions.
74 changes: 74 additions & 0 deletions connector/api/alerts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package api

import (
"context"
"fmt"
"time"

"github.com/hasura/ndc-prometheus/connector/metadata"
"github.com/hasura/ndc-sdk-go/schema"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"go.opentelemetry.io/otel/codes"
)

// AlertState models the state of an alert
// @enum firing, inactive, pending
type AlertState string

// Alert models an active alert.
type Alert struct {
ActiveAt time.Time `json:"activeAt"`
Annotations model.LabelSet `json:"annotations"`
Labels model.LabelSet `json:"labels"`
State AlertState `json:"state"`
Value Decimal `json:"value"`
}

// FunctionPrometheusRules return a list of all active alerts
func FunctionPrometheusAlerts(ctx context.Context, state *metadata.State) ([]Alert, error) {
ctx, span := state.Tracer.Start(ctx, "Prometheus Alerts")
defer span.End()

rawResults, err := state.Client.Alerts(ctx)
if err != nil {
span.SetStatus(codes.Error, "failed to get Prometheus alerts")
span.RecordError(err)
return nil, err
}

results := make([]Alert, len(rawResults.Alerts))
for _, item := range rawResults.Alerts {
value, err := NewDecimal(item.Value)
if err != nil {
return nil, schema.InternalServerError(fmt.Sprintf("failed to decode alert value %s", item.Value), map[string]any{
"error": err.Error(),
})
}
r := Alert{
ActiveAt: item.ActiveAt,
Annotations: item.Annotations,
Labels: item.Labels,
State: AlertState(item.State),
Value: value,
}
results = append(results, r)
}

return results, nil
}

// FunctionPrometheusAlertmanagers return an overview of the current state of the Prometheus alertmanager discovery
func FunctionPrometheusAlertmanagers(ctx context.Context, state *metadata.State) (v1.AlertManagersResult, error) {
ctx, span := state.Tracer.Start(ctx, "Prometheus Alertmanagers")
defer span.End()

results, err := state.Client.AlertManagers(ctx)
if err != nil {
span.SetStatus(codes.Error, "failed to get Prometheus alertmanagers")
span.RecordError(err)
return v1.AlertManagersResult{}, err
}

return results, nil
}
73 changes: 73 additions & 0 deletions connector/api/decimal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package api

import (
"encoding/json"
"fmt"

"github.com/hasura/ndc-sdk-go/utils"
)

// Decimal wraps the scalar implementation for big decimal,
// with string representation
//
// @scalar Decimal string
type Decimal struct {
value *float64
raw *string
}

// NewDecimal creates a BigDecimal instance
func NewDecimal[T comparable](value T) (Decimal, error) {
result := Decimal{}
if err := result.FromValue(value); err != nil {
return Decimal{}, nil
}
return result, nil
}

// ScalarName get the schema name of the scalar
func (bd Decimal) IsNil() bool {
return bd.raw == nil
}

// Stringer implements fmt.Stringer interface.
func (bd Decimal) String() string {
if bd.raw != nil {
return *bd.raw
}
if bd.value != nil {
return fmt.Sprint(*bd.value)
}
return "Inf"
}

// MarshalJSON implements json.Marshaler.
func (bi Decimal) MarshalJSON() ([]byte, error) {
return json.Marshal(bi.String())
}

// UnmarshalJSON implements json.Unmarshaler.
func (bi *Decimal) UnmarshalJSON(b []byte) error {
var value any
if err := json.Unmarshal(b, &value); err != nil {
return err
}

return bi.FromValue(value)
}

// FromValue decode any value to Int64.
func (bi *Decimal) FromValue(value any) error {
fValue, err := utils.DecodeNullableFloat[float64](value)
if err != nil {
return err
}

if fValue != nil {
bi.value = fValue
rawValue := fmt.Sprint(value)
bi.raw = &rawValue
}

return nil
}
2 changes: 1 addition & 1 deletion connector/api/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func FunctionPrometheusLabelNames(ctx context.Context, state *metadata.State, ar
if err != nil {
return nil, err
}
results, warnings, err := state.Client.LabelNames(ctx, arguments.Match, *args.Start, *args.End, opts...)
results, warnings, err := state.Client.LabelNames(ctx, args.Match, *args.Start, *args.End, opts...)
if len(warnings) > 0 {
span.SetAttributes(attribute.StringSlice("warnings", warnings))
}
Expand Down
25 changes: 25 additions & 0 deletions connector/api/rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package api

import (
"context"

"github.com/hasura/ndc-prometheus/connector/metadata"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"go.opentelemetry.io/otel/codes"
)

// FunctionPrometheusRules return a list of alerting and recording rules that are currently loaded.
// In addition it returns the currently active alerts fired by the Prometheus instance of each alerting rule.
func FunctionPrometheusRules(ctx context.Context, state *metadata.State) (v1.RulesResult, error) {
ctx, span := state.Tracer.Start(ctx, "Prometheus Rules")
defer span.End()

results, err := state.Client.Rules(ctx)
if err != nil {
span.SetStatus(codes.Error, "failed to get Prometheus rules")
span.RecordError(err)
return v1.RulesResult{}, err
}

return results, nil
}
181 changes: 181 additions & 0 deletions connector/api/schema.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 30584f6

Please sign in to comment.