Skip to content

Commit 59ee3a0

Browse files
authored
Merge branch 'main' into cirilla/fix_b3_propagator_panic
2 parents d235a93 + b18ecf5 commit 59ee3a0

File tree

48 files changed

+649
-98
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+649
-98
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
6969
- The gRPC trace `Filter` for interceptor is renamed to `InterceptorFilter`. (#5196)
7070
- The gRPC trace filter functions `Any`, `All`, `None`, `Not`, `MethodName`, `MethodPrefix`, `FullMethodName`, `ServiceName`, `ServicePrefix` and `HealthCheck` for interceptor are moved to `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/filters/interceptor`.
7171
With this change, the filters in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc` are now working for stats handler. (#5196)
72+
- `NewSDK` in `go.opentelemetry.io/contrib/config` now returns a configured SDK with a valid `LoggerProvider`. (#5427)
7273

7374
- `NewLogger` now accepts a `name` `string` as the first argument.
7475
This parameter is used as a replacement of `WithInstrumentationScope` to specify the name of the logger backing the underlying `Handler`. (#5588)

config/config.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"errors"
99

10+
"go.opentelemetry.io/otel/log"
1011
"go.opentelemetry.io/otel/metric"
1112
"go.opentelemetry.io/otel/trace"
1213
)
@@ -35,6 +36,7 @@ func noopShutdown(context.Context) error {
3536
type SDK struct {
3637
meterProvider metric.MeterProvider
3738
tracerProvider trace.TracerProvider
39+
loggerProvider log.LoggerProvider
3840
shutdown shutdownFunc
3941
}
4042

@@ -48,6 +50,11 @@ func (s *SDK) MeterProvider() metric.MeterProvider {
4850
return s.meterProvider
4951
}
5052

53+
// LoggerProvider returns a configured log.LoggerProvider.
54+
func (s *SDK) LoggerProvider() log.LoggerProvider {
55+
return s.loggerProvider
56+
}
57+
5158
// Shutdown calls shutdown on all configured providers.
5259
func (s *SDK) Shutdown(ctx context.Context) error {
5360
return s.shutdown(ctx)
@@ -77,12 +84,17 @@ func NewSDK(opts ...ConfigurationOption) (SDK, error) {
7784
return SDK{}, err
7885
}
7986

87+
lp, lpShutdown, err := loggerProvider(o, r)
88+
if err != nil {
89+
return SDK{}, err
90+
}
91+
8092
return SDK{
8193
meterProvider: mp,
8294
tracerProvider: tp,
95+
loggerProvider: lp,
8396
shutdown: func(ctx context.Context) error {
84-
err := mpShutdown(ctx)
85-
return errors.Join(err, tpShutdown(ctx))
97+
return errors.Join(mpShutdown(ctx), tpShutdown(ctx), lpShutdown(ctx))
8698
},
8799
}, nil
88100
}

config/config_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
"github.com/stretchr/testify/require"
1212

13+
lognoop "go.opentelemetry.io/otel/log/noop"
1314
metricnoop "go.opentelemetry.io/otel/metric/noop"
15+
sdklog "go.opentelemetry.io/otel/sdk/log"
1416
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
1517
sdktrace "go.opentelemetry.io/otel/sdk/trace"
1618
tracenoop "go.opentelemetry.io/otel/trace/noop"
@@ -22,13 +24,15 @@ func TestNewSDK(t *testing.T) {
2224
cfg []ConfigurationOption
2325
wantTracerProvider any
2426
wantMeterProvider any
27+
wantLoggerProvider any
2528
wantErr error
2629
wantShutdownErr error
2730
}{
2831
{
2932
name: "no-configuration",
3033
wantTracerProvider: tracenoop.NewTracerProvider(),
3134
wantMeterProvider: metricnoop.NewMeterProvider(),
35+
wantLoggerProvider: lognoop.NewLoggerProvider(),
3236
},
3337
{
3438
name: "with-configuration",
@@ -37,17 +41,20 @@ func TestNewSDK(t *testing.T) {
3741
WithOpenTelemetryConfiguration(OpenTelemetryConfiguration{
3842
TracerProvider: &TracerProvider{},
3943
MeterProvider: &MeterProvider{},
44+
LoggerProvider: &LoggerProvider{},
4045
}),
4146
},
4247
wantTracerProvider: &sdktrace.TracerProvider{},
4348
wantMeterProvider: &sdkmetric.MeterProvider{},
49+
wantLoggerProvider: &sdklog.LoggerProvider{},
4450
},
4551
}
4652
for _, tt := range tests {
4753
sdk, err := NewSDK(tt.cfg...)
4854
require.Equal(t, tt.wantErr, err)
4955
assert.IsType(t, tt.wantTracerProvider, sdk.TracerProvider())
5056
assert.IsType(t, tt.wantMeterProvider, sdk.MeterProvider())
57+
assert.IsType(t, tt.wantLoggerProvider, sdk.LoggerProvider())
5158
require.Equal(t, tt.wantShutdownErr, sdk.Shutdown(context.Background()))
5259
}
5360
}

config/go.mod

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ require (
66
github.com/prometheus/client_golang v1.19.1
77
github.com/stretchr/testify v1.9.0
88
go.opentelemetry.io/otel v1.27.0
9+
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.3.0
910
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.27.0
1011
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.27.0
1112
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.27.0
1213
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.27.0
1314
go.opentelemetry.io/otel/exporters/prometheus v0.49.0
1415
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.27.0
1516
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.27.0
17+
go.opentelemetry.io/otel/log v0.3.0
1618
go.opentelemetry.io/otel/metric v1.27.0
1719
go.opentelemetry.io/otel/sdk v1.27.0
20+
go.opentelemetry.io/otel/sdk/log v0.3.0
1821
go.opentelemetry.io/otel/sdk/metric v1.27.0
1922
go.opentelemetry.io/otel/trace v1.27.0
2023
)
@@ -32,12 +35,12 @@ require (
3235
github.com/prometheus/common v0.54.0 // indirect
3336
github.com/prometheus/procfs v0.15.1 // indirect
3437
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.27.0 // indirect
35-
go.opentelemetry.io/proto/otlp v1.2.0 // indirect
38+
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
3639
golang.org/x/net v0.26.0 // indirect
3740
golang.org/x/sys v0.21.0 // indirect
3841
golang.org/x/text v0.16.0 // indirect
39-
google.golang.org/genproto/googleapis/api v0.0.0-20240610135401-a8a62080eff3 // indirect
40-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240610135401-a8a62080eff3 // indirect
42+
google.golang.org/genproto/googleapis/api v0.0.0-20240617180043-68d350f18fd4 // indirect
43+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 // indirect
4144
google.golang.org/grpc v1.64.0 // indirect
4245
google.golang.org/protobuf v1.34.2 // indirect
4346
gopkg.in/yaml.v3 v3.0.1 // indirect

config/go.sum

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
3535
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3636
go.opentelemetry.io/otel v1.27.0 h1:9BZoF3yMK/O1AafMiQTVu0YDj5Ea4hPhxCs7sGva+cg=
3737
go.opentelemetry.io/otel v1.27.0/go.mod h1:DMpAK8fzYRzs+bi3rS5REupisuqTheUlSZJ1WnZaPAQ=
38+
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.3.0 h1:ccBrA8nCY5mM0y5uO7FT0ze4S0TuFcWdDB2FxGMTjkI=
39+
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.3.0/go.mod h1:/9pb6634zi2Lk8LYg9Q0X8Ar6jka4dkFOylBLbVQPCE=
3840
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.27.0 h1:bFgvUr3/O4PHj3VQcFEuYKvRZJX1SJDQ+11JXuSB3/w=
3941
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.27.0/go.mod h1:xJntEd2KL6Qdg5lwp97HMLQDVeAhrYxmzFseAMDPQ8I=
4042
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.27.0 h1:CIHWikMsN3wO+wq1Tp5VGdVRTcON+DmOJSfDjXypKOc=
@@ -51,16 +53,20 @@ go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.27.0 h1:/jlt1Y8gXWiHG9
5153
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.27.0/go.mod h1:bmToOGOBZ4hA9ghphIc1PAf66VA8KOtsuy3+ScStG20=
5254
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.27.0 h1:/0YaXu3755A/cFbtXp+21lkXgI0QE5avTWA2HjU9/WE=
5355
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.27.0/go.mod h1:m7SFxp0/7IxmJPLIY3JhOcU9CoFzDaCPL6xxQIxhA+o=
56+
go.opentelemetry.io/otel/log v0.3.0 h1:kJRFkpUFYtny37NQzL386WbznUByZx186DpEMKhEGZs=
57+
go.opentelemetry.io/otel/log v0.3.0/go.mod h1:ziCwqZr9soYDwGNbIL+6kAvQC+ANvjgG367HVcyR/ys=
5458
go.opentelemetry.io/otel/metric v1.27.0 h1:hvj3vdEKyeCi4YaYfNjv2NUje8FqKqUY8IlF0FxV/ik=
5559
go.opentelemetry.io/otel/metric v1.27.0/go.mod h1:mVFgmRlhljgBiuk/MP/oKylr4hs85GZAylncepAX/ak=
5660
go.opentelemetry.io/otel/sdk v1.27.0 h1:mlk+/Y1gLPLn84U4tI8d3GNJmGT/eXe3ZuOXN9kTWmI=
5761
go.opentelemetry.io/otel/sdk v1.27.0/go.mod h1:Ha9vbLwJE6W86YstIywK2xFfPjbWlCuwPtMkKdz/Y4A=
62+
go.opentelemetry.io/otel/sdk/log v0.3.0 h1:GEjJ8iftz2l+XO1GF2856r7yYVh74URiF9JMcAacr5U=
63+
go.opentelemetry.io/otel/sdk/log v0.3.0/go.mod h1:BwCxtmux6ACLuys1wlbc0+vGBd+xytjmjajwqqIul2g=
5864
go.opentelemetry.io/otel/sdk/metric v1.27.0 h1:5uGNOlpXi+Hbo/DRoI31BSb1v+OGcpv2NemcCrOL8gI=
5965
go.opentelemetry.io/otel/sdk/metric v1.27.0/go.mod h1:we7jJVrYN2kh3mVBlswtPU22K0SA+769l93J6bsyvqw=
6066
go.opentelemetry.io/otel/trace v1.27.0 h1:IqYb813p7cmbHk0a5y6pD5JPakbVfftRXABGt5/Rscw=
6167
go.opentelemetry.io/otel/trace v1.27.0/go.mod h1:6RiD1hkAprV4/q+yd2ln1HG9GoPx39SuvvstaLBl+l4=
62-
go.opentelemetry.io/proto/otlp v1.2.0 h1:pVeZGk7nXDC9O2hncA6nHldxEjm6LByfA2aN8IOkz94=
63-
go.opentelemetry.io/proto/otlp v1.2.0/go.mod h1:gGpR8txAl5M03pDhMC79G6SdqNV26naRm/KDsgaHD8A=
68+
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
69+
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
6470
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
6571
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
6672
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
@@ -69,10 +75,10 @@ golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
6975
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
7076
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
7177
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
72-
google.golang.org/genproto/googleapis/api v0.0.0-20240610135401-a8a62080eff3 h1:QW9+G6Fir4VcRXVH8x3LilNAb6cxBGLa6+GM4hRwexE=
73-
google.golang.org/genproto/googleapis/api v0.0.0-20240610135401-a8a62080eff3/go.mod h1:kdrSS/OiLkPrNUpzD4aHgCq2rVuC/YRxok32HXZ4vRE=
74-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240610135401-a8a62080eff3 h1:9Xyg6I9IWQZhRVfCWjKK+l6kI0jHcPesVlMnT//aHNo=
75-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240610135401-a8a62080eff3/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
78+
google.golang.org/genproto/googleapis/api v0.0.0-20240617180043-68d350f18fd4 h1:MuYw1wJzT+ZkybKfaOXKp5hJiZDn2iHaXRw0mRYdHSc=
79+
google.golang.org/genproto/googleapis/api v0.0.0-20240617180043-68d350f18fd4/go.mod h1:px9SlOOZBg1wM1zdnr8jEL4CNGUBZ+ZKYtNPApNQc4c=
80+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 h1:Di6ANFilr+S60a4S61ZM00vLdw0IrQOSMS2/6mrnOU0=
81+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
7682
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
7783
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
7884
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=

config/log.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package config // import "go.opentelemetry.io/contrib/config"
5+
6+
import (
7+
"context"
8+
"errors"
9+
"fmt"
10+
"net/url"
11+
"time"
12+
13+
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
14+
"go.opentelemetry.io/otel/log"
15+
"go.opentelemetry.io/otel/log/noop"
16+
sdklog "go.opentelemetry.io/otel/sdk/log"
17+
"go.opentelemetry.io/otel/sdk/resource"
18+
)
19+
20+
func loggerProvider(cfg configOptions, res *resource.Resource) (log.LoggerProvider, shutdownFunc, error) {
21+
if cfg.opentelemetryConfig.LoggerProvider == nil {
22+
return noop.NewLoggerProvider(), noopShutdown, nil
23+
}
24+
opts := []sdklog.LoggerProviderOption{
25+
sdklog.WithResource(res),
26+
}
27+
var errs []error
28+
for _, processor := range cfg.opentelemetryConfig.LoggerProvider.Processors {
29+
sp, err := logProcessor(cfg.ctx, processor)
30+
if err == nil {
31+
opts = append(opts, sdklog.WithProcessor(sp))
32+
} else {
33+
errs = append(errs, err)
34+
}
35+
}
36+
37+
if len(errs) > 0 {
38+
return noop.NewLoggerProvider(), noopShutdown, errors.Join(errs...)
39+
}
40+
41+
lp := sdklog.NewLoggerProvider(opts...)
42+
return lp, lp.Shutdown, nil
43+
}
44+
45+
func logProcessor(ctx context.Context, processor LogRecordProcessor) (sdklog.Processor, error) {
46+
if processor.Batch != nil && processor.Simple != nil {
47+
return nil, errors.New("must not specify multiple log processor type")
48+
}
49+
if processor.Batch != nil {
50+
exp, err := logExporter(ctx, processor.Batch.Exporter)
51+
if err != nil {
52+
return nil, err
53+
}
54+
return batchLogProcessor(processor.Batch, exp)
55+
}
56+
if processor.Simple != nil {
57+
exp, err := logExporter(ctx, processor.Simple.Exporter)
58+
if err != nil {
59+
return nil, err
60+
}
61+
return sdklog.NewSimpleProcessor(exp), nil
62+
}
63+
return nil, fmt.Errorf("unsupported log processor type, must be one of simple or batch")
64+
}
65+
66+
func logExporter(ctx context.Context, exporter LogRecordExporter) (sdklog.Exporter, error) {
67+
if exporter.OTLP != nil {
68+
switch exporter.OTLP.Protocol {
69+
case protocolProtobufHTTP:
70+
return otlpHTTPLogExporter(ctx, exporter.OTLP)
71+
default:
72+
return nil, fmt.Errorf("unsupported protocol %q", exporter.OTLP.Protocol)
73+
}
74+
}
75+
return nil, errors.New("no valid log exporter")
76+
}
77+
78+
func batchLogProcessor(blp *BatchLogRecordProcessor, exp sdklog.Exporter) (*sdklog.BatchProcessor, error) {
79+
var opts []sdklog.BatchProcessorOption
80+
if blp.ExportTimeout != nil {
81+
if *blp.ExportTimeout < 0 {
82+
return nil, fmt.Errorf("invalid export timeout %d", *blp.ExportTimeout)
83+
}
84+
opts = append(opts, sdklog.WithExportTimeout(time.Millisecond*time.Duration(*blp.ExportTimeout)))
85+
}
86+
if blp.MaxExportBatchSize != nil {
87+
if *blp.MaxExportBatchSize < 0 {
88+
return nil, fmt.Errorf("invalid batch size %d", *blp.MaxExportBatchSize)
89+
}
90+
opts = append(opts, sdklog.WithExportMaxBatchSize(*blp.MaxExportBatchSize))
91+
}
92+
if blp.MaxQueueSize != nil {
93+
if *blp.MaxQueueSize < 0 {
94+
return nil, fmt.Errorf("invalid queue size %d", *blp.MaxQueueSize)
95+
}
96+
opts = append(opts, sdklog.WithMaxQueueSize(*blp.MaxQueueSize))
97+
}
98+
99+
if blp.ScheduleDelay != nil {
100+
if *blp.ScheduleDelay < 0 {
101+
return nil, fmt.Errorf("invalid schedule delay %d", *blp.ScheduleDelay)
102+
}
103+
opts = append(opts, sdklog.WithExportInterval(time.Millisecond*time.Duration(*blp.ScheduleDelay)))
104+
}
105+
106+
return sdklog.NewBatchProcessor(exp, opts...), nil
107+
}
108+
109+
func otlpHTTPLogExporter(ctx context.Context, otlpConfig *OTLP) (sdklog.Exporter, error) {
110+
var opts []otlploghttp.Option
111+
112+
if len(otlpConfig.Endpoint) > 0 {
113+
u, err := url.ParseRequestURI(otlpConfig.Endpoint)
114+
if err != nil {
115+
return nil, err
116+
}
117+
opts = append(opts, otlploghttp.WithEndpoint(u.Host))
118+
119+
if u.Scheme == "http" {
120+
opts = append(opts, otlploghttp.WithInsecure())
121+
}
122+
if len(u.Path) > 0 {
123+
opts = append(opts, otlploghttp.WithURLPath(u.Path))
124+
}
125+
}
126+
if otlpConfig.Compression != nil {
127+
switch *otlpConfig.Compression {
128+
case compressionGzip:
129+
opts = append(opts, otlploghttp.WithCompression(otlploghttp.GzipCompression))
130+
case compressionNone:
131+
opts = append(opts, otlploghttp.WithCompression(otlploghttp.NoCompression))
132+
default:
133+
return nil, fmt.Errorf("unsupported compression %q", *otlpConfig.Compression)
134+
}
135+
}
136+
if otlpConfig.Timeout != nil && *otlpConfig.Timeout > 0 {
137+
opts = append(opts, otlploghttp.WithTimeout(time.Millisecond*time.Duration(*otlpConfig.Timeout)))
138+
}
139+
if len(otlpConfig.Headers) > 0 {
140+
opts = append(opts, otlploghttp.WithHeaders(otlpConfig.Headers))
141+
}
142+
143+
return otlploghttp.New(ctx, opts...)
144+
}

0 commit comments

Comments
 (0)