Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skeleton for zerolog bridge #5782

Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6dc2b69
added some funcs in hook.go
AkhigbeEromo Jun 18, 2024
0ba4cbb
added tests for the funcs in hook.go
AkhigbeEromo Jun 18, 2024
d535bc1
lint corrections
AkhigbeEromo Jun 18, 2024
3457523
Merge branch 'main' into Add-OpenTelemetry-SeverityHook-Integration-f…
AkhigbeEromo Jun 18, 2024
b58f8bc
Merge branch 'main' into Add-OpenTelemetry-SeverityHook-Integration-f…
AkhigbeEromo Jun 19, 2024
d650c08
Merge branch 'main' into Add-OpenTelemetry-SeverityHook-Integration-f…
AkhigbeEromo Jun 21, 2024
e014a2b
Changed hook levels funcs
AkhigbeEromo Jun 24, 2024
a0c0ed1
Merge branch 'main' into Add-OpenTelemetry-SeverityHook-Integration-f…
AkhigbeEromo Jul 2, 2024
207f20c
reduced code to just bolierplate code
AkhigbeEromo Jul 2, 2024
b8483cd
removed a function
AkhigbeEromo Jul 2, 2024
e55346d
Merge branch 'main' into Add-OpenTelemetry-SeverityHook-Integration-f…
AkhigbeEromo Jul 5, 2024
93c61b6
Update bridges/otelzerolog/hook.go
AkhigbeEromo Jul 8, 2024
66b8028
Merge branch 'main' into Add-OpenTelemetry-SeverityHook-Integration-f…
AkhigbeEromo Jul 8, 2024
20226d9
updated versions.yaml file
AkhigbeEromo Jul 8, 2024
cf489cb
fixed lint issues
AkhigbeEromo Jul 9, 2024
b9050cb
Merge branch 'main' into Add-OpenTelemetry-SeverityHook-Integration-f…
AkhigbeEromo Jul 9, 2024
8c23b31
Merge branch 'main' into Add-OpenTelemetry-SeverityHook-Integration-f…
AkhigbeEromo Jul 9, 2024
0cf086e
worked on skeletal structure
AkhigbeEromo Jul 9, 2024
d506ef2
fixed lint issues
AkhigbeEromo Jul 9, 2024
1c473e4
Update bridges/otelzerolog/hook.go
AkhigbeEromo Jul 10, 2024
988836c
Merge branch 'main' into Add-OpenTelemetry-SeverityHook-Integration-f…
AkhigbeEromo Jul 10, 2024
1b16f1b
added test for new severity hook
AkhigbeEromo Jul 10, 2024
842392a
removed enabled method
AkhigbeEromo Jul 10, 2024
f71a9bb
add example_test
AkhigbeEromo Jul 10, 2024
ed611a9
fixed lint issues
AkhigbeEromo Jul 10, 2024
5bd15a1
Merge branch 'main' into Add-OpenTelemetry-SeverityHook-Integration-f…
AkhigbeEromo Jul 10, 2024
e7887a9
Update bridges/otelzerolog/hook.go
AkhigbeEromo Jul 11, 2024
f2cf358
Update bridges/otelzerolog/hook.go
AkhigbeEromo Jul 11, 2024
0faabe0
Update bridges/otelzerolog/hook.go
AkhigbeEromo Jul 11, 2024
d1cce3b
added comment to example_test
AkhigbeEromo Jul 11, 2024
0065fbd
Merge branch 'main' into Add-OpenTelemetry-SeverityHook-Integration-f…
AkhigbeEromo Jul 11, 2024
bf91ce1
Update bridges/otelzerolog/example_test.go
AkhigbeEromo Jul 11, 2024
ecd5abf
Changed Severity Hook to Hook
AkhigbeEromo Jul 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions bridges/otelzerolog/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module go.opentelemetry.io/contrib/bridges/otelzerolog

go 1.21

require (
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel/log v0.3.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
40 changes: 40 additions & 0 deletions bridges/otelzerolog/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.opentelemetry.io/otel v1.27.0 h1:9BZoF3yMK/O1AafMiQTVu0YDj5Ea4hPhxCs7sGva+cg=
go.opentelemetry.io/otel v1.27.0/go.mod h1:DMpAK8fzYRzs+bi3rS5REupisuqTheUlSZJ1WnZaPAQ=
go.opentelemetry.io/otel/log v0.3.0 h1:kJRFkpUFYtny37NQzL386WbznUByZx186DpEMKhEGZs=
go.opentelemetry.io/otel/log v0.3.0/go.mod h1:ziCwqZr9soYDwGNbIL+6kAvQC+ANvjgG367HVcyR/ys=
go.opentelemetry.io/otel/metric v1.27.0 h1:hvj3vdEKyeCi4YaYfNjv2NUje8FqKqUY8IlF0FxV/ik=
go.opentelemetry.io/otel/metric v1.27.0/go.mod h1:mVFgmRlhljgBiuk/MP/oKylr4hs85GZAylncepAX/ak=
go.opentelemetry.io/otel/trace v1.27.0 h1:IqYb813p7cmbHk0a5y6pD5JPakbVfftRXABGt5/Rscw=
go.opentelemetry.io/otel/trace v1.27.0/go.mod h1:6RiD1hkAprV4/q+yd2ln1HG9GoPx39SuvvstaLBl+l4=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
112 changes: 112 additions & 0 deletions bridges/otelzerolog/hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Package otelzerolog provides a SeverityHook, a zerolog.Hook implementation that
AkhigbeEromo marked this conversation as resolved.
Show resolved Hide resolved
// can be used to bridge between the [zerolog] API and [OpenTelemetry].
// [zerolog]: https://github.com/rs/zerolog
AkhigbeEromo marked this conversation as resolved.
Show resolved Hide resolved
// [OpenTelemetry]: https://opentelemetry.io/docs/concepts/signals/logs/
package otelzerolog // import "go.opentelemetry.io/contrib/bridges/otelzerolog"

import (
"github.com/rs/zerolog"

"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/global"
)

type config struct {
provider log.LoggerProvider
version string
schemaURL string
}

func newConfig(options []Option) config {
var c config
for _, opt := range options {
c = opt.apply(c)
}

if c.provider == nil {
c.provider = global.GetLoggerProvider()
}
return c
}

func (c config) logger(name string) log.Logger {
var opts []log.LoggerOption
if c.version != "" {
opts = append(opts, log.WithInstrumentationVersion(c.version))
}
if c.schemaURL != "" {
opts = append(opts, log.WithSchemaURL(c.schemaURL))
}
return c.provider.Logger(name, opts...)
}

// Option configures a SeverityHook.
type Option interface {
apply(config) config
}
type optFunc func(config) config

func (f optFunc) apply(c config) config { return f(c) }

// WithVersion returns an [Option] that configures the version of the
// [log.Logger] used by a [SeverityHook]. The version should be the version of the
// package that is being logged.
func WithVersion(version string) Option {
return optFunc(func(c config) config {
c.version = version
return c
})
}

// WithSchemaURL returns an [Option] that configures the semantic convention
// schema URL of the [log.Logger] used by a [SeverityHook]. The schemaURL should be
// the schema URL for the semantic conventions used in log records.
func WithSchemaURL(schemaURL string) Option {
return optFunc(func(c config) config {
c.schemaURL = schemaURL
return c
})
}

// WithLoggerProvider returns an [Option] that configures [log.LoggerProvider]
// used by a [SeverityHook].
//
// By default if this Option is not provided, the SeverityHook will use the global
// LoggerProvider.
func WithLoggerProvider(provider log.LoggerProvider) Option {
return optFunc(func(c config) config {
c.provider = provider
return c
})
}

// SeverityHook is a [zerolog.Hook] that sends all logging records it receives to
// OpenTelemetry. See package documentation for how conversions are made.
type SeverityHook struct {
logger log.Logger
}
pellared marked this conversation as resolved.
Show resolved Hide resolved

// NewSeverityHook returns a new [SeverityHook] to be used as a [Zerolog.Hook].
// If [WithLoggerProvider] is not provided, the returned SeverityHook will use the
// global LoggerProvider.
func NewSeverityHook(name string, options ...Option) *SeverityHook {
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
cfg := newConfig(options)
return &SeverityHook{
logger: cfg.logger(name),
}
}

// TODO
// LevelEnabler decides whether a given logging level is enabled when logging a message.
AkhigbeEromo marked this conversation as resolved.
Show resolved Hide resolved
func (o *SeverityHook) Enabled(level zerolog.Level) bool {
AkhigbeEromo marked this conversation as resolved.
Show resolved Hide resolved
return true
}

// TODO
// Run handles the passed record, and sends it to OpenTelemetry.
func (h SeverityHook) Run(e *zerolog.Event, level zerolog.Level, msg string) error {
return nil
}
72 changes: 72 additions & 0 deletions bridges/otelzerolog/hook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package otelzerolog

import (
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/embedded"
"go.opentelemetry.io/otel/log/global"
)

type mockLoggerProvider struct {
embedded.LoggerProvider
}

func (mockLoggerProvider) Logger(name string, options ...log.LoggerOption) log.Logger {
return nil
}

func TestNewConfig(t *testing.T) {
customLoggerProvider := mockLoggerProvider{}

for _, tt := range []struct {
name string
options []Option

wantConfig config
}{
{
name: "with no options",
wantConfig: config{
provider: global.GetLoggerProvider(),
},
},
{
name: "with a custom version",
options: []Option{
WithVersion("1.0"),
},
wantConfig: config{
version: "1.0",
provider: global.GetLoggerProvider(),
},
},
{
name: "with a custom schema URL",
options: []Option{
WithSchemaURL("https://example.com"),
},
wantConfig: config{
schemaURL: "https://example.com",
provider: global.GetLoggerProvider(),
},
},
{
name: "with a custom logger provider",
options: []Option{
WithLoggerProvider(customLoggerProvider),
},
wantConfig: config{
provider: customLoggerProvider,
},
},
} {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.wantConfig, newConfig(tt.options))
})
}
}
1 change: 1 addition & 0 deletions versions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ module-sets:
modules:
- go.opentelemetry.io/contrib/detectors/azure/azurevm
excluded-modules:
- go.opentelemetry.io/contrib/bridges/otelzerolog
- go.opentelemetry.io/contrib/bridges/otelzap
- go.opentelemetry.io/contrib/instrgen
- go.opentelemetry.io/contrib/instrgen/driver
Expand Down