Skip to content

nrslog: Logs in context does not work with durations #1013

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

Open
frankywahl opened this issue Mar 21, 2025 · 0 comments · May be fixed by #1021
Open

nrslog: Logs in context does not work with durations #1013

frankywahl opened this issue Mar 21, 2025 · 0 comments · May be fixed by #1021
Labels

Comments

@frankywahl
Copy link

frankywahl commented Mar 21, 2025

Description

Structured logging in go can use the nrslog.Duration type to define an attribute. However, that does not get reported as a duration, but instead gets reported as the string time.Duration

Steps to Reproduce

The following code sample is based from the example in https://github.com/newrelic/go-agent/blob/master/v3/integrations/logcontext-v2/nrslog/example/main.go and adds a slog.Duration attribute when logging.

package main

import (
	"context"
	"log/slog"
	"os"
	"time"

	"github.com/newrelic/go-agent/v3/integrations/logcontext-v2/nrslog"
	"github.com/newrelic/go-agent/v3/newrelic"
)

func main() {
	app, err := newrelic.NewApplication(
		newrelic.ConfigAppName("slog example app"),
		newrelic.ConfigFromEnvironment(),
		newrelic.ConfigAppLogEnabled(true),
	)
	if err != nil {
		panic(err)
	}

	app.WaitForConnection(time.Second * 5)
	log := slog.New(nrslog.TextHandler(app, os.Stdout, &slog.HandlerOptions{}))

	log.Info("I am a log message")

	txn := app.StartTransaction("example transaction")
	ctx := newrelic.NewContext(context.Background(), txn)

	log.InfoContext(ctx, "I am a log inside a transaction with custom attributes!",
		slog.String("foo", "bar"),
		slog.Int("answer", 42),
		slog.Duration("duration", 3*time.Second),
		slog.Any("some_map", map[string]interface{}{"a": 1.0, "b": 2}),
	)

	// pretend to do some work
	time.Sleep(500 * time.Millisecond)
	log.Warn("Uh oh, something important happened!")
	txn.End()

	log.Info("All Done!")

	app.Shutdown(time.Second * 10)
}

Expected Behavior

An actual duration (as integer, or float based on the unit) would be populated instead of the string.

Your Environment

Go: 1.24
Go-Agent: v3.38.0

Reproduction case

Image

Additional context

Note: time.Time has similar issues in that it comes as a string timestamp which NR would then have to parse.

@frankywahl frankywahl added the bug label Mar 21, 2025
@iamemilio iamemilio linked a pull request Apr 7, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant