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

Add autoexport to automatically configure trace exporters #2753

Merged
merged 64 commits into from May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
bc10897
add autoexport
MikeGoldsmith Sep 14, 2022
01b61b5
fix linting, add registry tests
MikeGoldsmith Sep 15, 2022
cf9e82d
update env var to OTEL_TRACES_EXPORTERS
MikeGoldsmith Sep 15, 2022
c03aaf6
update to go 1.18
MikeGoldsmith Sep 15, 2022
cd320da
add changelog entry
MikeGoldsmith Sep 15, 2022
c8eee46
add autoexport
MikeGoldsmith Sep 14, 2022
b33a022
fix linting, add registry tests
MikeGoldsmith Sep 15, 2022
8958075
update env var to OTEL_TRACES_EXPORTERS
MikeGoldsmith Sep 15, 2022
f5b23b4
update to go 1.18
MikeGoldsmith Sep 15, 2022
44d7ecb
add changelog entry
MikeGoldsmith Sep 15, 2022
8f220f6
Merge branch 'mike/autoexport' of https://github.com/honeycombio/open…
MikeGoldsmith Sep 15, 2022
baa5cb2
fix changelog
MikeGoldsmith Sep 15, 2022
ff36a60
fix typo :(
MikeGoldsmith Sep 15, 2022
ddc0597
refactor to only return single trace exporter
MikeGoldsmith Sep 15, 2022
78f8ebe
replace single case switch with if
MikeGoldsmith Sep 16, 2022
bec9d58
break up imports
MikeGoldsmith Sep 16, 2022
beb19ee
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
MikeGoldsmith Sep 23, 2022
dc65dd1
Update exporters/autoexport/exporter.go
MikeGoldsmith Sep 30, 2022
86b1759
move and rename errDupReg to var block
MikeGoldsmith Sep 30, 2022
bc32a8b
Apply suggestions from code review
MikeGoldsmith Oct 18, 2022
f30be94
fix const usage after rename
MikeGoldsmith Oct 18, 2022
e7e258e
update registry to reuse default otlp exporter
MikeGoldsmith Oct 19, 2022
4b15a95
update autoexport to use options design and register otlp exporters
MikeGoldsmith Oct 19, 2022
1bf9802
move otlp exporter registration to init
MikeGoldsmith Oct 19, 2022
cb5a453
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
MikeGoldsmith Oct 19, 2022
a9b6606
add autoexport to dependabot and version.yml
MikeGoldsmith Oct 19, 2022
718b1c0
fix linter and update descriptions
MikeGoldsmith Oct 19, 2022
7e44b89
fix typo
MikeGoldsmith Oct 19, 2022
ad5c4d0
update go.mod
MikeGoldsmith Oct 19, 2022
692c582
fix changelog
MikeGoldsmith Oct 19, 2022
0c03c27
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
MikeGoldsmith May 17, 2023
47b10b7
use go 1.19 in go.mod
MikeGoldsmith May 17, 2023
9647ace
update registry to use span exporter factory instead
MikeGoldsmith May 19, 2023
bd28bba
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
MikeGoldsmith May 19, 2023
9272837
fix formatting in registry
MikeGoldsmith May 22, 2023
6104122
update go.mod dependencies
MikeGoldsmith May 22, 2023
37fb2ee
update tests
MikeGoldsmith May 22, 2023
bd5d0dc
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
MikeGoldsmith May 22, 2023
b92e45d
add autoexport to codeowners
MikeGoldsmith May 22, 2023
e5453fa
update exporter func to get exporter from env
MikeGoldsmith May 22, 2023
f2808a6
fix typo in test name
MikeGoldsmith May 22, 2023
b5a3fde
Merge branch 'main' into mike/autoexport
MikeGoldsmith May 22, 2023
fab825c
reverrt test names to PascalCase instead of snake_case
MikeGoldsmith May 23, 2023
58dc7c9
Merge branch 'main' into mike/autoexport
MikeGoldsmith May 23, 2023
4bd3bbe
Apply suggestions from code review
MikeGoldsmith May 23, 2023
ef9afd3
Update CHANGELOG.md
MikeGoldsmith May 25, 2023
4a10f64
update config option pattern to match repo
MikeGoldsmith May 25, 2023
b1ffb17
update NewTraceExporter to return errors
MikeGoldsmith May 25, 2023
44578b0
update registry factory funcs to take context
MikeGoldsmith May 25, 2023
edac7cc
return factory return params direclty during load
MikeGoldsmith May 25, 2023
20499ec
add traceSpanExporterFactory type
MikeGoldsmith May 25, 2023
d34f914
pass in context when making exporter from env
MikeGoldsmith May 25, 2023
782c62e
add README
MikeGoldsmith May 25, 2023
d66ccd6
add doc.go
MikeGoldsmith May 25, 2023
a39a084
go mod tidy
MikeGoldsmith May 25, 2023
95a1948
fix formatting
MikeGoldsmith May 25, 2023
9536e1b
Update exporters/autoexport/doc.go
MikeGoldsmith May 25, 2023
f20dc48
move docs from readme to docs.go
MikeGoldsmith May 25, 2023
f5fd316
fix typo
MikeGoldsmith May 25, 2023
27f8710
Apply suggestions from code review
MikeGoldsmith May 25, 2023
8481734
remove traceSpanExporterFactoryFunc
MikeGoldsmith May 25, 2023
0a529dc
Merge branch 'mike/autoexport' of https://github.com/honeycombio/open…
MikeGoldsmith May 25, 2023
7efa4aa
renamed NewTraceExporter to NewSpanExporter
MikeGoldsmith May 25, 2023
d98ccee
Merge branch 'main' into mike/autoexport
MikeGoldsmith May 25, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Expand Up @@ -190,6 +190,15 @@ updates:
schedule:
interval: weekly
day: sunday
- package-ecosystem: gomod
directory: /exporters/autoexport
labels:
- dependencies
- go
- Skip Changelog
schedule:
interval: weekly
day: sunday
- package-ecosystem: gomod
directory: /instrgen
labels:
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Added

- Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068, #3108)
- The `go.opentelemetry.io/contrib/exporters/autoexport` package to provide configuration of trace exporters with useful defaults and envar support. (#2753)

## [1.17.0/0.42.0/0.11.0] - 2023-05-23

Expand Down Expand Up @@ -68,7 +69,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Fixed

- AWS SDK rename attributes `aws.operation`, `aws.service` to `rpc.method`,`rpc.service` in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws`. (#3582, #3617)
- AWS SDK rename attributes `aws.operation`, `aws.service` to `rpc.method`,`rpc.service` in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws`. (#3582, #3617)
- AWS SDK span name to be of the format `Service.Operation` in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws`. (#3582, #3521)
- Prevent sampler configuration reset from erroneously sampling first span in `go.opentelemetry.io/contrib/samplers/jaegerremote`. (#3603, #3604)

Expand Down
10 changes: 6 additions & 4 deletions CODEOWNERS
Validating CODEOWNERS rules …
Expand Up @@ -16,7 +16,7 @@
# instrumentation/net/http/otelhttp/ @open-telemetry/collector-go-approvers @madvikinggod @mralias
# Path separator and minimum of 1 space between component path and owners is
# important for validation steps
#
#

* @open-telemetry/go-approvers

Expand All @@ -25,19 +25,21 @@ CODEOWNERS @MrAlias @Aneurysm9 @MadVikingGod
detectors/aws/ @open-telemetry/go-approvers @Aneurysm9
detectors/gcp/ @open-telemetry/go-approvers @dashpole

exporters/autoexport @open-telemetry/go-approvers @MikeGoldsmith

instrumentation/github.com/astaxie/beego/otelbeego/ @open-telemetry/go-approvers
instrumentation/github.com/aws/aws-lambda-go/otellambda/ @open-telemetry/go-approvers @Aneurysm9
instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/ @open-telemetry/go-approvers @Aneurysm9
instrumentation/github.com/bradfitz/gomemcache/memcache/otelmemcache/ @open-telemetry/go-approvers
instrumentation/github.com/emicklei/go-restful/otelrestful/ @open-telemetry/go-approvers
instrumentation/github.com/gin-gonic/gin/otelgin/ @open-telemetry/go-approvers @hanyuancheung
instrumentation/github.com/gin-gonic/gin/otelgin/ @open-telemetry/go-approvers @hanyuancheung
instrumentation/github.com/go-kit/kit/otelkit/ @open-telemetry/go-approvers
instrumentation/github.com/gocql/gocql/otelgocql/ @open-telemetry/go-approvers
instrumentation/github.com/gorilla/mux/otelmux/ @open-telemetry/go-approvers
instrumentation/github.com/labstack/echo/otelecho/ @open-telemetry/go-approvers
instrumentation/github.com/Shopify/sarama/otelsarama/ @open-telemetry/go-approvers @pellared @hanyuancheung
instrumentation/github.com/Shopify/sarama/otelsarama/ @open-telemetry/go-approvers @pellared @hanyuancheung
instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo/ @open-telemetry/go-approvers
instrumentation/google.golang.org/grpc/otelgrpc/ @open-telemetry/go-approvers @dashpole @hanyuancheung
instrumentation/google.golang.org/grpc/otelgrpc/ @open-telemetry/go-approvers @dashpole @hanyuancheung
instrumentation/gopkg.in/macaron.v1/otelmacaron/ @open-telemetry/go-approvers

instrumentation/host/ @open-telemetry/go-approvers @MadVikingGod
Expand Down
22 changes: 22 additions & 0 deletions exporters/autoexport/README.md
@@ -0,0 +1,22 @@
# Automatic Exporter configuration
MikeGoldsmith marked this conversation as resolved.
Show resolved Hide resolved

[![Go Reference][goref-image]][goref-url]
[![Apache License][license-image]][license-url]

This module provides easy access to configuring a trace exporter that can be used when configuring an OpenTelemetry Go SDK trace export pipeline.

## Useful links

- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
- For more about OpenTelemetry Go: <https://github.com/open-telemetry/opentelemetry-go>
- For help or feedback on this project, join us in [GitHub Discussions][discussions-url]

## License

Apache 2.0 - See [LICENSE][license-url] for more information.

[license-url]: https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/LICENSE
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
[goref-image]: https://pkg.go.dev/badge/go.opentelemetry.io/contrib/exporters/autoexport.svg
[goref-url]: https://pkg.go.dev/go.opentelemetry.io/contrib/exporters/autoexport
[discussions-url]: https://github.com/open-telemetry/opentelemetry-go/discussions
33 changes: 33 additions & 0 deletions exporters/autoexport/doc.go
@@ -0,0 +1,33 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This module provides easy access to configuring a trace exporter
// that can be used when configuring an OpenTelemetry Go SDK trace export
// pipeline.
//
// [NewSpanExporter] looks for the `OTEL_TRACES_EXPORTER` environment
// variable and if set, attempts to load the exporter from it's registry of
// exporters. The registry is always loaded with an OTLP exporter with the key
// `otlp` and additional exporters can be registered using
// [RegisterSpanExporter].
// Exporter registration uses a factory method pattern to not unneccarily build
// exporters and use resources until they are requested.
//
// If the environment variable is not set, the fallback exporter is returned.
// The fallback exporter defaults to an
// [OTLP exporter](https://pkg.go.dev/go.opentelemetry.io/otel/exporters/otlp/otlptrace)
// and can be overridden using the [RegisterSpanExporter](https://pkg.go.dev/go.opentelemetry.io/contrib/exporters/autoexport#WithFallbackSpanExporter)
// option.

MikeGoldsmith marked this conversation as resolved.
Show resolved Hide resolved
package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport"
98 changes: 98 additions & 0 deletions exporters/autoexport/exporter.go
@@ -0,0 +1,98 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport"
pellared marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"os"

"go.opentelemetry.io/otel/sdk/trace"
)

const (
otelTracesExportersEnvKey = "OTEL_TRACES_EXPORTER"
)

type config struct {
fallbackExporter trace.SpanExporter
}

func newConfig(ctx context.Context, opts ...Option) (config, error) {
cfg := config{}
for _, opt := range opts {
cfg = opt.apply(cfg)
}

// if no fallback exporter is configured, use otlp exporter
if cfg.fallbackExporter == nil {
exp, err := spanExporter(context.Background(), "otlp")
if err != nil {
return cfg, err
}

Check warning on line 43 in exporters/autoexport/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporters/autoexport/exporter.go#L42-L43

Added lines #L42 - L43 were not covered by tests
cfg.fallbackExporter = exp
}
return cfg, nil
}

// Option applies an autoexport configuration option.
type Option interface {
apply(config) config
}

type optionFunc func(config) config

func (fn optionFunc) apply(cfg config) config {
return fn(cfg)
}

// WithFallbackSpanExporter sets the fallback exporter to use when no exporter
// is configured through the OTEL_TRACES_EXPORTER environment variable.
func WithFallbackSpanExporter(exporter trace.SpanExporter) Option {
return optionFunc(func(cfg config) config {
cfg.fallbackExporter = exporter
return cfg
})
}

// NewSpanExporter returns a configured SpanExporter defined using the environment
// variable OTEL_TRACES_EXPORTER, the configured fallback exporter via options or
// a default OTLP exporter (in this order).
func NewSpanExporter(ctx context.Context, opts ...Option) (trace.SpanExporter, error) {
// prefer exporter configured via environment variables over exporter
// passed in via exporter parameter
envExporter, err := makeExporterFromEnv(ctx)
if err != nil {
return nil, err
}

Check warning on line 78 in exporters/autoexport/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporters/autoexport/exporter.go#L77-L78

Added lines #L77 - L78 were not covered by tests
if envExporter != nil {
return envExporter, nil
}
config, err := newConfig(ctx, opts...)
if err != nil {
return nil, err
}

Check warning on line 85 in exporters/autoexport/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporters/autoexport/exporter.go#L84-L85

Added lines #L84 - L85 were not covered by tests
return config.fallbackExporter, nil
}

// makeExporterFromEnv returns a configured SpanExporter defined by the OTEL_TRACES_EXPORTER
// environment variable.
// nil is returned if no exporter is defined for the environment variable.
func makeExporterFromEnv(ctx context.Context) (trace.SpanExporter, error) {
expType, defined := os.LookupEnv(otelTracesExportersEnvKey)
if !defined {
return nil, nil
}
return spanExporter(ctx, expType)
}
64 changes: 64 additions & 0 deletions exporters/autoexport/exporter_test.go
@@ -0,0 +1,64 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package autoexport

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/sdk/trace"
)

func TestOTLPExporterReturnedWhenNoEnvOrFallbackExporterConfigured(t *testing.T) {
exporter, err := NewSpanExporter(context.Background())
assert.Nil(t, err)
assert.NotNil(t, exporter)
assert.IsType(t, &otlptrace.Exporter{}, exporter)
}

func TestFallbackExporterReturnedWhenNoEnvExporterConfigured(t *testing.T) {
testExporter := &testExporter{}
exporter, err := NewSpanExporter(
context.Background(),
WithFallbackSpanExporter(testExporter),
)
assert.Nil(t, err)
assert.Equal(t, testExporter, exporter)
}

func TestEnvExporterIsPreferredOverFallbackExporter(t *testing.T) {
t.Setenv("OTEL_TRACES_EXPORTER", "otlp")

testExporter := &testExporter{}
exporter, err := NewSpanExporter(
context.Background(),
WithFallbackSpanExporter(testExporter),
)
assert.Nil(t, err)
assert.IsType(t, &otlptrace.Exporter{}, exporter)
}

type testExporter struct{}

func (e *testExporter) ExportSpans(ctx context.Context, ss []trace.ReadOnlySpan) error {
return nil
}

func (e *testExporter) Shutdown(ctx context.Context) error {
return nil
}
33 changes: 33 additions & 0 deletions exporters/autoexport/go.mod
@@ -0,0 +1,33 @@
module go.opentelemetry.io/contrib/exporters/autoexport
MikeGoldsmith marked this conversation as resolved.
Show resolved Hide resolved

go 1.19

require (
github.com/stretchr/testify v1.7.1
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.10.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.10.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.10.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.10.0
go.opentelemetry.io/otel/sdk v1.10.0
)

require (
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel v1.10.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.10.0 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/text v0.3.5 // indirect
google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1 // indirect
google.golang.org/grpc v1.46.2 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)