From 0eca1c9694770f7c18d904d3d3387202d82b13f5 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Thu, 26 Oct 2023 10:38:48 -0700 Subject: [PATCH 1/5] 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/go.mod | 2 ++ exporters/autoexport/go.sum | 4 ++++ exporters/autoexport/metrics.go | 9 +++++++++ exporters/autoexport/metrics_test.go | 9 +++++++++ exporters/autoexport/spans.go | 5 +++++ exporters/autoexport/spans_test.go | 8 ++++++++ 7 files changed, 38 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cff94fe55d7..a17f343b62c 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 (No-op, OTLP and Prometheus) to `go.opentelemetry.io/contrib/exporters/autoexport`. (#4229, #4479) +- 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/go.mod b/exporters/autoexport/go.mod index e2a8b562877..10a6c8bd6c8 100644 --- a/exporters/autoexport/go.mod +++ b/exporters/autoexport/go.mod @@ -12,6 +12,8 @@ require ( go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0 go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 go.opentelemetry.io/otel/exporters/prometheus v0.42.0 + go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.42.0 + go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0 go.opentelemetry.io/otel/sdk v1.19.0 go.opentelemetry.io/otel/sdk/metric v1.19.0 go.uber.org/goleak v1.2.1 diff --git a/exporters/autoexport/go.sum b/exporters/autoexport/go.sum index 47b02887646..0431813cc1e 100644 --- a/exporters/autoexport/go.sum +++ b/exporters/autoexport/go.sum @@ -53,6 +53,10 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 h1:IeMey go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0/go.mod h1:oVdCUtjq9MK9BlS7TtucsQwUcXcymNiEDjgDD2jMtZU= go.opentelemetry.io/otel/exporters/prometheus v0.42.0 h1:jwV9iQdvp38fxXi8ZC+lNpxjK16MRcZlpDYvbuO1FiA= go.opentelemetry.io/otel/exporters/prometheus v0.42.0/go.mod h1:f3bYiqNqhoPxkvI2LrXqQVC546K7BuRDL/kKuxkujhA= +go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.42.0 h1:4jJuoeOo9W6hZnz+r046fyoH5kykZPRvKfUXJVfMpB0= +go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.42.0/go.mod h1:/MtYTE1SfC2QIcE0bDot6fIX+h+WvXjgTqgn9P0LNPE= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0 h1:Nw7Dv4lwvGrI68+wULbcq7su9K2cebeCUrDjVrUJHxM= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0/go.mod h1:1MsF6Y7gTqosgoZvHlzcaaM8DIMNZgJh87ykokoNH7Y= 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= diff --git a/exporters/autoexport/metrics.go b/exporters/autoexport/metrics.go index d8f260cadd8..d7f4e2f527e 100644 --- a/exporters/autoexport/metrics.go +++ b/exporters/autoexport/metrics.go @@ -30,6 +30,7 @@ import ( "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" promexporter "go.opentelemetry.io/otel/exporters/prometheus" + "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" "go.opentelemetry.io/otel/sdk/metric" ) @@ -49,6 +50,7 @@ func WithFallbackMetricReader(exporter metric.Reader) MetricOption { // - "none" - "no operation" exporter // - "otlp" (default) - OTLP exporter; see [go.opentelemetry.io/otel/exporters/otlp/otlpmetric] // - "prometheus" - Prometheus exporter + HTTP server; see [go.opentelemetry.io/otel/exporters/prometheus] +// - "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: @@ -106,6 +108,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 6624db36ecd..8972c45abbd 100644 --- a/exporters/autoexport/metrics_test.go +++ b/exporters/autoexport/metrics_test.go @@ -40,6 +40,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 b2851549295..1b4de67786a 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" ) @@ -35,6 +36,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") From a915a984c09b18ec38cd3f238d06c46a48e634d2 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Thu, 26 Oct 2023 10:41:39 -0700 Subject: [PATCH 2/5] update changelog Signed-off-by: Alex Boten --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a17f343b62c..593a76475e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +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 (No-op, OTLP and Prometheus) to `go.opentelemetry.io/contrib/exporters/autoexport`. (#4229, #4479) -- Add support for standard output exporter via environment variables for the metric and trace signals identified by the key: `console`. () +- Add support for standard output exporter via environment variables for the metric and trace signals identified by the key: `console`. (#4486) ### Changed From df4f2539cea91ef390b24f4d7b6f356f2f020249 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Mon, 30 Oct 2023 07:48:09 -0700 Subject: [PATCH 3/5] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Robert PajÄ…k --- exporters/autoexport/metrics.go | 2 +- exporters/autoexport/spans.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/exporters/autoexport/metrics.go b/exporters/autoexport/metrics.go index d7f4e2f527e..1cfbbbc7884 100644 --- a/exporters/autoexport/metrics.go +++ b/exporters/autoexport/metrics.go @@ -50,7 +50,7 @@ func WithFallbackMetricReader(exporter metric.Reader) MetricOption { // - "none" - "no operation" exporter // - "otlp" (default) - OTLP exporter; see [go.opentelemetry.io/otel/exporters/otlp/otlpmetric] // - "prometheus" - Prometheus exporter + HTTP server; see [go.opentelemetry.io/otel/exporters/prometheus] -// - "console" - Standard output exporter; see [https://pkg.go.dev/go.opentelemetry.io/otel/exporters/stdout/stdoutmetric] +// - "console" - Standard output exporter; see [go.opentelemetry.io/otel/exporters/stdout/stdoutmetric] // // OTEL_EXPORTER_OTLP_PROTOCOL defines OTLP exporter's transport protocol; // supported values: diff --git a/exporters/autoexport/spans.go b/exporters/autoexport/spans.go index 69edb3afa87..af8c1bc3010 100644 --- a/exporters/autoexport/spans.go +++ b/exporters/autoexport/spans.go @@ -44,7 +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] +// - "console" - Standard output exporter; see [go.opentelemetry.io/otel/exporters/stdout/stdouttrace] // // OTEL_EXPORTER_OTLP_PROTOCOL defines OTLP exporter's transport protocol; // supported values: From 7e05cf518c22ccdce46df2fa3e9cdea54f57e73f Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Wed, 1 Nov 2023 11:50:21 -0700 Subject: [PATCH 4/5] add cleanup to test Signed-off-by: Alex Boten --- exporters/autoexport/metrics_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/exporters/autoexport/metrics_test.go b/exporters/autoexport/metrics_test.go index 8972c45abbd..81180b2f08c 100644 --- a/exporters/autoexport/metrics_test.go +++ b/exporters/autoexport/metrics_test.go @@ -44,6 +44,9 @@ func TestMetricExporterConsole(t *testing.T) { t.Setenv("OTEL_METRICS_EXPORTER", "console") got, err := NewMetricReader(context.Background()) assert.NoError(t, err) + t.Cleanup(func() { + assert.NoError(t, got.Shutdown(context.Background())) + }) assert.IsType(t, &metric.PeriodicReader{}, got) exporterType := reflect.Indirect(reflect.ValueOf(got)).FieldByName("exporter").Elem().Type() assert.Equal(t, "*stdoutmetric.exporter", exporterType.String()) From 00570c2f0792ec05f2b02809b7e1ac82029ba7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Mon, 6 Nov 2023 08:02:43 +0100 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fdd325fe3b..bdc319aaf64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +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 (No-op, OTLP and Prometheus) to `go.opentelemetry.io/contrib/exporters/autoexport`. (#4229, #4479) -- Add support for standard output exporter via environment variables for the metric and trace signals identified by the key: `console`. (#4486) +- Add support for `console` span exporter and metrics exporter in `go.opentelemetry.io/contrib/exporters/autoexport`. (#4486) - Set unit and description on all instruments in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#4500) ### Changed