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 1 commit
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
95 changes: 95 additions & 0 deletions exporters/autoexport/exporter.go
@@ -0,0 +1,95 @@
// 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"
"errors"
"fmt"
"os"
"strings"

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

const otelTracesExportersEnvKey = "OTEL_EXPORTERS"
MikeGoldsmith marked this conversation as resolved.
Show resolved Hide resolved

// errUnknownExpoter is returned when an unknown exporter name is used in
// the OTEL_EXPORTERS environment variable.
MikeGoldsmith marked this conversation as resolved.
Show resolved Hide resolved
var errUnknownExpoter = errors.New("unknown exporter")

func NewTraceExporters(exporters ...trace.SpanExporter) []trace.SpanExporter {
MikeGoldsmith marked this conversation as resolved.
Show resolved Hide resolved
// prefer exporters configured via environment variables over exporters
// passed in via exporters paramter
envExporters, err := parseEnv()
if err != nil {
otel.Handle(err)
}
if len(envExporters) > 0 {
return envExporters
}

return exporters
}

// parseEnv returns an array of SpanExporter's defined by the OTEL_EXPORTERS
// environment variable.
// A nil slice is returned if no exporters are defined for the environment variable.
func parseEnv() ([]trace.SpanExporter, error) {
expTypes, defined := os.LookupEnv(otelTracesExportersEnvKey)
if !defined {
return nil, nil
}

const sep = ","

var (
exporters []trace.SpanExporter
unknown []string
errors []error
)

for _, expType := range strings.Split(expTypes, sep) {
switch expType {
case "otlp":
// TODO: switch between otlp exporter protocol (grpc, http)
exp, err := otlptracegrpc.New(context.Background())
if err != nil {
errors = append(errors, err)
} else {
exporters = append(exporters, exp)
}
break
default:
exp, ok := envRegistry.load(expType)
if !ok {
unknown = append(unknown, expType)
continue
}
exporters = append(exporters, exp)
}
}

var err error
if len(unknown) > 0 {
joined := strings.Join(unknown, sep)
err = fmt.Errorf("%w: %s", errUnknownExpoter, joined)
}

// TODO: combine start errors with unknown exporter error
return exporters, err
}
41 changes: 41 additions & 0 deletions exporters/autoexport/exporter_test.go
@@ -0,0 +1,41 @@
// 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_test

import (
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/contrib/exporters/autoexport"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
)

func TestNoExportersAreConfiguredIfEnvNotSetAndNoExportersProvided(t *testing.T) {
exporters := autoexport.NewTraceExporters()
assert.Equal(t, 0, len(exporters))
}

func TestProvidedExportersAreUsedWhenEnvVarIsNotSet(t *testing.T) {
exporters := autoexport.NewTraceExporters(
otlptracegrpc.NewUnstarted(),
)
assert.Equal(t, 1, len(exporters))
}

func TestExportersConfiguredInEnvVarAreReturned(t *testing.T) {
t.Setenv("OTEL_TRACES_EXPORTERS", "otlp")
exporters := autoexport.NewTraceExporters()
assert.Equal(t, 1, len(exporters))
}
31 changes: 31 additions & 0 deletions exporters/autoexport/go.mod
@@ -0,0 +1,31 @@
module go.opentelemetry.io/contrib/exporters/autoexport
MikeGoldsmith marked this conversation as resolved.
Show resolved Hide resolved

go 1.17
MikeGoldsmith marked this conversation as resolved.
Show resolved Hide resolved

require (
github.com/stretchr/testify v1.7.1
go.opentelemetry.io/otel v1.10.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc 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/exporters/otlp/internal/retry v1.10.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace 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
)