Skip to content

Commit

Permalink
Instantiated OTEL Metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Wise-Wizard <saransh.shankar@gmail.com>
  • Loading branch information
Wise-Wizard committed Jun 19, 2024
1 parent 3f6f5fe commit f1f9b7a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
45 changes: 45 additions & 0 deletions pkg/metrics/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
package metrics

import (
"context"
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
)

// NSOptions defines the name and tags map associated with a factory namespace
Expand Down Expand Up @@ -48,6 +52,47 @@ type HistogramOptions struct {
Buckets []float64
}

type otelFactory struct {
meter metric.Meter
}

func NewOTelFactory() Factory {
return &otelFactory{
meter: otel.Meter("jaeger-V2"),

Check warning on line 61 in pkg/metrics/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/metrics/factory.go#L59-L61

Added lines #L59 - L61 were not covered by tests
}
}

func (f *otelFactory) Counter(options Options) Counter {
counter, _ := f.meter.Int64Counter(options.Name)
attrs := getAttributes(options.Tags)
return &otelCounter{
counter: counter,
ctx: context.Background(),
attrs: attrs,

Check warning on line 71 in pkg/metrics/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/metrics/factory.go#L65-L71

Added lines #L65 - L71 were not covered by tests
}
}

func (f *otelFactory) Timer(options TimerOptions) Timer {

Check failure on line 75 in pkg/metrics/factory.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'f' is not referenced in method's body, consider removing or renaming it as _ (revive)

Check warning on line 75 in pkg/metrics/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/metrics/factory.go#L75

Added line #L75 was not covered by tests
// Implement the OTEL Timer
return NullTimer

Check warning on line 77 in pkg/metrics/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/metrics/factory.go#L77

Added line #L77 was not covered by tests
}

func (f *otelFactory) Gauge(options Options) Gauge {

Check failure on line 80 in pkg/metrics/factory.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'f' is not referenced in method's body, consider removing or renaming it as _ (revive)

Check warning on line 80 in pkg/metrics/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/metrics/factory.go#L80

Added line #L80 was not covered by tests
// Implement the OTEL Gauge
return NullGauge

Check warning on line 82 in pkg/metrics/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/metrics/factory.go#L82

Added line #L82 was not covered by tests
}

func (f *otelFactory) Histogram(options HistogramOptions) Histogram {

Check failure on line 85 in pkg/metrics/factory.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'f' is not referenced in method's body, consider removing or renaming it as _ (revive)

Check warning on line 85 in pkg/metrics/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/metrics/factory.go#L85

Added line #L85 was not covered by tests
// Implement the OTEL Histogram
return NullHistogram

Check warning on line 87 in pkg/metrics/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/metrics/factory.go#L87

Added line #L87 was not covered by tests
}

func (f *otelFactory) Namespace(scope NSOptions) Factory {

Check failure on line 90 in pkg/metrics/factory.go

View workflow job for this annotation

GitHub Actions / lint

unused-receiver: method receiver 'f' is not referenced in method's body, consider removing or renaming it as _ (revive)
return &otelFactory{
meter: otel.Meter(scope.Name),

Check warning on line 92 in pkg/metrics/factory.go

View check run for this annotation

Codecov / codecov/patch

pkg/metrics/factory.go#L90-L92

Added lines #L90 - L92 were not covered by tests
}
}

// Factory creates new metrics
type Factory interface {
Counter(metric Options) Counter
Expand Down
28 changes: 28 additions & 0 deletions pkg/metrics/otelCounter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package metrics

import (
"context"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)

// otelCounter is a wrapper around otel.Counter

type otelCounter struct {
counter metric.Int64Counter
ctx context.Context
attrs []metric.AddOption
}

func (c *otelCounter) Inc(value int64) {
c.counter.Add(c.ctx, value, c.attrs...)

Check warning on line 19 in pkg/metrics/otelCounter.go

View check run for this annotation

Codecov / codecov/patch

pkg/metrics/otelCounter.go#L18-L19

Added lines #L18 - L19 were not covered by tests
}

func getAttributes(tags map[string]string) []metric.AddOption {
var options []metric.AddOption
for k, v := range tags {
options = append(options, metric.WithAttributes(attribute.String(k, v)))

Check warning on line 25 in pkg/metrics/otelCounter.go

View check run for this annotation

Codecov / codecov/patch

pkg/metrics/otelCounter.go#L22-L25

Added lines #L22 - L25 were not covered by tests
}
return options

Check warning on line 27 in pkg/metrics/otelCounter.go

View check run for this annotation

Codecov / codecov/patch

pkg/metrics/otelCounter.go#L27

Added line #L27 was not covered by tests
}

0 comments on commit f1f9b7a

Please sign in to comment.