From ac2dcece0eb2628940673ffea8d01b70fb07b50e Mon Sep 17 00:00:00 2001 From: Robert Pajak Date: Thu, 20 Mar 2025 12:03:19 +0100 Subject: [PATCH] otellogr: Add WithAttributes option --- CHANGELOG.md | 1 + bridges/otellogr/logsink.go | 20 +++++++++++++++++--- bridges/otellogr/logsink_test.go | 11 +++++++---- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b73f991c01..c17d9927c6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - `http.route` attribute to otelhttp server request spans, when `net/http.Request.Pattern` is set in `go.opentelemetry.io/contrib/instrumentation/github.com/emicklei/go-restful/otelrestful`, `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`, `go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux`, `go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho` and `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#6905) - Add `WithAttributes` option to set instrumentation scope attributes on the created `log.Logger` in `go.opentelemetry.io/contrib/bridges/otelzap`. (#6962) +- Add `WithAttributes` option to set instrumentation scope attributes on the created `log.Logger` in `go.opentelemetry.io/contrib/bridges/otellogr`. (#6967) ### Changed diff --git a/bridges/otellogr/logsink.go b/bridges/otellogr/logsink.go index 6332db075e6..28a4d0f7805 100644 --- a/bridges/otellogr/logsink.go +++ b/bridges/otellogr/logsink.go @@ -59,15 +59,17 @@ import ( "github.com/go-logr/logr" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/log" "go.opentelemetry.io/otel/log/global" semconv "go.opentelemetry.io/otel/semconv/v1.27.0" ) type config struct { - provider log.LoggerProvider - version string - schemaURL string + provider log.LoggerProvider + version string + schemaURL string + attributes []attribute.KeyValue levelSeverity func(int) log.Severity } @@ -127,6 +129,15 @@ func WithSchemaURL(schemaURL string) Option { }) } +// WithAttributes returns an [Option] that configures the instrumentation scope +// attributes of the [log.Logger] used by a [LogSink]. +func WithAttributes(attributes ...attribute.KeyValue) Option { + return optFunc(func(c config) config { + c.attributes = attributes + return c + }) +} + // WithLoggerProvider returns an [Option] that configures [log.LoggerProvider] // used by a [LogSink] to create its [log.Logger]. // @@ -169,6 +180,9 @@ func NewLogSink(name string, options ...Option) *LogSink { if c.schemaURL != "" { opts = append(opts, log.WithSchemaURL(c.schemaURL)) } + if c.attributes != nil { + opts = append(opts, log.WithInstrumentationAttributes(c.attributes...)) + } return &LogSink{ name: name, diff --git a/bridges/otellogr/logsink_test.go b/bridges/otellogr/logsink_test.go index ec2aeb489d5..6330388a930 100644 --- a/bridges/otellogr/logsink_test.go +++ b/bridges/otellogr/logsink_test.go @@ -12,6 +12,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/log" "go.opentelemetry.io/otel/log/embedded" "go.opentelemetry.io/otel/log/global" @@ -85,15 +86,17 @@ func TestNewLogSink(t *testing.T) { wantScopeRecords: &logtest.ScopeRecords{Name: name}, }, { - name: "with version and schema URL", + name: "with custom options", options: []Option{ WithVersion("1.0"), WithSchemaURL("https://example.com"), + WithAttributes(attribute.String("testattr", "testval")), }, wantScopeRecords: &logtest.ScopeRecords{ - Name: name, - Version: "1.0", - SchemaURL: "https://example.com", + Name: name, + Version: "1.0", + SchemaURL: "https://example.com", + Attributes: attribute.NewSet(attribute.String("testattr", "testval")), }, }, } {