| package debug |
|
|
| import ( |
| "bytes" |
| "context" |
| "fmt" |
| "time" |
|
|
| dto "github.com/prometheus/client_model/go" |
| "github.com/prometheus/common/expfmt" |
| "google.golang.org/protobuf/proto" |
| "google.golang.org/protobuf/types/known/timestamppb" |
|
|
| "github.com/openmeterio/openmeter/openmeter/streaming" |
| ) |
|
|
| |
| type DebugConnector interface { |
| GetDebugMetrics(ctx context.Context, namespace string) (string, error) |
| } |
|
|
| |
| type debugConnector struct { |
| streaming streaming.Connector |
| } |
|
|
| |
| func NewDebugConnector(streaming streaming.Connector) DebugConnector { |
| return &debugConnector{ |
| streaming: streaming, |
| } |
| } |
|
|
| |
| |
| func (c *debugConnector) GetDebugMetrics(ctx context.Context, namespace string) (string, error) { |
| |
| queryParams := streaming.CountEventsParams{ |
| From: time.Now().Truncate(time.Hour * 24).UTC(), |
| } |
|
|
| |
| rows, err := c.streaming.CountEvents(ctx, namespace, queryParams) |
| if err != nil { |
| return "", fmt.Errorf("connector count events: %w", err) |
| } |
|
|
| |
| var metrics []*dto.Metric |
| for _, row := range rows { |
| metric := &dto.Metric{ |
| Label: []*dto.LabelPair{ |
| { |
| Name: proto.String("subject"), |
| Value: proto.String(row.Subject), |
| }, |
| }, |
| Counter: &dto.Counter{ |
| |
| Value: proto.Float64(float64(row.Count)), |
| CreatedTimestamp: timestamppb.New(time.Now()), |
| }, |
| } |
|
|
| metrics = append(metrics, metric) |
| } |
|
|
| family := &dto.MetricFamily{ |
| Name: proto.String("openmeter_events_total"), |
| Help: proto.String("Number of ingested events"), |
| Type: dto.MetricType_COUNTER.Enum(), |
| Unit: proto.String("events"), |
| Metric: metrics, |
| } |
|
|
| var out bytes.Buffer |
| _, err = expfmt.MetricFamilyToOpenMetrics(&out, family) |
| if err != nil { |
| return "", fmt.Errorf("convert metric family to OpenMetrics: %w", err) |
| } |
|
|
| return out.String(), nil |
| } |
|
|