Skip to content

Commit

Permalink
Format log message before logging with logr (#4143)
Browse files Browse the repository at this point in the history
* Format log message before logging with logr

Fixes #4141

The logr calling convention does not support fmt semantics for message
formatting. Format the message before it is sent to logr for logging.

* Add change to changelog

* Fix lint

Don't shadow the log pkg.

* Replace buflogr with funcr

* Run make

* Update exporters/zipkin/zipkin_test.go

Co-authored-by: Robert Pająk <pellared@hotmail.com>

---------

Co-authored-by: Robert Pająk <pellared@hotmail.com>
  • Loading branch information
MrAlias and pellared committed Jun 26, 2023
1 parent 5e69812 commit 1b02c91
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
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}

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...)

want := "\"level\"=0 \"msg\"=\"string \\\"s\\\", int 1\""
got := buf.String()
assert.Equal(t, want, got)
}

0 comments on commit 1b02c91

Please sign in to comment.