Skip to content

Commit b368fc0

Browse files
sysulqMrAlias
andauthored
feat: add log support for autoexport (#5733)
Resolve #5730 --------- Co-authored-by: Tyler Yahn <[email protected]>
1 parent 4319808 commit b368fc0

File tree

6 files changed

+185
-0
lines changed

6 files changed

+185
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1212

1313
- The `go.opentelemetry.io/contrib/config` add support to configure periodic reader interval and timeout. (#5661)
1414
- Add support to configure views when creating MeterProvider using the config package. (#5654)
15+
- Add log support for the autoexport package. (#5733)
1516

1617
### Fixed
1718

exporters/autoexport/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ require (
88
go.opentelemetry.io/collector/pdata v1.8.0
99
go.opentelemetry.io/contrib/bridges/prometheus v0.52.0
1010
go.opentelemetry.io/otel v1.27.0
11+
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.3.0
1112
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.27.0
1213
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.27.0
1314
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.27.0
1415
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.27.0
1516
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.27.0
1617
go.opentelemetry.io/otel/exporters/prometheus v0.49.0
18+
go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.3.0
1719
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.27.0
1820
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.27.0
1921
go.opentelemetry.io/otel/sdk v1.27.0
22+
go.opentelemetry.io/otel/sdk/log v0.3.0
2023
go.opentelemetry.io/otel/sdk/metric v1.27.0
2124
go.uber.org/goleak v1.3.0
2225
)
@@ -37,6 +40,7 @@ require (
3740
github.com/prometheus/client_model v0.6.1 // indirect
3841
github.com/prometheus/common v0.54.0 // indirect
3942
github.com/prometheus/procfs v0.15.1 // indirect
43+
go.opentelemetry.io/otel/log v0.3.0 // indirect
4044
go.opentelemetry.io/otel/metric v1.27.0 // indirect
4145
go.opentelemetry.io/otel/trace v1.27.0 // indirect
4246
go.opentelemetry.io/proto/otlp v1.2.0 // indirect

exporters/autoexport/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ go.opentelemetry.io/collector/pdata v1.8.0 h1:d/QQgZxB4Y+d3mqLVh2ozvzujUhloD3P/f
5454
go.opentelemetry.io/collector/pdata v1.8.0/go.mod h1:/W7clu0wFC4WSRp94Ucn6Vm36Wkrt+tmtlDb1aiNZCY=
5555
go.opentelemetry.io/otel v1.27.0 h1:9BZoF3yMK/O1AafMiQTVu0YDj5Ea4hPhxCs7sGva+cg=
5656
go.opentelemetry.io/otel v1.27.0/go.mod h1:DMpAK8fzYRzs+bi3rS5REupisuqTheUlSZJ1WnZaPAQ=
57+
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.3.0 h1:ccBrA8nCY5mM0y5uO7FT0ze4S0TuFcWdDB2FxGMTjkI=
58+
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.3.0/go.mod h1:/9pb6634zi2Lk8LYg9Q0X8Ar6jka4dkFOylBLbVQPCE=
5759
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.27.0 h1:bFgvUr3/O4PHj3VQcFEuYKvRZJX1SJDQ+11JXuSB3/w=
5860
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.27.0/go.mod h1:xJntEd2KL6Qdg5lwp97HMLQDVeAhrYxmzFseAMDPQ8I=
5961
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.27.0 h1:CIHWikMsN3wO+wq1Tp5VGdVRTcON+DmOJSfDjXypKOc=
@@ -66,14 +68,20 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.27.0 h1:QY7/0
6668
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.27.0/go.mod h1:HVkSiDhTM9BoUJU8qE6j2eSWLLXvi1USXjyd2BXT8PY=
6769
go.opentelemetry.io/otel/exporters/prometheus v0.49.0 h1:Er5I1g/YhfYv9Affk9nJLfH/+qCCVVg1f2R9AbJfqDQ=
6870
go.opentelemetry.io/otel/exporters/prometheus v0.49.0/go.mod h1:KfQ1wpjf3zsHjzP149P4LyAwWRupc6c7t1ZJ9eXpKQM=
71+
go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.3.0 h1:6aGq6rMOdOx9B385JpF1OpeL18+6Ho8bTFdxy10oEGY=
72+
go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.3.0/go.mod h1:fdZI+pB2Y6Dpl3Uf+1ZPrkX6cnwsUAhjK1f9yCAlJIM=
6973
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.27.0 h1:/jlt1Y8gXWiHG9FBx6cJaIC5hYx5Fe64nC8w5Cylt/0=
7074
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.27.0/go.mod h1:bmToOGOBZ4hA9ghphIc1PAf66VA8KOtsuy3+ScStG20=
7175
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.27.0 h1:/0YaXu3755A/cFbtXp+21lkXgI0QE5avTWA2HjU9/WE=
7276
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.27.0/go.mod h1:m7SFxp0/7IxmJPLIY3JhOcU9CoFzDaCPL6xxQIxhA+o=
77+
go.opentelemetry.io/otel/log v0.3.0 h1:kJRFkpUFYtny37NQzL386WbznUByZx186DpEMKhEGZs=
78+
go.opentelemetry.io/otel/log v0.3.0/go.mod h1:ziCwqZr9soYDwGNbIL+6kAvQC+ANvjgG367HVcyR/ys=
7379
go.opentelemetry.io/otel/metric v1.27.0 h1:hvj3vdEKyeCi4YaYfNjv2NUje8FqKqUY8IlF0FxV/ik=
7480
go.opentelemetry.io/otel/metric v1.27.0/go.mod h1:mVFgmRlhljgBiuk/MP/oKylr4hs85GZAylncepAX/ak=
7581
go.opentelemetry.io/otel/sdk v1.27.0 h1:mlk+/Y1gLPLn84U4tI8d3GNJmGT/eXe3ZuOXN9kTWmI=
7682
go.opentelemetry.io/otel/sdk v1.27.0/go.mod h1:Ha9vbLwJE6W86YstIywK2xFfPjbWlCuwPtMkKdz/Y4A=
83+
go.opentelemetry.io/otel/sdk/log v0.3.0 h1:GEjJ8iftz2l+XO1GF2856r7yYVh74URiF9JMcAacr5U=
84+
go.opentelemetry.io/otel/sdk/log v0.3.0/go.mod h1:BwCxtmux6ACLuys1wlbc0+vGBd+xytjmjajwqqIul2g=
7785
go.opentelemetry.io/otel/sdk/metric v1.27.0 h1:5uGNOlpXi+Hbo/DRoI31BSb1v+OGcpv2NemcCrOL8gI=
7886
go.opentelemetry.io/otel/sdk/metric v1.27.0/go.mod h1:we7jJVrYN2kh3mVBlswtPU22K0SA+769l93J6bsyvqw=
7987
go.opentelemetry.io/otel/trace v1.27.0 h1:IqYb813p7cmbHk0a5y6pD5JPakbVfftRXABGt5/Rscw=

exporters/autoexport/logs.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport"
5+
6+
import (
7+
"context"
8+
"os"
9+
10+
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
11+
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog"
12+
"go.opentelemetry.io/otel/sdk/log"
13+
)
14+
15+
// LogOption applies an autoexport configuration option.
16+
type LogOption = option[log.Exporter]
17+
18+
var logsSignal = newSignal[log.Exporter]("OTEL_LOGS_EXPORTER")
19+
20+
// NewLogExporter returns a configured [go.opentelemetry.io/otel/sdk/log.Exporter]
21+
// defined using the environment variables described below.
22+
//
23+
// OTEL_LOGS_EXPORTER defines the logs exporter; supported values:
24+
// - "none" - "no operation" exporter
25+
// - "otlp" (default) - OTLP exporter; see [go.opentelemetry.io/otel/exporters/otlp/otlplog]
26+
// - "console" - Standard output exporter; see [go.opentelemetry.io/otel/exporters/stdout/stdoutlog]
27+
//
28+
// OTEL_EXPORTER_OTLP_PROTOCOL defines OTLP exporter's transport protocol;
29+
// supported values:
30+
// - "http/protobuf" (default) - protobuf-encoded data over HTTP connection;
31+
// see: [go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp]
32+
//
33+
// An error is returned if an environment value is set to an unhandled value.
34+
//
35+
// Use [RegisterLogExporter] to handle more values of OTEL_LOGS_EXPORTER.
36+
//
37+
// Use [WithFallbackLogExporter] option to change the returned exporter
38+
// when OTEL_LOGS_EXPORTER is unset or empty.
39+
//
40+
// Use [IsNoneLogExporter] to check if the returned exporter is a "no operation" exporter.
41+
func NewLogExporter(ctx context.Context, opts ...LogOption) (log.Exporter, error) {
42+
return logsSignal.create(ctx, opts...)
43+
}
44+
45+
// RegisterLogExporter sets the log.Exporter factory to be used when the
46+
// OTEL_LOGS_EXPORTER environment variable contains the exporter name.
47+
// This will panic if name has already been registered.
48+
func RegisterLogExporter(name string, factory func(context.Context) (log.Exporter, error)) {
49+
must(logsSignal.registry.store(name, factory))
50+
}
51+
52+
func init() {
53+
RegisterLogExporter("otlp", func(ctx context.Context) (log.Exporter, error) {
54+
proto := os.Getenv(otelExporterOTLPProtoEnvKey)
55+
if proto == "" {
56+
proto = "http/protobuf"
57+
}
58+
59+
switch proto {
60+
// grpc is not supported yet, should comment out when it is supported
61+
// case "grpc":
62+
// return otlploggrpc.New(ctx)
63+
case "http/protobuf":
64+
return otlploghttp.New(ctx)
65+
default:
66+
return nil, errInvalidOTLPProtocol
67+
}
68+
})
69+
RegisterLogExporter("console", func(ctx context.Context) (log.Exporter, error) {
70+
return stdoutlog.New()
71+
})
72+
RegisterLogExporter("none", func(ctx context.Context) (log.Exporter, error) {
73+
return noopLogExporter{}, nil
74+
})
75+
}

exporters/autoexport/logs_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport"
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"reflect"
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
14+
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog"
15+
"go.opentelemetry.io/otel/sdk/log"
16+
)
17+
18+
func TestLogExporterNone(t *testing.T) {
19+
t.Setenv("OTEL_LOGS_EXPORTER", "none")
20+
got, err := NewLogExporter(context.Background())
21+
assert.NoError(t, err)
22+
t.Cleanup(func() {
23+
assert.NoError(t, got.ForceFlush(context.Background()))
24+
assert.NoError(t, got.Shutdown(context.Background()))
25+
})
26+
assert.NoError(t, got.Export(context.Background(), nil))
27+
assert.True(t, IsNoneLogExporter(got))
28+
}
29+
30+
func TestLogExporterConsole(t *testing.T) {
31+
t.Setenv("OTEL_LOGS_EXPORTER", "console")
32+
got, err := NewLogExporter(context.Background())
33+
assert.NoError(t, err)
34+
assert.IsType(t, &stdoutlog.Exporter{}, got)
35+
}
36+
37+
func TestLogExporterOTLP(t *testing.T) {
38+
t.Setenv("OTEL_LOGS_EXPORTER", "otlp")
39+
40+
for _, tc := range []struct {
41+
protocol, clientType string
42+
}{
43+
{"http/protobuf", "atomic.Pointer[go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp.client]"},
44+
{"", "atomic.Pointer[go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp.client]"},
45+
} {
46+
t.Run(fmt.Sprintf("protocol=%q", tc.protocol), func(t *testing.T) {
47+
t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", tc.protocol)
48+
49+
got, err := NewLogExporter(context.Background())
50+
assert.NoError(t, err)
51+
t.Cleanup(func() {
52+
assert.NoError(t, got.Shutdown(context.Background()))
53+
})
54+
assert.Implements(t, new(log.Exporter), got)
55+
56+
// Implementation detail hack. This may break when bumping OTLP exporter modules as it uses unexported API.
57+
clientType := reflect.Indirect(reflect.ValueOf(got)).FieldByName("client").Type()
58+
assert.Equal(t, tc.clientType, clientType.String())
59+
})
60+
}
61+
}
62+
63+
func TestLogExporterOTLPOverInvalidProtocol(t *testing.T) {
64+
t.Setenv("OTEL_LOGS_EXPORTER", "otlp")
65+
t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "invalid-protocol")
66+
67+
_, err := NewLogExporter(context.Background())
68+
assert.Error(t, err)
69+
}

exporters/autoexport/noop.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport"
66
import (
77
"context"
88

9+
"go.opentelemetry.io/otel/sdk/log"
910
"go.opentelemetry.io/otel/sdk/metric"
1011
"go.opentelemetry.io/otel/sdk/metric/metricdata"
1112
"go.opentelemetry.io/otel/sdk/trace"
@@ -57,3 +58,30 @@ func (e noopMetricProducer) Produce(ctx context.Context) ([]metricdata.ScopeMetr
5758
func newNoopMetricProducer() noopMetricProducer {
5859
return noopMetricProducer{}
5960
}
61+
62+
// noopLogExporter is an implementation of log.SpanExporter that performs no operations.
63+
type noopLogExporter struct{}
64+
65+
var _ log.Exporter = noopLogExporter{}
66+
67+
// ExportSpans is part of log.Exporter interface.
68+
func (e noopLogExporter) Export(ctx context.Context, records []log.Record) error {
69+
return nil
70+
}
71+
72+
// Shutdown is part of log.Exporter interface.
73+
func (e noopLogExporter) Shutdown(ctx context.Context) error {
74+
return nil
75+
}
76+
77+
// ForceFlush is part of log.Exporter interface.
78+
func (e noopLogExporter) ForceFlush(ctx context.Context) error {
79+
return nil
80+
}
81+
82+
// IsNoneLogExporter returns true for the exporter returned by [NewLogExporter]
83+
// when OTEL_LOGSS_EXPORTER environment variable is set to "none".
84+
func IsNoneLogExporter(e log.Exporter) bool {
85+
_, ok := e.(noopLogExporter)
86+
return ok
87+
}

0 commit comments

Comments
 (0)