-
Notifications
You must be signed in to change notification settings - Fork 573
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] add NewSDK func #4414
Merged
Merged
[config] add NewSDK func #4414
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b166c58
[config] add NewSDK func
f24cfcc
Update config/config_test.go
codeboten 5753343
rename test vars as per feedback
8178684
precommit
be372c4
Update CHANGELOG.md
pellared 30d2bbe
Merge branch 'main' into codeboten/config-new
pellared a699f0e
Merge branch 'main' into codeboten/config-new
pellared 0d7a16f
Merge branch 'main' into codeboten/config-new
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package config // import "go.opentelemetry.io/contrib/config" | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/otel/metric" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
type configOptions struct { | ||
ctx context.Context | ||
opentelemetryConfig OpenTelemetryConfiguration | ||
} | ||
|
||
// SDK is a struct that contains all the providers | ||
// configured via the configuration model. | ||
type SDK struct { | ||
meterProvider metric.MeterProvider | ||
tracerProvider trace.TracerProvider | ||
} | ||
|
||
// TracerProvider returns a configured trace.TracerProvider. | ||
func (s *SDK) TracerProvider() trace.TracerProvider { | ||
return s.tracerProvider | ||
} | ||
|
||
// MeterProvider returns a configured metric.MeterProvider. | ||
func (s *SDK) MeterProvider() metric.MeterProvider { | ||
return s.meterProvider | ||
} | ||
|
||
// NewSDK creates SDK providers based on the configuration model. | ||
func NewSDK(opts ...ConfigurationOption) (SDK, error) { | ||
o := configOptions{} | ||
for _, opt := range opts { | ||
o = opt.apply(o) | ||
} | ||
|
||
return SDK{ | ||
meterProvider: initMeterProvider(o), | ||
tracerProvider: initTracerProvider(o), | ||
}, nil | ||
} | ||
|
||
// ConfigurationOption configures options for providers. | ||
type ConfigurationOption interface { | ||
apply(configOptions) configOptions | ||
} | ||
|
||
type configurationOptionFunc func(configOptions) configOptions | ||
|
||
func (fn configurationOptionFunc) apply(cfg configOptions) configOptions { | ||
return fn(cfg) | ||
} | ||
|
||
// WithContext sets the context.Context for the SDK. | ||
func WithContext(ctx context.Context) ConfigurationOption { | ||
return configurationOptionFunc(func(c configOptions) configOptions { | ||
c.ctx = ctx | ||
return c | ||
}) | ||
} | ||
|
||
// WithOpenTelemetryConfiguration sets the OpenTelemetryConfiguration used | ||
// to produce the SDK. | ||
func WithOpenTelemetryConfiguration(cfg OpenTelemetryConfiguration) ConfigurationOption { | ||
return configurationOptionFunc(func(c configOptions) configOptions { | ||
c.opentelemetryConfig = cfg | ||
return c | ||
}) | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package config | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/otel/metric/noop" | ||
sdkmetric "go.opentelemetry.io/otel/sdk/metric" | ||
sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
func TestNewSDK(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cfg []ConfigurationOption | ||
wantTracerProvider any | ||
wantMeterProvider any | ||
wantErr error | ||
}{ | ||
{ | ||
name: "no-configuration", | ||
wantTracerProvider: trace.NewNoopTracerProvider(), | ||
wantMeterProvider: noop.NewMeterProvider(), | ||
}, | ||
{ | ||
name: "with-configuration", | ||
cfg: []ConfigurationOption{ | ||
WithContext(context.Background()), | ||
WithOpenTelemetryConfiguration(OpenTelemetryConfiguration{ | ||
TracerProvider: &TracerProvider{}, | ||
pellared marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MeterProvider: &MeterProvider{}, | ||
}), | ||
}, | ||
wantTracerProvider: &sdktrace.TracerProvider{}, | ||
wantMeterProvider: &sdkmetric.MeterProvider{}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
sdk, err := NewSDK(tt.cfg...) | ||
require.Equal(t, tt.wantErr, err) | ||
assert.IsType(t, tt.wantTracerProvider, sdk.TracerProvider()) | ||
assert.IsType(t, tt.wantMeterProvider, sdk.MeterProvider()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,21 @@ | ||
module go.opentelemetry.io/contrib/config | ||
|
||
go 1.20 | ||
|
||
require ( | ||
github.com/stretchr/testify v1.8.4 | ||
go.opentelemetry.io/otel/metric v1.19.0 | ||
go.opentelemetry.io/otel/sdk v1.19.0 | ||
go.opentelemetry.io/otel/sdk/metric v1.19.0 | ||
go.opentelemetry.io/otel/trace v1.19.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/go-logr/logr v1.2.4 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
go.opentelemetry.io/otel v1.19.0 // indirect | ||
golang.org/x/sys v0.12.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= | ||
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= | ||
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= | ||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= | ||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= | ||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs= | ||
go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY= | ||
go.opentelemetry.io/otel/metric v1.19.0 h1:aTzpGtV0ar9wlV4Sna9sdJyII5jTVJEvKETPiOKwvpE= | ||
go.opentelemetry.io/otel/metric v1.19.0/go.mod h1:L5rUsV9kM1IxCj1MmSdS+JQAcVm319EUrDVLrt7jqt8= | ||
go.opentelemetry.io/otel/sdk v1.19.0 h1:6USY6zH+L8uMH8L3t1enZPR3WFEmSTADlqldyHtJi3o= | ||
go.opentelemetry.io/otel/sdk v1.19.0/go.mod h1:NedEbbS4w3C6zElbLdPJKOpJQOrGUJ+GfzpjUvI0v1A= | ||
go.opentelemetry.io/otel/sdk/metric v1.19.0 h1:EJoTO5qysMsYCa+w4UghwFV/ptQgqSL/8Ni+hx+8i1k= | ||
go.opentelemetry.io/otel/sdk/metric v1.19.0/go.mod h1:XjG0jQyFJrv2PbMvwND7LwCEhsJzCzV5210euduKcKY= | ||
go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg= | ||
go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo= | ||
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= | ||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package config // import "go.opentelemetry.io/contrib/config" | ||
|
||
import ( | ||
"go.opentelemetry.io/otel/metric" | ||
"go.opentelemetry.io/otel/metric/noop" | ||
sdkmetric "go.opentelemetry.io/otel/sdk/metric" | ||
) | ||
|
||
func initMeterProvider(cfg configOptions) metric.MeterProvider { | ||
if cfg.opentelemetryConfig.MeterProvider == nil { | ||
return noop.NewMeterProvider() | ||
} | ||
return sdkmetric.NewMeterProvider() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/otel/metric" | ||
"go.opentelemetry.io/otel/metric/noop" | ||
) | ||
|
||
func TestInitMeterProvider(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cfg configOptions | ||
wantProvider metric.MeterProvider | ||
}{ | ||
{ | ||
name: "no-meter-provider-configured", | ||
wantProvider: noop.NewMeterProvider(), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
require.Equal(t, tt.wantProvider, initMeterProvider(tt.cfg)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package config // import "go.opentelemetry.io/contrib/config" | ||
|
||
import ( | ||
sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
func initTracerProvider(cfg configOptions) trace.TracerProvider { | ||
if cfg.opentelemetryConfig.TracerProvider == nil { | ||
return trace.NewNoopTracerProvider() | ||
} | ||
return sdktrace.NewTracerProvider() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
func TestInitTracerPovider(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cfg configOptions | ||
wantProvider trace.TracerProvider | ||
}{ | ||
{ | ||
name: "no-tracer-provider-configured", | ||
wantProvider: trace.NewNoopTracerProvider(), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
require.Equal(t, tt.wantProvider, initTracerProvider(tt.cfg)) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Shouldn't
cfg OpenTelemetryConfiguration
be a required parameter?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.
It could definitely be, i suppose not setting it would be useful for someone who wants a noop SDK... not sure if that's a realistic use-case, but i figured it would be easier to be consistent w/ all options.
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.
Interesting use case. For me you can leave it as it is. You can resolve the comment or ask/wait for other opinions.