| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package log // import "go.opentelemetry.io/otel/sdk/log" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"sync" |
| 10 |
"sync/atomic" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel" |
| 13 |
"go.opentelemetry.io/otel/internal/global" |
| 14 |
"go.opentelemetry.io/otel/log" |
| 15 |
"go.opentelemetry.io/otel/log/embedded" |
| 16 |
"go.opentelemetry.io/otel/log/noop" |
| 17 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 18 |
"go.opentelemetry.io/otel/sdk/resource" |
| 19 |
) |
| 20 |
|
| 21 |
const ( |
| 22 |
defaultAttrCntLim = 128 |
| 23 |
defaultAttrValLenLim = -1 |
| 24 |
|
| 25 |
envarAttrCntLim = "OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT" |
| 26 |
envarAttrValLenLim = "OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT" |
| 27 |
) |
| 28 |
|
| 29 |
type providerConfig struct { |
| 30 |
resource *resource.Resource |
| 31 |
processors []Processor |
| 32 |
attrCntLim setting[int] |
| 33 |
attrValLenLim setting[int] |
| 34 |
allowDupKeys setting[bool] |
| 35 |
} |
| 36 |
|
| 37 |
func newProviderConfig(opts []LoggerProviderOption) providerConfig { |
| 38 |
var c providerConfig |
| 39 |
for _, opt := range opts { |
| 40 |
c = opt.apply(c) |
| 41 |
} |
| 42 |
|
| 43 |
if c.resource == nil { |
| 44 |
c.resource = resource.Default() |
| 45 |
} |
| 46 |
|
| 47 |
c.attrCntLim = c.attrCntLim.Resolve( |
| 48 |
getenv[int](envarAttrCntLim), |
| 49 |
fallback[int](defaultAttrCntLim), |
| 50 |
) |
| 51 |
|
| 52 |
c.attrValLenLim = c.attrValLenLim.Resolve( |
| 53 |
getenv[int](envarAttrValLenLim), |
| 54 |
fallback[int](defaultAttrValLenLim), |
| 55 |
) |
| 56 |
|
| 57 |
return c |
| 58 |
} |
| 59 |
|
| 60 |
// LoggerProvider handles the creation and coordination of Loggers. All Loggers |
| 61 |
// created by a LoggerProvider will be associated with the same Resource. |
| 62 |
type LoggerProvider struct { |
| 63 |
embedded.LoggerProvider |
| 64 |
|
| 65 |
resource *resource.Resource |
| 66 |
processors []Processor |
| 67 |
attributeCountLimit int |
| 68 |
attributeValueLengthLimit int |
| 69 |
allowDupKeys bool |
| 70 |
|
| 71 |
loggersMu sync.Mutex |
| 72 |
loggers map[instrumentation.Scope]*logger |
| 73 |
|
| 74 |
stopped atomic.Bool |
| 75 |
|
| 76 |
noCmp [0]func() //nolint: unused // This is indeed used. |
| 77 |
} |
| 78 |
|
| 79 |
// Compile-time check LoggerProvider implements log.LoggerProvider. |
| 80 |
var _ log.LoggerProvider = (*LoggerProvider)(nil) |
| 81 |
|
| 82 |
// NewLoggerProvider returns a new and configured LoggerProvider. |
| 83 |
// |
| 84 |
// By default, the returned LoggerProvider is configured with the default |
| 85 |
// Resource and no Processors. Processors cannot be added after a LoggerProvider is |
| 86 |
// created. This means the returned LoggerProvider, one created with no |
| 87 |
// Processors, will perform no operations. |
| 88 |
func NewLoggerProvider(opts ...LoggerProviderOption) *LoggerProvider { |
| 89 |
cfg := newProviderConfig(opts) |
| 90 |
return &LoggerProvider{ |
| 91 |
resource: cfg.resource, |
| 92 |
processors: cfg.processors, |
| 93 |
attributeCountLimit: cfg.attrCntLim.Value, |
| 94 |
attributeValueLengthLimit: cfg.attrValLenLim.Value, |
| 95 |
allowDupKeys: cfg.allowDupKeys.Value, |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// Logger returns a new [log.Logger] with the provided name and configuration. |
| 100 |
// |
| 101 |
// If p is shut down, a [noop.Logger] instance is returned. |
| 102 |
// |
| 103 |
// This method can be called concurrently. |
| 104 |
func (p *LoggerProvider) Logger(name string, opts ...log.LoggerOption) log.Logger { |
| 105 |
if name == "" { |
| 106 |
global.Warn("Invalid Logger name.", "name", name) |
| 107 |
} |
| 108 |
|
| 109 |
if p.stopped.Load() { |
| 110 |
return noop.NewLoggerProvider().Logger(name, opts...) |
| 111 |
} |
| 112 |
|
| 113 |
cfg := log.NewLoggerConfig(opts...) |
| 114 |
scope := instrumentation.Scope{ |
| 115 |
Name: name, |
| 116 |
Version: cfg.InstrumentationVersion(), |
| 117 |
SchemaURL: cfg.SchemaURL(), |
| 118 |
Attributes: cfg.InstrumentationAttributes(), |
| 119 |
} |
| 120 |
|
| 121 |
p.loggersMu.Lock() |
| 122 |
defer p.loggersMu.Unlock() |
| 123 |
|
| 124 |
if p.loggers == nil { |
| 125 |
l := newLogger(p, scope) |
| 126 |
p.loggers = map[instrumentation.Scope]*logger{scope: l} |
| 127 |
return l |
| 128 |
} |
| 129 |
|
| 130 |
l, ok := p.loggers[scope] |
| 131 |
if !ok { |
| 132 |
l = newLogger(p, scope) |
| 133 |
p.loggers[scope] = l |
| 134 |
} |
| 135 |
|
| 136 |
return l |
| 137 |
} |
| 138 |
|
| 139 |
// Shutdown shuts down the provider and all processors. |
| 140 |
// |
| 141 |
// This method can be called concurrently. |
| 142 |
func (p *LoggerProvider) Shutdown(ctx context.Context) error { |
| 143 |
stopped := p.stopped.Swap(true) |
| 144 |
if stopped { |
| 145 |
return nil |
| 146 |
} |
| 147 |
|
| 148 |
var err error |
| 149 |
for _, p := range p.processors { |
| 150 |
err = errors.Join(err, p.Shutdown(ctx)) |
| 151 |
} |
| 152 |
return err |
| 153 |
} |
| 154 |
|
| 155 |
// ForceFlush flushes all processors. |
| 156 |
// |
| 157 |
// This method can be called concurrently. |
| 158 |
func (p *LoggerProvider) ForceFlush(ctx context.Context) error { |
| 159 |
if p.stopped.Load() { |
| 160 |
return nil |
| 161 |
} |
| 162 |
|
| 163 |
var err error |
| 164 |
for _, p := range p.processors { |
| 165 |
err = errors.Join(err, p.ForceFlush(ctx)) |
| 166 |
} |
| 167 |
return err |
| 168 |
} |
| 169 |
|
| 170 |
// LoggerProviderOption applies a configuration option value to a LoggerProvider. |
| 171 |
type LoggerProviderOption interface { |
| 172 |
apply(providerConfig) providerConfig |
| 173 |
} |
| 174 |
|
| 175 |
type loggerProviderOptionFunc func(providerConfig) providerConfig |
| 176 |
|
| 177 |
func (fn loggerProviderOptionFunc) apply(c providerConfig) providerConfig { |
| 178 |
return fn(c) |
| 179 |
} |
| 180 |
|
| 181 |
// WithResource associates a Resource with a LoggerProvider. This Resource |
| 182 |
// represents the entity producing telemetry and is associated with all Loggers |
| 183 |
// the LoggerProvider will create. |
| 184 |
// |
| 185 |
// By default, if this Option is not used, the default Resource from the |
| 186 |
// go.opentelemetry.io/otel/sdk/resource package will be used. |
| 187 |
func WithResource(res *resource.Resource) LoggerProviderOption { |
| 188 |
return loggerProviderOptionFunc(func(cfg providerConfig) providerConfig { |
| 189 |
var err error |
| 190 |
cfg.resource, err = resource.Merge(resource.Environment(), res) |
| 191 |
if err != nil { |
| 192 |
otel.Handle(err) |
| 193 |
} |
| 194 |
return cfg |
| 195 |
}) |
| 196 |
} |
| 197 |
|
| 198 |
// WithProcessor associates Processor with a LoggerProvider. |
| 199 |
// |
| 200 |
// By default, if this option is not used, the LoggerProvider will perform no |
| 201 |
// operations; no data will be exported without a processor. |
| 202 |
// |
| 203 |
// The SDK invokes the processors sequentially in the same order as they were |
| 204 |
// registered. |
| 205 |
// |
| 206 |
// For production, use [NewBatchProcessor] to batch log records before they are exported. |
| 207 |
// For testing and debugging, use [NewSimpleProcessor] to synchronously export log records. |
| 208 |
func WithProcessor(processor Processor) LoggerProviderOption { |
| 209 |
return loggerProviderOptionFunc(func(cfg providerConfig) providerConfig { |
| 210 |
cfg.processors = append(cfg.processors, processor) |
| 211 |
return cfg |
| 212 |
}) |
| 213 |
} |
| 214 |
|
| 215 |
// WithAttributeCountLimit sets the maximum allowed log record attribute count. |
| 216 |
// Any attribute added to a log record once this limit is reached will be dropped. |
| 217 |
// |
| 218 |
// Setting this to zero means no attributes will be recorded. |
| 219 |
// |
| 220 |
// Setting this to a negative value means no limit is applied. |
| 221 |
// |
| 222 |
// If the OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT environment variable is set, |
| 223 |
// and this option is not passed, that variable value will be used. |
| 224 |
// |
| 225 |
// By default, if an environment variable is not set, and this option is not |
| 226 |
// passed, 128 will be used. |
| 227 |
func WithAttributeCountLimit(limit int) LoggerProviderOption { |
| 228 |
return loggerProviderOptionFunc(func(cfg providerConfig) providerConfig { |
| 229 |
cfg.attrCntLim = newSetting(limit) |
| 230 |
return cfg |
| 231 |
}) |
| 232 |
} |
| 233 |
|
| 234 |
// WithAttributeValueLengthLimit sets the maximum allowed attribute value length. |
| 235 |
// |
| 236 |
// This limit only applies to string and string slice attribute values. |
| 237 |
// Any string longer than this value will be truncated to this length. |
| 238 |
// |
| 239 |
// Setting this to a negative value means no limit is applied. |
| 240 |
// |
| 241 |
// If the OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT environment variable is set, |
| 242 |
// and this option is not passed, that variable value will be used. |
| 243 |
// |
| 244 |
// By default, if an environment variable is not set, and this option is not |
| 245 |
// passed, no limit (-1) will be used. |
| 246 |
func WithAttributeValueLengthLimit(limit int) LoggerProviderOption { |
| 247 |
return loggerProviderOptionFunc(func(cfg providerConfig) providerConfig { |
| 248 |
cfg.attrValLenLim = newSetting(limit) |
| 249 |
return cfg |
| 250 |
}) |
| 251 |
} |
| 252 |
|
| 253 |
// WithAllowKeyDuplication sets whether deduplication is skipped for log attributes or other key-value collections. |
| 254 |
// |
| 255 |
// By default, the key-value collections within a log record are deduplicated to comply with the OpenTelemetry Specification. |
| 256 |
// Deduplication means that if multiple key–value pairs with the same key are present, only a single pair |
| 257 |
// is retained and others are discarded. |
| 258 |
// |
| 259 |
// Disabling deduplication with this option can improve performance e.g. of adding attributes to the log record. |
| 260 |
// |
| 261 |
// Note that if you disable deduplication, you are responsible for ensuring that duplicate |
| 262 |
// key-value pairs within in a single collection are not emitted, |
| 263 |
// or that the telemetry receiver can handle such duplicates. |
| 264 |
func WithAllowKeyDuplication() LoggerProviderOption { |
| 265 |
return loggerProviderOptionFunc(func(cfg providerConfig) providerConfig { |
| 266 |
cfg.allowDupKeys = newSetting(true) |
| 267 |
return cfg |
| 268 |
}) |
| 269 |
} |