Skip to content

Commit

Permalink
Prepend metrics names with caller (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephhuynh18 authored Jul 31, 2023
1 parent 13624cf commit cb96ca3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/cas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func main() {
logger.Fatalf("error creating discord handler: %v", err)
}

metricService, err := metrics.NewMetricService(serverCtx, logger)
metricService, err := metrics.NewMetricService(serverCtx, logger, models.MetricsCallerName)
if err != nil {
logger.Fatalf("error creating metric service: %v", err)
}
Expand Down
16 changes: 10 additions & 6 deletions common/metrics/otl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"context"
"encoding/json"
"fmt"
"os"

"go.opentelemetry.io/otel/attribute"
Expand All @@ -18,12 +19,13 @@ import (
)

type OtlMetricService struct {
caller string
meterProvider *sdk.MeterProvider
meter metric.Meter
logger models.Logger
}

func NewMetricService(ctx context.Context, logger models.Logger) (models.MetricService, error) {
func NewMetricService(ctx context.Context, logger models.Logger, caller string) (models.MetricService, error) {
var resources *resource.Resource
// Resources are a special type of attribute that apply to all spans generated by a process.
// These should be used to represent underlying metadata about a process that’s non-ephemeral,
Expand Down Expand Up @@ -63,7 +65,7 @@ func NewMetricService(ctx context.Context, logger models.Logger) (models.MetricS
meter := meterProvider.Meter("go-cas")
logger.Infof("started")

return &OtlMetricService{meter: meter, meterProvider: meterProvider, logger: logger}, nil
return &OtlMetricService{caller: caller, meter: meter, meterProvider: meterProvider, logger: logger}, nil
}

func (o OtlMetricService) Shutdown(ctx context.Context) {
Expand All @@ -72,7 +74,8 @@ func (o OtlMetricService) Shutdown(ctx context.Context) {
}

func (o OtlMetricService) Count(ctx context.Context, name models.MetricName, val int) error {
counter, err := o.meter.Int64Counter(string(name) + "_count")
fullname := fmt.Sprintf("%s:%s_count", o.caller, name)
counter, err := o.meter.Int64Counter(fullname)
if err != nil {
return err
}
Expand All @@ -81,7 +84,8 @@ func (o OtlMetricService) Count(ctx context.Context, name models.MetricName, val
}

func (o OtlMetricService) Distribution(ctx context.Context, name models.MetricName, val int) error {
histogram, err := o.meter.Int64Histogram(string(name) + "_histogram")
fullname := fmt.Sprintf("%s:%s_histogram", o.caller, name)
histogram, err := o.meter.Int64Histogram(fullname)
if err != nil {
return err
}
Expand All @@ -90,11 +94,11 @@ func (o OtlMetricService) Distribution(ctx context.Context, name models.MetricNa
}

func (o OtlMetricService) QueueGauge(ctx context.Context, queueName string, monitor models.QueueMonitor) error {
unprocessed, err := o.meter.Int64ObservableGauge(queueName + "_unprocessed_gauge")
unprocessed, err := o.meter.Int64ObservableGauge(fmt.Sprintf("%s:%s_unprocessed_gauge", o.caller, queueName))
if err != nil {
return err
}
inflight, err := o.meter.Int64ObservableGauge(queueName + "_inflight_gauge")
inflight, err := o.meter.Int64ObservableGauge(fmt.Sprintf("%s:%s_inflight_gauge", o.caller, queueName))
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions models/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ const (
MetricName_ValidateReprocessedRequest MetricName = "validate_reprocessed_request"
MetricName_WorkerJobCreated MetricName = "worker_job_created"
)

const MetricsCallerName = "go-cas"

0 comments on commit cb96ca3

Please sign in to comment.