Debian Codegraph @

otelconf/v0.2.0/log.go

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