Skip to content

Commit

Permalink
move code to disable high cardinality to use views in service
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Boten <[email protected]>
  • Loading branch information
codeboten committed Nov 5, 2024
1 parent 90944a3 commit ad621ea
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 136 deletions.
52 changes: 52 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/receiver"
semconv118 "go.opentelemetry.io/collector/semconv/v1.18.0"
semconv "go.opentelemetry.io/collector/semconv/v1.26.0"
"go.opentelemetry.io/collector/service/extensions"
"go.opentelemetry.io/collector/service/internal/builders"
Expand All @@ -48,6 +49,14 @@ var _ = featuregate.GlobalRegistry().MustRegister(
featuregate.WithRegisterDescription("controls whether the collector supports extended OpenTelemetry"+
"configuration for internal telemetry"))

// disableHighCardinalityMetricsFeatureGate is the feature gate that controls whether the collector should enable
// potentially high cardinality metrics. The gate will be removed when the collector allows for view configuration.
var disableHighCardinalityMetricsFeatureGate = featuregate.GlobalRegistry().MustRegister(
"telemetry.disableHighCardinalityMetrics",
featuregate.StageAlpha,
featuregate.WithRegisterDescription("controls whether the collector should enable potentially high"+
"cardinality metrics. The gate will be removed when the collector allows for view configuration."))

// Settings holds configuration for building a new Service.
type Settings struct {
// BuildInfo provides collector start information.
Expand Down Expand Up @@ -126,6 +135,8 @@ func New(ctx context.Context, set Settings, cfg Config) (*Service, error) {
Attributes: attributes(res, cfg.Telemetry),
}

views := disableHighCardinalityMetrics()

sdk, err := config.NewSDK(
config.WithContext(ctx),
config.WithOpenTelemetryConfiguration(
Expand All @@ -135,6 +146,7 @@ func New(ctx context.Context, set Settings, cfg Config) (*Service, error) {
},
MeterProvider: &config.MeterProvider{
Readers: cfg.Telemetry.Metrics.Readers,
Views: views,
},
TracerProvider: &config.TracerProvider{
Processors: cfg.Telemetry.Traces.Processors,
Expand Down Expand Up @@ -373,3 +385,43 @@ func pdataFromSdk(res *sdkresource.Resource) pcommon.Resource {
}
return pcommonRes
}

func disableHighCardinalityMetrics() []config.View {
var views []config.View
if disableHighCardinalityMetricsFeatureGate.IsEnabled() {
return views
}
return []config.View{
{
Selector: &config.ViewSelector{
MeterName: ptr("go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"),
},
Stream: &config.ViewStream{
AttributeKeys: &config.IncludeExclude{
Excluded: []string{
semconv118.AttributeNetSockPeerAddr,
semconv118.AttributeNetSockPeerPort,
semconv118.AttributeNetSockPeerName,
},
},
},
},
{
Selector: &config.ViewSelector{
MeterName: ptr("go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"),
},
Stream: &config.ViewStream{
AttributeKeys: &config.IncludeExclude{
Excluded: []string{
semconv118.AttributeNetHostName,
semconv118.AttributeNetHostPort,
},
},
},
},
}
}

func ptr[T any](v T) *T {
return &v
}
11 changes: 1 addition & 10 deletions service/telemetry/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ var useLocalHostAsDefaultMetricsAddressFeatureGate = featuregate.GlobalRegistry(
featuregate.WithRegisterDescription("controls whether default Prometheus metrics server use localhost as the default host for their endpoints"),
)

// disableHighCardinalityMetricsFeatureGate is the feature gate that controls whether the collector should enable
// potentially high cardinality metrics. The gate will be removed when the collector allows for view configuration.
var disableHighCardinalityMetricsFeatureGate = featuregate.GlobalRegistry().MustRegister(
"telemetry.disableHighCardinalityMetrics",
featuregate.StageAlpha,
featuregate.WithRegisterDescription("controls whether the collector should enable potentially high"+
"cardinality metrics. The gate will be removed when the collector allows for view configuration."))

// Settings holds configuration for building Telemetry.
type Settings struct {
BuildInfo component.BuildInfo
Expand Down Expand Up @@ -76,8 +68,7 @@ func NewFactory() Factory {
}),
withMeterProvider(func(_ context.Context, set Settings, cfg component.Config) (metric.MeterProvider, error) {
c := *cfg.(*Config)
disableHighCard := disableHighCardinalityMetricsFeatureGate.IsEnabled()
return newMeterProvider(set, c, disableHighCard)
return newMeterProvider(set, c)
}),
)
}
Expand Down
18 changes: 1 addition & 17 deletions service/telemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ import (
"go.opentelemetry.io/collector/config/configtelemetry"
)

const (
zapKeyTelemetryAddress = "address"
zapKeyTelemetryLevel = "metrics level"
)

// newMeterProvider creates a new MeterProvider from Config.
func newMeterProvider(set Settings, cfg Config, disableHighCardinality bool) (metric.MeterProvider, error) {
func newMeterProvider(set Settings, cfg Config) (metric.MeterProvider, error) {
if cfg.Metrics.Level == configtelemetry.LevelNone || len(cfg.Metrics.Readers) == 0 {
return noop.NewMeterProvider(), nil
}
Expand All @@ -28,14 +23,3 @@ func newMeterProvider(set Settings, cfg Config, disableHighCardinality bool) (me
}
return nil, errors.New("no sdk set")
}

// LogAboutServers logs about the servers that are serving metrics.
// func (mp *meterProvider) LogAboutServers(logger *zap.Logger, cfg MetricsConfig) {
// for _, server := range mp.servers {
// logger.Info(
// "Serving metrics",
// zap.String(zapKeyTelemetryAddress, server.Addr),
// zap.Stringer(zapKeyTelemetryLevel, cfg.Level),
// )
// }
// }
157 changes: 48 additions & 109 deletions service/telemetry/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func TestTelemetryInit(t *testing.T) {

for _, tt := range []struct {
name string
disableHighCard bool
expectedMetrics map[string]metricValue
}{
{
Expand All @@ -48,106 +47,38 @@ func TestTelemetryInit(t *testing.T) {
metricPrefix + otelPrefix + counterName: {
value: 13,
labels: map[string]string{
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
"telemetry_sdk_language": "go",
"telemetry_sdk_name": "opentelemetry",
"telemetry_sdk_version": "1.31.0",
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
},
},
metricPrefix + grpcPrefix + counterName: {
value: 11,
labels: map[string]string{
"net_sock_peer_addr": "",
"net_sock_peer_name": "",
"net_sock_peer_port": "",
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
"telemetry_sdk_language": "go",
"telemetry_sdk_name": "opentelemetry",
"telemetry_sdk_version": "1.31.0",
"net_sock_peer_addr": "",
"net_sock_peer_name": "",
"net_sock_peer_port": "",
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
},
},
metricPrefix + httpPrefix + counterName: {
value: 10,
labels: map[string]string{
"net_host_name": "",
"net_host_port": "",
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
"telemetry_sdk_language": "go",
"telemetry_sdk_name": "opentelemetry",
"telemetry_sdk_version": "1.31.0",
"net_host_name": "",
"net_host_port": "",
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
},
},
"target_info": {
value: 0,
labels: map[string]string{
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
"telemetry_sdk_language": "go",
"telemetry_sdk_name": "opentelemetry",
"telemetry_sdk_version": "1.31.0",
},
},
"promhttp_metric_handler_errors_total": {
value: 0,
labels: map[string]string{
"cause": "encoding",
},
},
},
},
{
name: "DisableHighCardinalityWithOtel",
disableHighCard: true,
expectedMetrics: map[string]metricValue{
metricPrefix + otelPrefix + counterName: {
value: 13,
labels: map[string]string{
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
"telemetry_sdk_language": "go",
"telemetry_sdk_name": "opentelemetry",
"telemetry_sdk_version": "1.31.0",
},
},
metricPrefix + grpcPrefix + counterName: {
value: 11,
labels: map[string]string{
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
"telemetry_sdk_language": "go",
"telemetry_sdk_name": "opentelemetry",
"telemetry_sdk_version": "1.31.0",
},
},
metricPrefix + httpPrefix + counterName: {
value: 10,
labels: map[string]string{
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
"telemetry_sdk_language": "go",
"telemetry_sdk_name": "opentelemetry",
"telemetry_sdk_version": "1.31.0",
},
},
"target_info": {
value: 0,
labels: map[string]string{
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
"telemetry_sdk_language": "go",
"telemetry_sdk_name": "opentelemetry",
"telemetry_sdk_version": "1.31.0",
"service_name": "otelcol",
"service_version": "latest",
"service_instance_id": testInstanceID,
},
},
"promhttp_metric_handler_errors_total": {
Expand All @@ -161,32 +92,36 @@ func TestTelemetryInit(t *testing.T) {
} {
prom := promtest.GetAvailableLocalAddressPrometheus(t)
endpoint := fmt.Sprintf("http://%s:%d/metrics", *prom.Host, *prom.Port)
cfg := Config{
Metrics: MetricsConfig{
Level: configtelemetry.LevelDetailed,
Readers: []config.MetricReader{{
Pull: &config.PullMetricReader{
Exporter: config.PullMetricExporter{Prometheus: prom},
},
}},
},
}
t.Run(tt.name, func(t *testing.T) {
cfg := &Config{
Metrics: MetricsConfig{
Level: configtelemetry.LevelDetailed,
Readers: []config.MetricReader{{
Pull: &config.PullMetricReader{Exporter: config.PullMetricExporter{Prometheus: prom}},
}},
},
Traces: TracesConfig{
Processors: []config.SpanProcessor{
{
Batch: &config.BatchSpanProcessor{
Exporter: config.SpanExporter{
Console: config.Console{},
},
},
sdk, err := config.NewSDK(
config.WithContext(context.Background()),
config.WithOpenTelemetryConfiguration(config.OpenTelemetryConfiguration{
MeterProvider: &config.MeterProvider{
Readers: cfg.Metrics.Readers,
},
Resource: &config.Resource{
SchemaUrl: ptr(""),
Attributes: []config.AttributeNameValue{
{Name: semconv.AttributeServiceInstanceID, Value: testInstanceID},
{Name: semconv.AttributeServiceName, Value: "otelcol"},
{Name: semconv.AttributeServiceVersion, Value: "latest"},
},
},
},
Resource: map[string]*string{
semconv.AttributeServiceInstanceID: &testInstanceID,
semconv.AttributeServiceName: ptr("otelcol"),
semconv.AttributeServiceVersion: ptr("latest"),
},
}
mp, err := newMeterProvider(context.Background(), Settings{}, *cfg, tt.disableHighCard)
}),
)
require.NoError(t, err)

mp, err := newMeterProvider(Settings{SDK: &sdk}, cfg)
require.NoError(t, err)
defer func() {
if prov, ok := mp.(interface{ Shutdown(context.Context) error }); ok {
Expand Down Expand Up @@ -254,3 +189,7 @@ func getMetricsFromPrometheus(t *testing.T, endpoint string) map[string]*io_prom

return parsed
}

func ptr[T any](v T) *T {
return &v
}

0 comments on commit ad621ea

Please sign in to comment.