Skip to content

Commit

Permalink
feat: Add minimal log level support to OpenTelemetry Core (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbatoufflet authored Jan 21, 2024
1 parent 3c92bfe commit 16df32c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
5 changes: 3 additions & 2 deletions otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type otlpCore struct {
logger otel.Logger

fields []zapcore.Field
level zapcore.Level
}

var instrumentationScope = instrumentation.Scope{
Expand All @@ -42,8 +43,8 @@ var instrumentationScope = instrumentation.Scope{
SchemaURL: semconv.SchemaURL,
}

func (otlpCore) Enabled(zapcore.Level) bool {
return true
func (c *otlpCore) Enabled(level zapcore.Level) bool {
return c.level.Enabled(level)
}

func (c *otlpCore) With(f []zapcore.Field) zapcore.Core {
Expand Down
20 changes: 18 additions & 2 deletions otelzap/otelzap.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,29 @@ import (
)

// NewOtelCore creates new OpenTelemetry Core to export logs in OTLP format
func NewOtelCore(loggerProvider otel.LoggerProvider) zapcore.Core {
func NewOtelCore(loggerProvider otel.LoggerProvider, opts ...Option) zapcore.Core {
logger := loggerProvider.Logger(
instrumentationScope.Name,
otel.WithInstrumentationVersion(instrumentationScope.Version),
)

return &otlpCore{
c := &otlpCore{
logger: logger,
level: zapcore.InfoLevel,
}
for _, apply := range opts {
apply(c)
}

return c
}

// Option is a function that applies an option to an OpenTelemetry Core
type Option func(c *otlpCore)

// WithLevel sets the minimum level for the OpenTelemetry Core log to be exported
func WithLevel(level zapcore.Level) Option {
return Option(func(c *otlpCore) {
c.level = level
})
}

0 comments on commit 16df32c

Please sign in to comment.