File: logcontext.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (41 lines) | stat: -rw-r--r-- 1,329 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package logcontext

import newrelic "github.com/newrelic/go-agent"

// Keys used for logging context JSON.
const (
	KeyFile       = "file.name"
	KeyLevel      = "log.level"
	KeyLine       = "line.number"
	KeyMessage    = "message"
	KeyMethod     = "method.name"
	KeyTimestamp  = "timestamp"
	KeyTraceID    = "trace.id"
	KeySpanID     = "span.id"
	KeyEntityName = "entity.name"
	KeyEntityType = "entity.type"
	KeyEntityGUID = "entity.guid"
	KeyHostname   = "hostname"
)

func metadataMapField(m map[string]interface{}, key, val string) {
	if val != "" {
		m[key] = val
	}
}

// AddLinkingMetadata adds the LinkingMetadata into a map.  Only non-empty
// string fields are included in the map.  The specific key names facilitate
// agent logs in context.  These keys are: "trace.id", "span.id",
// "entity.name", "entity.type", "entity.guid", and "hostname".
func AddLinkingMetadata(m map[string]interface{}, md newrelic.LinkingMetadata) {
	metadataMapField(m, KeyTraceID, md.TraceID)
	metadataMapField(m, KeySpanID, md.SpanID)
	metadataMapField(m, KeyEntityName, md.EntityName)
	metadataMapField(m, KeyEntityType, md.EntityType)
	metadataMapField(m, KeyEntityGUID, md.EntityGUID)
	metadataMapField(m, KeyHostname, md.Hostname)
}