Skip to content
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

config: fix bug where WithResourceAsConstantLabels wasn't set #6260

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Transform nil attribute values to `log.Value` zero value instead of panicking in `go.opentelemetry.io/contrib/bridges/otelzap`. (#6237)
- Transform nil attribute values to `log.Value` zero value instead of `log.StringValue("<nil>")` in `go.opentelemetry.io/contrib/bridges/otelslog`. (#6246)
- Fix `NewClientHandler` so that `rpc.client.request.*` metrics measure requests instead of responses and `rpc.client.responses.*` metrics measure responses instead of requests in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc`. (#6250)
- Fix issue in `go.opentelemetry.io/contrib/config` causing `otelprom.WithResourceAsConstantLabels` configuration to not be respected. (#6260)

<!-- Released section -->
<!-- Don't change this section unless doing release -->
Expand Down
26 changes: 18 additions & 8 deletions config/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,8 @@
}, nil
}

func prometheusReader(ctx context.Context, prometheusConfig *Prometheus) (sdkmetric.Reader, error) {
func prometheusReaderOpts(prometheusConfig *Prometheus) ([]otelprom.Option, error) {
var opts []otelprom.Option
if prometheusConfig.Host == nil {
return nil, fmt.Errorf("host must be specified")
}
if prometheusConfig.Port == nil {
return nil, fmt.Errorf("port must be specified")
}
if prometheusConfig.WithoutScopeInfo != nil && *prometheusConfig.WithoutScopeInfo {
opts = append(opts, otelprom.WithoutScopeInfo())
}
Expand All @@ -319,7 +313,23 @@
if err != nil {
return nil, err
}
otelprom.WithResourceAsConstantLabels(f)
opts = append(opts, otelprom.WithResourceAsConstantLabels(f))
}

return opts, nil
}

func prometheusReader(ctx context.Context, prometheusConfig *Prometheus) (sdkmetric.Reader, error) {
codeboten marked this conversation as resolved.
Show resolved Hide resolved
if prometheusConfig.Host == nil {
return nil, fmt.Errorf("host must be specified")
}
if prometheusConfig.Port == nil {
return nil, fmt.Errorf("port must be specified")
}

opts, err := prometheusReaderOpts(prometheusConfig)
if err != nil {
return nil, err

Check warning on line 332 in config/metric.go

View check run for this annotation

Codecov / codecov/patch

config/metric.go#L332

Added line #L332 was not covered by tests
}

reg := prometheus.NewRegistry()
Expand Down
41 changes: 41 additions & 0 deletions config/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1138,3 +1138,44 @@ func TestNewIncludeExcludeFilterError(t *testing.T) {
}))
require.Equal(t, fmt.Errorf("attribute cannot be in both include and exclude list: foo"), err)
}

func TestPrometheusReaderOpts(t *testing.T) {
testCases := []struct {
name string
cfg Prometheus
wantOptions int
}{
{
name: "no options",
cfg: Prometheus{},
wantOptions: 0,
},
{
name: "all set",
cfg: Prometheus{
WithoutScopeInfo: ptr(true),
WithoutTypeSuffix: ptr(true),
WithoutUnits: ptr(true),
WithResourceConstantLabels: &IncludeExclude{},
},
wantOptions: 4,
},
{
name: "all set false",
cfg: Prometheus{
WithoutScopeInfo: ptr(false),
WithoutTypeSuffix: ptr(false),
WithoutUnits: ptr(false),
WithResourceConstantLabels: &IncludeExclude{},
},
wantOptions: 1,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
opts, err := prometheusReaderOpts(&tt.cfg)
require.NoError(t, err)
require.Len(t, opts, tt.wantOptions)
})
}
}
Loading