Skip to content

Commit

Permalink
Merge branch 'main' into slogbridge-withsource
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse0Michael authored Oct 17, 2024
2 parents 35441db + 13536dd commit 066b5a3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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
42 changes: 26 additions & 16 deletions config/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,28 +298,16 @@ func newIncludeExcludeFilter(lists *IncludeExclude) (attribute.Filter, error) {
}

func prometheusReader(ctx context.Context, prometheusConfig *Prometheus) (sdkmetric.Reader, 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())
}
if prometheusConfig.WithoutTypeSuffix != nil && *prometheusConfig.WithoutTypeSuffix {
opts = append(opts, otelprom.WithoutCounterSuffixes())
}
if prometheusConfig.WithoutUnits != nil && *prometheusConfig.WithoutUnits {
opts = append(opts, otelprom.WithoutUnits())
}
if prometheusConfig.WithResourceConstantLabels != nil {
f, err := newIncludeExcludeFilter(prometheusConfig.WithResourceConstantLabels)
if err != nil {
return nil, err
}
otelprom.WithResourceAsConstantLabels(f)

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

reg := prometheus.NewRegistry()
Expand Down Expand Up @@ -358,6 +346,28 @@ func prometheusReader(ctx context.Context, prometheusConfig *Prometheus) (sdkmet
return readerWithServer{reader, &server}, nil
}

func prometheusReaderOpts(prometheusConfig *Prometheus) ([]otelprom.Option, error) {
var opts []otelprom.Option
if prometheusConfig.WithoutScopeInfo != nil && *prometheusConfig.WithoutScopeInfo {
opts = append(opts, otelprom.WithoutScopeInfo())
}
if prometheusConfig.WithoutTypeSuffix != nil && *prometheusConfig.WithoutTypeSuffix {
opts = append(opts, otelprom.WithoutCounterSuffixes())
}
if prometheusConfig.WithoutUnits != nil && *prometheusConfig.WithoutUnits {
opts = append(opts, otelprom.WithoutUnits())
}
if prometheusConfig.WithResourceConstantLabels != nil {
f, err := newIncludeExcludeFilter(prometheusConfig.WithResourceConstantLabels)
if err != nil {
return nil, err
}
opts = append(opts, otelprom.WithResourceAsConstantLabels(f))
}

return opts, nil
}

type readerWithServer struct {
sdkmetric.Reader
server *http.Server
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)
})
}
}

0 comments on commit 066b5a3

Please sign in to comment.