Skip to content

Commit

Permalink
reduce handler instance creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong committed Feb 25, 2024
1 parent 1748bf0 commit ca7f5d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
54 changes: 27 additions & 27 deletions gcp/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (h logHandler) Enabled(ctx context.Context, level slog.Level) bool {
}

func (h logHandler) Handle(ctx context.Context, record slog.Record) error { //nolint:funlen
handler := h.handler
attrs := make([]slog.Attr, 0, 6) //nolint:gomnd // 6 is the maximum number of attributes.

// Associate logs with a trace and span.
//
Expand All @@ -198,11 +198,11 @@ func (h logHandler) Handle(ctx context.Context, record slog.Record) error { //no
})
if !found {
if traceID, spanID, traceFlags := h.contextProvider(ctx); traceID != [16]byte{} {
handler = handler.WithAttrs([]slog.Attr{
attrs = append(attrs,
slog.String(TraceKey, hex.EncodeToString(traceID[:])),
slog.String(SpanKey, hex.EncodeToString(spanID[:])),
slog.String(TraceFlagsKey, hex.EncodeToString([]byte{traceFlags})),
})
)
}
}
}
Expand All @@ -227,32 +227,32 @@ func (h logHandler) Handle(ctx context.Context, record slog.Record) error { //no
callers = loadCallers(firstFrame)
}

handler = handler.WithAttrs(
[]slog.Attr{
{
Key: "context",
Value: slog.GroupValue(
slog.Attr{
Key: "reportLocation",
Value: slog.GroupValue(
slog.String("filePath", firstFrame.File),
slog.Int("lineNumber", firstFrame.Line),
slog.String("functionName", firstFrame.Function),
),
},
),
},
{
Key: "serviceContext",
Value: slog.GroupValue(
slog.String("service", h.service),
slog.String("version", h.version),
),
},
slog.String("stack_trace", stack(record.Message, callers)),
})
attrs = append(attrs,
slog.Attr{
Key: "context",
Value: slog.GroupValue(
slog.Attr{
Key: "reportLocation",
Value: slog.GroupValue(
slog.String("filePath", firstFrame.File),
slog.Int("lineNumber", firstFrame.Line),
slog.String("functionName", firstFrame.Function),
),
},
),
},
slog.Attr{
Key: "serviceContext",
Value: slog.GroupValue(
slog.String("service", h.service),
slog.String("version", h.version),
),
},
slog.String("stack_trace", stack(record.Message, callers)),
)
}

handler := h.handler.WithAttrs(attrs)
for _, group := range h.groups {
handler = handler.WithGroup(group.name).WithAttrs(group.attrs)
}
Expand Down
2 changes: 0 additions & 2 deletions rate/rate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ func TestHandler(t *testing.T) {
}

func TestHandler_race(t *testing.T) {
t.Parallel()

procs := (runtime.GOMAXPROCS(0) / 2) * 2
buf := &bytes.Buffer{}
handler := rate.New(
Expand Down

0 comments on commit ca7f5d6

Please sign in to comment.