-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
3,651 additions
and
2,159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.