Skip to content

Commit

Permalink
chore: respond to PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps committed Apr 24, 2024
1 parent aa2d99a commit 043fc7c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
13 changes: 3 additions & 10 deletions internal/cmd/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ import (
"go.flipt.io/flipt/internal/tracing"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel"
metric "go.opentelemetry.io/otel/metric"
metricnoop "go.opentelemetry.io/otel/metric/noop"
metricsdk "go.opentelemetry.io/otel/sdk/metric"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.uber.org/zap"
Expand Down Expand Up @@ -154,9 +152,6 @@ func NewGRPCServer(

logger.Debug("store enabled", zap.Stringer("store", store))

// Default to a no-op OTEL meter provider
var meterProvider metric.MeterProvider = metricnoop.NewMeterProvider()

// Initialize metrics exporter if enabled
if cfg.Metrics.Enabled {
metricExp, metricExpShutdown, err := metrics.GetExporter(ctx, &cfg.Metrics)
Expand All @@ -166,14 +161,12 @@ func NewGRPCServer(

server.onShutdown(metricExpShutdown)

meterProvider = metricsdk.NewMeterProvider(metricsdk.WithReader(metricExp))
meterProvider := metricsdk.NewMeterProvider(metricsdk.WithReader(metricExp))
otel.SetMeterProvider(meterProvider)

logger.Debug("otel metrics enabled", zap.String("exporter", string(cfg.Metrics.Exporter)))
}

otel.SetMeterProvider(meterProvider)

metrics.Meter = meterProvider.Meter("github.com/flipt-io/flipt")

// Initialize tracingProvider regardless of configuration. No extraordinary resources
// are consumed, or goroutines initialized until a SpanProcessor is registered.
tracingProvider, err := tracing.NewProvider(ctx, info.Version, cfg.Tracing)
Expand Down
2 changes: 1 addition & 1 deletion internal/config/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
type MetricsConfig struct {
Enabled bool `json:"enabled" mapstructure:"enabled" yaml:"enabled"`
Exporter MetricsExporter `json:"exporter,omitempty" mapstructure:"exporter" yaml:"exporter,omitempty"`
OTLP *OTLPMetricsConfig `json:"otlp,omitempty" mapstructure:"otlp" yaml:"otlp,omitempty"`
OTLP *OTLPMetricsConfig `json:"otlp,omitempty" mapstructure:"otlp,omitempty" yaml:"otlp,omitempty"`
}

type OTLPMetricsConfig struct {
Expand Down
28 changes: 13 additions & 15 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"

"go.flipt.io/flipt/internal/config"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/exporters/prometheus"
Expand All @@ -15,16 +16,13 @@ import (
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
)

// Meter is the default Flipt-wide otel metric Meter.
var Meter metric.Meter

func init() {
// If the global Meter is nil, create a new noop Meter.
// Useful for testing.
if Meter == nil {
meterProvider := metricnoop.NewMeterProvider()
Meter = meterProvider.Meter("github.com/flipt-io/flipt")
}
otel.SetMeterProvider(metricnoop.NewMeterProvider())
}

// This is memoized in the OTEL library to avoid creating multiple instances of the same exporter.
func meter() metric.Meter {
return otel.Meter("github.com/flipt-io/flipt")
}

// MustInt64 returns an instrument provider based on the global Meter.
Expand Down Expand Up @@ -55,7 +53,7 @@ type mustInt64Meter struct{}

// Counter creates an instrument for recording increasing values.
func (m mustInt64Meter) Counter(name string, opts ...metric.Int64CounterOption) metric.Int64Counter {
counter, err := Meter.Int64Counter(name, opts...)
counter, err := meter().Int64Counter(name, opts...)
if err != nil {
panic(err)
}
Expand All @@ -65,7 +63,7 @@ func (m mustInt64Meter) Counter(name string, opts ...metric.Int64CounterOption)

// UpDownCounter creates an instrument for recording changes of a value.
func (m mustInt64Meter) UpDownCounter(name string, opts ...metric.Int64UpDownCounterOption) metric.Int64UpDownCounter {
counter, err := Meter.Int64UpDownCounter(name, opts...)
counter, err := meter().Int64UpDownCounter(name, opts...)
if err != nil {
panic(err)
}
Expand All @@ -75,7 +73,7 @@ func (m mustInt64Meter) UpDownCounter(name string, opts ...metric.Int64UpDownCou

// Histogram creates an instrument for recording a distribution of values.
func (m mustInt64Meter) Histogram(name string, opts ...metric.Int64HistogramOption) metric.Int64Histogram {
hist, err := Meter.Int64Histogram(name, opts...)
hist, err := meter().Int64Histogram(name, opts...)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -111,7 +109,7 @@ type mustFloat64Meter struct{}

// Counter creates an instrument for recording increasing values.
func (m mustFloat64Meter) Counter(name string, opts ...metric.Float64CounterOption) metric.Float64Counter {
counter, err := Meter.Float64Counter(name, opts...)
counter, err := meter().Float64Counter(name, opts...)
if err != nil {
panic(err)
}
Expand All @@ -121,7 +119,7 @@ func (m mustFloat64Meter) Counter(name string, opts ...metric.Float64CounterOpti

// UpDownCounter creates an instrument for recording changes of a value.
func (m mustFloat64Meter) UpDownCounter(name string, opts ...metric.Float64UpDownCounterOption) metric.Float64UpDownCounter {
counter, err := Meter.Float64UpDownCounter(name, opts...)
counter, err := meter().Float64UpDownCounter(name, opts...)
if err != nil {
panic(err)
}
Expand All @@ -131,7 +129,7 @@ func (m mustFloat64Meter) UpDownCounter(name string, opts ...metric.Float64UpDow

// Histogram creates an instrument for recording a distribution of values.
func (m mustFloat64Meter) Histogram(name string, opts ...metric.Float64HistogramOption) metric.Float64Histogram {
hist, err := Meter.Float64Histogram(name, opts...)
hist, err := meter().Float64Histogram(name, opts...)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 043fc7c

Please sign in to comment.