@@ -5,22 +5,39 @@ package autoexport // import "go.opentelemetry.io/contrib/exporters/autoexport"
5
5
6
6
import (
7
7
"context"
8
- "os "
8
+ "errors "
9
9
10
+ "go.opentelemetry.io/contrib/exporters/autoexport/utils/env"
11
+ "go.opentelemetry.io/contrib/exporters/autoexport/utils/functional"
10
12
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
11
13
"go.opentelemetry.io/otel/exporters/stdout/stdoutlog"
12
14
"go.opentelemetry.io/otel/sdk/log"
13
15
)
14
16
15
- // LogOption applies an autoexport configuration option.
16
- type LogOption = option [log.Exporter ]
17
+ const (
18
+ otelLogsExporterEnvKey = "OTEL_LOGS_EXPORTER"
19
+ otelLogsExporterProtocolEnvKey = "OTEL_EXPORTER_OTLP_LOGS_PROTOCOL"
20
+ )
17
21
18
- var logsSignal = newSignal [log.Exporter ]("OTEL_LOGS_EXPORTER" )
22
+ var (
23
+ logsSignal = newSignal [log.Exporter ](otelLogsExporterEnvKey )
19
24
20
- // NewLogExporter returns a configured [go.opentelemetry.io/otel/sdk/log.Exporter]
25
+ errLogsUnsupportedGRPCProtocol = errors .New ("log exporter do not support 'grpc' protocol yet - consider using 'http/protobuf' instead" )
26
+ )
27
+
28
+ // LogExporterOption applies an autoexport configuration option.
29
+ type LogExporterOption = functional.Option [config [log.Exporter ]]
30
+
31
+ // WithFallbackLogExporter sets the fallback exporter to use when no exporter
32
+ // is configured through the OTEL_LOGS_EXPORTER environment variable.
33
+ func WithFallbackLogExporter (factoryFn factory [log.Exporter ]) LogExporterOption {
34
+ return withFallbackFactory (factoryFn )
35
+ }
36
+
37
+ // NewLogExporters returns one or more configured [go.opentelemetry.io/otel/sdk/log.Exporter]
21
38
// defined using the environment variables described below.
22
39
//
23
- // OTEL_LOGS_EXPORTER defines the logs exporter; supported values:
40
+ // OTEL_LOGS_EXPORTER defines the logs exporter; this value accepts a comma-separated list of values to enable multiple exporters; supported values:
24
41
// - "none" - "no operation" exporter
25
42
// - "otlp" (default) - OTLP exporter; see [go.opentelemetry.io/otel/exporters/otlp/otlplog]
26
43
// - "console" - Standard output exporter; see [go.opentelemetry.io/otel/exporters/stdout/stdoutlog]
@@ -31,45 +48,80 @@ var logsSignal = newSignal[log.Exporter]("OTEL_LOGS_EXPORTER")
31
48
// see: [go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp]
32
49
//
33
50
// An error is returned if an environment value is set to an unhandled value.
51
+ // Use [WithFallbackLogExporter] option to change the returned exporter
52
+ // when OTEL_LOGS_EXPORTER is unset or empty.
34
53
//
35
54
// Use [RegisterLogExporter] to handle more values of OTEL_LOGS_EXPORTER.
36
55
//
56
+ // Use [IsNoneLogExporter] to check if the returned exporter is a "no operation" exporter.
57
+ func NewLogExporters (ctx context.Context , options ... LogExporterOption ) ([]log.Exporter , error ) {
58
+ return logsSignal .create (ctx , options ... )
59
+ }
60
+
61
+ // NewLogExporter returns a configured [go.opentelemetry.io/otel/sdk/log.Exporter]
62
+ // defined using the environment variables described below.
63
+ //
64
+ // DEPRECATED: consider using [NewLogExporters] instead.
65
+ //
66
+ // OTEL_LOGS_EXPORTER defines the logs exporter; supported values:
67
+ // - "none" - "no operation" exporter
68
+ // - "otlp" (default) - OTLP exporter; see [go.opentelemetry.io/otel/exporters/otlp/otlplog]
69
+ // - "console" - Standard output exporter; see [go.opentelemetry.io/otel/exporters/stdout/stdoutlog]
70
+ //
71
+ // OTEL_EXPORTER_OTLP_PROTOCOL defines OTLP exporter's transport protocol;
72
+ // supported values:
73
+ // - "http/protobuf" (default) - protobuf-encoded data over HTTP connection;
74
+ // see: [go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp]
75
+ //
76
+ // An error is returned if an environment value is set to an unhandled value.
37
77
// Use [WithFallbackLogExporter] option to change the returned exporter
38
78
// when OTEL_LOGS_EXPORTER is unset or empty.
39
79
//
80
+ // Use [RegisterLogExporter] to handle more values of OTEL_LOGS_EXPORTER.
81
+ //
40
82
// 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 ... )
83
+ func NewLogExporter (ctx context.Context , options ... LogExporterOption ) (log.Exporter , error ) {
84
+ exporters , err := NewLogExporters (ctx , options ... )
85
+ if err != nil {
86
+ return nil , err
87
+ }
88
+ return exporters [0 ], nil
43
89
}
44
90
45
91
// RegisterLogExporter sets the log.Exporter factory to be used when the
46
92
// OTEL_LOGS_EXPORTER environment variable contains the exporter name.
47
93
// 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 ))
94
+ func RegisterLogExporter (name string , factoryFn factory [ log.Exporter ] ) {
95
+ must (logsSignal .registry .store (name , factoryFn ))
50
96
}
51
97
52
98
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
- }
99
+ RegisterLogExporter (otlp , func (ctx context.Context ) (log.Exporter , error ) {
100
+ // The transport protocol used by the exporter is determined using the
101
+ // following environment variables, ordered by priority:
102
+ // - OTEL_EXPORTER_OTLP_LOGS_PROTOCOL
103
+ // - OTEL_EXPORTER_OTLP_PROTOCOL
104
+ // - fallback to 'http/protobuf' if variables above are not set or empty.
105
+ proto := env .WithDefaultString (
106
+ otelLogsExporterProtocolEnvKey ,
107
+ env .WithDefaultString (otelExporterOTLPProtoEnvKey , httpProtobuf ),
108
+ )
58
109
59
110
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" :
111
+ case grpc :
112
+ // grpc is not supported yet, should uncomment when it is supported.
113
+ // return otlplogrpc.New(ctx)
114
+ return nil , errLogsUnsupportedGRPCProtocol
115
+ case httpProtobuf :
64
116
return otlploghttp .New (ctx )
65
117
default :
66
118
return nil , errInvalidOTLPProtocol
67
119
}
68
120
})
69
- RegisterLogExporter (" console" , func (ctx context.Context ) (log.Exporter , error ) {
121
+ RegisterLogExporter (console , func (_ context.Context ) (log.Exporter , error ) {
70
122
return stdoutlog .New ()
71
123
})
72
- RegisterLogExporter (" none" , func (ctx context.Context ) (log.Exporter , error ) {
124
+ RegisterLogExporter (none , func (_ context.Context ) (log.Exporter , error ) {
73
125
return noopLogExporter {}, nil
74
126
})
75
127
}
0 commit comments