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 5 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 @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### 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
19 changes: 19 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,20 @@ 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)
}