Skip to content

Commit

Permalink
support console key in trace/metric exporter env var config
Browse files Browse the repository at this point in the history
This adds support for the standard output exporter for traces and metrics. To remain consistent with other implementations, i used the key `console` as the identifier for the exporter. This change is related to the specification pull request open-telemetry/opentelemetry-specification#3742

Signed-off-by: Alex Boten <[email protected]>
  • Loading branch information
Alex Boten committed Oct 26, 2023
1 parent 5adc271 commit 51ee3a1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add `"go.opentelemetry.io/contrib/config"` package that includes configuration models generated via go-jsonschema. (#4376)
- Add `NewSDK` function to `"go.opentelemetry.io/contrib/config"`. The initial implementation only returns noop providers. (#4414)
- Add metrics support to `go.opentelemetry.io/contrib/exporters/autoexport`. (#4229)
- Add support for standard output exporter via environment variables for the metric and trace signals identified by the key: `console`. ()

### Changed

Expand Down
9 changes: 9 additions & 0 deletions exporters/autoexport/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
"go.opentelemetry.io/otel/sdk/metric"
)

Expand All @@ -38,6 +39,7 @@ func WithFallbackMetricReader(exporter metric.Reader) MetricOption {
// OTEL_METRICS_EXPORTER defines the metrics exporter; supported values:
// - "none" - "no operation" exporter
// - "otlp" (default) - OTLP exporter; see [go.opentelemetry.io/otel/exporters/otlp/otlpmetric]
// - "console" - Standard output exporter; see [https://pkg.go.dev/go.opentelemetry.io/otel/exporters/stdout/stdoutmetric]
//
// OTEL_EXPORTER_OTLP_PROTOCOL defines OTLP exporter's transport protocol;
// supported values:
Expand Down Expand Up @@ -91,6 +93,13 @@ func init() {
return nil, errInvalidOTLPProtocol
}
})
RegisterMetricReader("console", func(ctx context.Context) (metric.Reader, error) {
r, err := stdoutmetric.New()
if err != nil {
return nil, err
}
return metric.NewPeriodicReader(r), nil
})
RegisterMetricReader("none", func(ctx context.Context) (metric.Reader, error) {
return newNoopMetricReader(), nil
})
Expand Down
9 changes: 9 additions & 0 deletions exporters/autoexport/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ func TestMetricExporterNone(t *testing.T) {
assert.True(t, IsNoneMetricReader(got))
}

func TestMetricExporterConsole(t *testing.T) {
t.Setenv("OTEL_METRICS_EXPORTER", "console")
got, err := NewMetricReader(context.Background())
assert.NoError(t, err)
assert.IsType(t, &metric.PeriodicReader{}, got)
exporterType := reflect.Indirect(reflect.ValueOf(got)).FieldByName("exporter").Elem().Type()
assert.Equal(t, "*stdoutmetric.exporter", exporterType.String())
}

func TestMetricExporterOTLP(t *testing.T) {
t.Setenv("OTEL_METRICS_EXPORTER", "otlp")

Expand Down
5 changes: 5 additions & 0 deletions exporters/autoexport/spans.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

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

Expand All @@ -43,6 +44,7 @@ func WithFallbackSpanExporter(exporter trace.SpanExporter) SpanOption {
// OTEL_TRACES_EXPORTER defines the traces exporter; supported values:
// - "none" - "no operation" exporter
// - "otlp" (default) - OTLP exporter; see [go.opentelemetry.io/otel/exporters/otlp/otlptrace]
// - "console" - Standard output exporter; see [https://pkg.go.dev/go.opentelemetry.io/otel/exporters/stdout/stdouttrace]
//
// OTEL_EXPORTER_OTLP_PROTOCOL defines OTLP exporter's transport protocol;
// supported values:
Expand Down Expand Up @@ -88,6 +90,9 @@ func init() {
return nil, errInvalidOTLPProtocol
}
})
RegisterSpanExporter("console", func(ctx context.Context) (trace.SpanExporter, error) {
return stdouttrace.New()
})
RegisterSpanExporter("none", func(ctx context.Context) (trace.SpanExporter, error) {
return noopSpanExporter{}, nil
})
Expand Down
8 changes: 8 additions & 0 deletions exporters/autoexport/spans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"

"github.com/stretchr/testify/assert"
)
Expand All @@ -32,6 +33,13 @@ func TestSpanExporterNone(t *testing.T) {
assert.True(t, IsNoneSpanExporter(got))
}

func TestSpanExporterConsole(t *testing.T) {
t.Setenv("OTEL_TRACES_EXPORTER", "console")
got, err := NewSpanExporter(context.Background())
assert.NoError(t, err)
assert.IsType(t, &stdouttrace.Exporter{}, got)
}

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

Expand Down

0 comments on commit 51ee3a1

Please sign in to comment.