-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add support to emit Metrics same as Traces via OTEL #456
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,16 +96,19 @@ func Load(path string) (*ServiceConfig, error) { | |
} | ||
|
||
serverConfig := ServerConfig{ | ||
Address: fmt.Sprintf(":%d", viper.GetUint(OpenSavesPort)), | ||
Cloud: viper.GetString(OpenSavesCloud), | ||
Bucket: viper.GetString(OpenSavesBucket), | ||
Project: viper.GetString(OpenSavesProject), | ||
ShutdownGracePeriod: viper.GetDuration(ShutdownGracePeriod), | ||
EnableTrace: viper.GetBool(EnableTrace), | ||
TraceSampleRate: viper.GetFloat64(TraceSampleRate), | ||
TraceServiceName: viper.GetString(TraceServiceName), | ||
EnableGRPCCollector: viper.GetBool(TraceEnableGRPCCollector), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added the Trace prefix for both EnableGRPCCollector and EnableHTTPCollector to differentiate them from the counter parts of Metrics. It is important to mention that the OTLP endpoint for Traces and Metrics could or could not be the same. |
||
EnableHTTPCollector: viper.GetBool(TraceEnableHTTPCollector), | ||
Address: fmt.Sprintf(":%d", viper.GetUint(OpenSavesPort)), | ||
Cloud: viper.GetString(OpenSavesCloud), | ||
Bucket: viper.GetString(OpenSavesBucket), | ||
Project: viper.GetString(OpenSavesProject), | ||
ShutdownGracePeriod: viper.GetDuration(ShutdownGracePeriod), | ||
EnableMetrics: viper.GetBool(EnableTrace), | ||
MetricsEnableGRPCCollector: viper.GetBool(MetricsEnableGRPCCollector), | ||
MetricsEnableHTTPCollector: viper.GetBool(MetricsEnableHTTPCollector), | ||
EnableTrace: viper.GetBool(EnableTrace), | ||
TraceSampleRate: viper.GetFloat64(TraceSampleRate), | ||
TraceServiceName: viper.GetString(TraceServiceName), | ||
TraceEnableGRPCCollector: viper.GetBool(TraceEnableGRPCCollector), | ||
TraceEnableHTTPCollector: viper.GetBool(TraceEnableHTTPCollector), | ||
} | ||
|
||
// Cloud Run environment populates the PORT env var, so check for it here. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package metrics | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" | ||
"go.opentelemetry.io/otel/sdk/metric" | ||
sdkresource "go.opentelemetry.io/otel/sdk/resource" | ||
) | ||
|
||
func InitMetrics(enableGRPCCollector bool, enableHTTPCollector bool) (*metric.MeterProvider, error) { | ||
extraResources, _ := sdkresource.New( | ||
context.Background(), | ||
sdkresource.WithOS(), | ||
sdkresource.WithProcess(), | ||
sdkresource.WithContainer(), | ||
sdkresource.WithHost(), | ||
sdkresource.WithAttributes(), | ||
) | ||
resource, _ := sdkresource.Merge( | ||
sdkresource.Default(), | ||
extraResources, | ||
) | ||
|
||
options := []metric.Option{metric.WithResource(resource)} | ||
|
||
if enableGRPCCollector { | ||
exporter, err := otlpmetricgrpc.New(context.Background()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
options = append(options, metric.WithReader(metric.NewPeriodicReader(exporter))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are options we can configure at the PeriodicReader, like the frequency, but I didn't want to make configuration too complex, and the defaults are sensible. |
||
} | ||
|
||
if enableHTTPCollector { | ||
exporter, err := otlpmetrichttp.New(context.Background()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
options = append(options, metric.WithReader(metric.NewPeriodicReader(exporter))) | ||
} | ||
|
||
meterProvider := metric.NewMeterProvider(options...) | ||
|
||
// Register as global meter provider so that it can be used via otel.Meter | ||
// and accessed using otel.GetMeterProvider. | ||
// Most instrumentation libraries use the global meter provider as default. | ||
// If the global meter provider is not set then a no-op implementation | ||
// is used, which fails to generate data. | ||
otel.SetMeterProvider(meterProvider) | ||
|
||
return meterProvider, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UnaryServerInterceptor and StreamServerInterceptor have been deprecated in favour of NewServerHandler()