Skip to content

Commit a867641

Browse files
authored
[mdatagen] Add context parameter to set traceID and spanID in generated events (#13068)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Add context parameter to set traceID and spanID in generated events <!-- Issue number if applicable --> #### Link to tracking issue Part of #12571 <!--Describe what testing was performed and which tests were added.--> #### Testing Added <!--Describe the documentation added.--> #### Documentation Added <!--Please delete paragraphs that you did not use before submitting.-->
1 parent fef33e9 commit a867641

File tree

5 files changed

+107
-24
lines changed

5 files changed

+107
-24
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: breaking
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: mdatagen
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add context parameter for recording event to set traceID and spanID
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12571]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_logs.go

Lines changed: 30 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_logs_test.go

Lines changed: 18 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/mdatagen/internal/templates/logs.go.tmpl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import (
1212
{{- if .SemConvVersion }}
1313
conventions "go.opentelemetry.io/otel/semconv/v{{ .SemConvVersion }}"
1414
{{- end }}
15+
{{- if .Events }}
16+
"context"
17+
"go.opentelemetry.io/otel/trace"
18+
"go.opentelemetry.io/collector/filter"
19+
{{- end }}
1520
)
1621

1722

@@ -21,15 +26,21 @@ type event{{ $name.Render }} struct {
2126
config EventConfig // event config provided by user.
2227
}
2328

24-
func (e *event{{ $name.Render }}) recordEvent(timestamp pcommon.Timestamp
29+
func (e *event{{ $name.Render }}) recordEvent(ctx context.Context, timestamp pcommon.Timestamp
2530
{{- range $event.Attributes -}}, {{ .RenderUnexported }}AttributeValue {{ (attributeInfo .).Type.Primitive }}{{ end }}) {
2631
if !e.config.Enabled {
2732
return
2833
}
2934
lr := e.data.AppendEmpty()
3035
lr.SetEventName("{{ $name }}")
3136
lr.SetTimestamp(timestamp)
32-
{{- range $event.Attributes }}
37+
38+
if span := trace.SpanContextFromContext(ctx); span.IsValid() {
39+
lr.SetTraceID(pcommon.TraceID(span.TraceID()))
40+
lr.SetSpanID(pcommon.SpanID(span.SpanID()))
41+
}
42+
43+
{{ range $event.Attributes }}
3344
{{- if eq (attributeInfo .).Type.Primitive "[]byte" }}
3445
lr.Attributes().PutEmptyBytes("{{ (attributeInfo .).Name }}").FromRaw({{ .RenderUnexported }}AttributeValue)
3546
{{- else if eq (attributeInfo .).Type.Primitive "[]any" }}
@@ -236,11 +247,11 @@ func (lb *LogsBuilder) Emit(options ...ResourceLogsOption) plog.Logs {
236247

237248
{{ range $name, $event := .Events -}}
238249
// Record{{ $name.Render }}Event adds a log record of {{ $name }} event.
239-
func (lb *LogsBuilder) Record{{ $name.Render }}Event(timestamp pcommon.Timestamp
250+
func (lb *LogsBuilder) Record{{ $name.Render }}Event(ctx context.Context, timestamp pcommon.Timestamp
240251
{{- range $event.Attributes -}}
241252
, {{ .RenderUnexported }}AttributeValue {{ if (attributeInfo .).Enum }}Attribute{{ .Render }}{{ else }}{{ (attributeInfo .).Type.Primitive }}{{ end }}
242253
{{- end }}) {
243-
lb.event{{ $name.Render }}.recordEvent(timestamp
254+
lb.event{{ $name.Render }}.recordEvent(ctx, timestamp
244255
{{- range $event.Attributes -}}
245256
, {{ .RenderUnexported }}AttributeValue{{ if (attributeInfo .).Enum }}.String(){{ end }}
246257
{{- end }})

cmd/mdatagen/internal/templates/logs_test.go.tmpl

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ package {{ .Package }}
55
import (
66
"time"
77
"testing"
8-
98
"github.com/stretchr/testify/assert"
109
"go.uber.org/zap"
1110
"go.uber.org/zap/zaptest/observer"
12-
13-
"go.opentelemetry.io/collector/pdata/pcommon"
14-
"go.opentelemetry.io/collector/pdata/plog"
15-
{{- if or isReceiver isScraper }}
16-
"go.opentelemetry.io/collector/{{ .Status.Class }}/{{ .Status.Class }}test"
17-
{{- end }}
11+
"go.opentelemetry.io/collector/pdata/pcommon"
12+
"go.opentelemetry.io/collector/pdata/plog"
13+
{{- if or isReceiver isScraper }}
14+
"go.opentelemetry.io/collector/{{ .Status.Class }}/{{ .Status.Class }}test"
15+
{{- end }}
16+
{{- if .Events }}
17+
"context"
18+
"go.opentelemetry.io/otel/trace"
19+
{{- end }}
1820
)
1921

2022
{{- if .Events }}
@@ -128,6 +130,13 @@ func TestLogsBuilder(t *testing.T) {
128130
for _, tt := range tests {
129131
t.Run(tt.name, func(t *testing.T) {
130132
timestamp := pcommon.Timestamp(1_000_001_000)
133+
traceID := [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
134+
spanID := [8]byte{0, 1, 2, 3, 4, 5, 6, 7}
135+
ctx := trace.ContextWithSpanContext(context.Background(), trace.NewSpanContext(trace.SpanContextConfig{
136+
TraceID: trace.TraceID(traceID),
137+
SpanID: trace.SpanID(spanID),
138+
TraceFlags: trace.FlagsSampled,
139+
}))
131140
observedZapCore, observedLogs := observer.New(zap.WarnLevel)
132141
{{- if or isReceiver isScraper }}
133142
settings := {{ .Status.Class }}test.NewNopSettings({{ .Status.Class }}test.NopType)
@@ -185,7 +194,7 @@ func TestLogsBuilder(t *testing.T) {
185194
{{- range $name, $event := .Events }}
186195
{{ if $event.Enabled }}defaultEventsCount++{{ end }}
187196
allEventsCount++
188-
lb.Record{{ $name.Render }}Event(timestamp
197+
lb.Record{{ $name.Render }}Event(ctx, timestamp
189198
{{- range $event.Attributes -}}
190199
, {{ if (attributeInfo .).Enum }}Attribute{{ .Render }}{{ (index (attributeInfo .).Enum 0) | publicVar }}{{ else }}{{ (attributeInfo .).TestValue }}{{ end }}
191200
{{- end }})
@@ -231,6 +240,8 @@ func TestLogsBuilder(t *testing.T) {
231240
validatedEvents["{{ $name }}"] = true
232241
lr := lrs.At(i)
233242
assert.Equal(t, timestamp, lr.Timestamp())
243+
assert.Equal(t, pcommon.TraceID(traceID), lr.TraceID())
244+
assert.Equal(t, pcommon.SpanID(spanID), lr.SpanID())
234245

235246
{{- range $i, $attr := $event.Attributes }}
236247
attrVal, ok {{ if eq $i 0 }}:{{ end }}= lr.Attributes().Get("{{ (attributeInfo $attr).Name }}")

0 commit comments

Comments
 (0)