Skip to content

Commit 49bb5d4

Browse files
Fiery-FenixChrsMarkcodeboten
authored andcommitted
[exporter/clickhouse] Fix incorrect ServiceName set for Logs Records (open-telemetry#36350)
#### Description Fixing a bug in `clickhouseexporter` when ServiceName field in ClickHouse for `otel_logs` table is set from previous Log Record, when current LogRecord doesn't have `service.name` set #### Link to tracking issue Fixes open-telemetry#36349 #### Testing Respective unit test is added to code --------- Co-authored-by: Christos Markou <[email protected]> Co-authored-by: Alex Boten <[email protected]>
1 parent 745bad1 commit 49bb5d4

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: clickhouseexporter
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix incorrect Resource Attribute `service.name` translation to ClickHouse ServiceName field for Logs Records
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [36349]
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+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

exporter/clickhouseexporter/exporter_logs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ func (e *logsExporter) pushLogsData(ctx context.Context, ld plog.Logs) error {
7171
defer func() {
7272
_ = statement.Close()
7373
}()
74-
var serviceName string
7574

7675
for i := 0; i < ld.ResourceLogs().Len(); i++ {
7776
logs := ld.ResourceLogs().At(i)
7877
res := logs.Resource()
7978
resURL := logs.SchemaUrl()
8079
resAttr := attributesToMap(res.Attributes())
80+
var serviceName string
8181
if v, ok := res.Attributes().Get(conventions.AttributeServiceName); ok {
8282
serviceName = v.Str()
8383
}

exporter/clickhouseexporter/exporter_logs_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ func TestExporter_pushLogsData(t *testing.T) {
128128
exporter := newTestLogsExporter(t, defaultEndpoint)
129129
mustPushLogsData(t, exporter, simpleLogsWithNoTimestamp(1))
130130
})
131+
t.Run("test with 2 log records with different service.name", func(t *testing.T) {
132+
initClickhouseTestServer(t, func(query string, values []driver.Value) error {
133+
if strings.HasPrefix(query, "INSERT") {
134+
body, _ := values[7].(string)
135+
if body == "empty ServiceName" {
136+
require.Equal(t, "", values[6])
137+
} else {
138+
require.Equal(t, "test-service", values[6])
139+
}
140+
}
141+
return nil
142+
})
143+
144+
exporter := newTestLogsExporter(t, defaultEndpoint)
145+
mustPushLogsData(t, exporter, multipleLogsWithDifferentServiceName(1))
146+
})
131147
}
132148

133149
func TestLogsClusterConfig(t *testing.T) {
@@ -215,6 +231,30 @@ func simpleLogsWithNoTimestamp(count int) plog.Logs {
215231
return logs
216232
}
217233

234+
func multipleLogsWithDifferentServiceName(count int) plog.Logs {
235+
logs := simpleLogs(count)
236+
rl := logs.ResourceLogs().AppendEmpty()
237+
rl.SetSchemaUrl("https://opentelemetry.io/schemas/1.4.0")
238+
sl := rl.ScopeLogs().AppendEmpty()
239+
sl.SetSchemaUrl("https://opentelemetry.io/schemas/1.7.0")
240+
sl.Scope().SetName("io.opentelemetry.contrib.clickhouse")
241+
sl.Scope().SetVersion("1.0.0")
242+
sl.Scope().Attributes().PutStr("lib", "clickhouse")
243+
timestamp := time.Unix(1703498029, 0)
244+
for i := 0; i < count; i++ {
245+
r := sl.LogRecords().AppendEmpty()
246+
r.SetObservedTimestamp(pcommon.NewTimestampFromTime(timestamp))
247+
r.SetSeverityNumber(plog.SeverityNumberError2)
248+
r.SetSeverityText("error")
249+
r.Body().SetStr("empty ServiceName")
250+
r.Attributes().PutStr(conventions.AttributeServiceNamespace, "default")
251+
r.SetFlags(plog.DefaultLogRecordFlags)
252+
r.SetTraceID([16]byte{1, 2, 3, byte(i)})
253+
r.SetSpanID([8]byte{1, 2, 3, byte(i)})
254+
}
255+
return logs
256+
}
257+
218258
func mustPushLogsData(t *testing.T, exporter *logsExporter, ld plog.Logs) {
219259
err := exporter.pushLogsData(context.TODO(), ld)
220260
require.NoError(t, err)

0 commit comments

Comments
 (0)