Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Boten
committed
Oct 20, 2023
1 parent
488b204
commit 8918009
Showing
9 changed files
with
263 additions
and
1 deletion.
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{}, | ||
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)) | ||
} | ||
} |