From 51ee3a1d6c499650a2cddb80778e5088c2874847 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Thu, 26 Oct 2023 10:38:48 -0700 Subject: [PATCH] support console key in trace/metric exporter env var config 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 https://github.com/open-telemetry/opentelemetry-specification/pull/3742 Signed-off-by: Alex Boten --- CHANGELOG.md | 1 + exporters/autoexport/metrics.go | 9 +++++++++ exporters/autoexport/metrics_test.go | 9 +++++++++ exporters/autoexport/spans.go | 5 +++++ exporters/autoexport/spans_test.go | 8 ++++++++ 5 files changed, 32 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75798dfaa49..1306992c234 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/exporters/autoexport/metrics.go b/exporters/autoexport/metrics.go index f3d44d783d1..4d79a29b7a2 100644 --- a/exporters/autoexport/metrics.go +++ b/exporters/autoexport/metrics.go @@ -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" ) @@ -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: @@ -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 }) diff --git a/exporters/autoexport/metrics_test.go b/exporters/autoexport/metrics_test.go index 4a3ecff2f68..fa0ef1207a7 100644 --- a/exporters/autoexport/metrics_test.go +++ b/exporters/autoexport/metrics_test.go @@ -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") diff --git a/exporters/autoexport/spans.go b/exporters/autoexport/spans.go index e7de52e28a7..69edb3afa87 100644 --- a/exporters/autoexport/spans.go +++ b/exporters/autoexport/spans.go @@ -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" ) @@ -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: @@ -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 }) diff --git a/exporters/autoexport/spans_test.go b/exporters/autoexport/spans_test.go index 35ec975b301..ca568afaab4 100644 --- a/exporters/autoexport/spans_test.go +++ b/exporters/autoexport/spans_test.go @@ -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" ) @@ -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")