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

Format log message before logging with logr #4143

Merged
merged 8 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `NewPeriodicReader` in `go.opentelemetry.io/otel/sdk/metric` returns `*PeriodicReader` instead of `Reader`. (#4244)
- Count the Collect time in the PeriodicReader timeout. (#4221)

### Fixed

- Correctly format log messages from the `go.opentelemetry.io/otel/exporters/zipkin` exporter. (#4143)

## [1.16.0/0.39.0] 2023-05-18

This release contains the first stable release of the OpenTelemetry Go [metric API].
Expand Down
2 changes: 1 addition & 1 deletion exporters/zipkin/zipkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (e *Exporter) Shutdown(ctx context.Context) error {

func (e *Exporter) logf(format string, args ...interface{}) {
if e.logger != emptyLogger {
e.logger.Info(format, args)
e.logger.Info(fmt.Sprintf(format, args...))
}
}

Expand Down
20 changes: 20 additions & 0 deletions exporters/zipkin/zipkin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package zipkin

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -28,6 +29,7 @@ import (

ottest "go.opentelemetry.io/otel/internal/internaltest"

"github.com/go-logr/logr/funcr"
zkmodel "github.com/openzipkin/zipkin-go/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -364,3 +366,21 @@ func TestErrorOnExportShutdownExporter(t *testing.T) {
assert.NoError(t, exp.Shutdown(context.Background()))
assert.NoError(t, exp.ExportSpans(context.Background(), nil))
}

func TestLogrFormatting(t *testing.T) {
format := "string %q, int %d"
args := []interface{}{"s", 1}
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

var buf bytes.Buffer
l := funcr.New(func(prefix, args string) {
_, _ = buf.WriteString(fmt.Sprint(prefix, args))
}, funcr.Options{})
exp, err := New("", WithLogr(l))
require.NoError(t, err)

exp.logf(format, args...)
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

want := "\"level\"=0 \"msg\"=\"string \\\"s\\\", int 1\""
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
got := buf.String()
assert.Equal(t, want, got)
}