Skip to content

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

Open
@frankywahl

Description

@frankywahl

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions