Skip to content

Commit ae7d5ab

Browse files
committed
Translate OTEL log's InstrumentationScope as logger name
InstrumentationScope usually contains the name of the instrumentation library. However, in the case of logs, it may also contain the name of the logger if the log source defines it, see [here][1]. An instrumentation library usually has a version set, so assume that the name without the version is the name of the logger. [1]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/api.md#get-a-logger Signed-off-by: Oldřich Jedlička <oldium.pro@gmail.com>
1 parent c7b04dc commit ae7d5ab

4 files changed

Lines changed: 32 additions & 4 deletions

File tree

input/otlp/logs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (c *Consumer) convertLogRecord(
119119
event := baseEvent.CloneVT()
120120
initEventLabels(event)
121121

122-
translateScopeMetadata(scope, event)
122+
translateLogScopeMetadata(scope, event)
123123

124124
if record.Timestamp() == 0 {
125125
event.Timestamp = modelpb.FromTime(record.ObservedTimestamp().AsTime().Add(timeDelta))

input/otlp/metadata.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ func translateResourceMetadata(resource pcommon.Resource, out *modelpb.APMEvent)
456456
}
457457
}
458458

459-
func translateScopeMetadata(scope pcommon.InstrumentationScope, out *modelpb.APMEvent) {
459+
func translateScopeCommonMetadata(scope pcommon.InstrumentationScope, out *modelpb.APMEvent) {
460460
scope.Attributes().Range(func(k string, v pcommon.Value) bool {
461461
switch k {
462462
// data_stream.*
@@ -473,7 +473,9 @@ func translateScopeMetadata(scope pcommon.InstrumentationScope, out *modelpb.APM
473473
}
474474
return true
475475
})
476+
}
476477

478+
func translateScopeFrameworkMetadata(scope pcommon.InstrumentationScope, out *modelpb.APMEvent) {
477479
if name := scope.Name(); name != "" {
478480
if out.Service == nil {
479481
out.Service = &modelpb.Service{}
@@ -485,6 +487,32 @@ func translateScopeMetadata(scope pcommon.InstrumentationScope, out *modelpb.APM
485487
}
486488
}
487489

490+
func translateLogScopeMetadata(scope pcommon.InstrumentationScope, out *modelpb.APMEvent) {
491+
translateScopeCommonMetadata(scope, out)
492+
if version := scope.Version(); version != "" {
493+
// If the version is set, we assume the name is an instrumentation library name
494+
translateScopeFrameworkMetadata(scope, out)
495+
} else {
496+
// If the version is not set, we assume the name is a logger name
497+
if name := scope.Name(); name != "" {
498+
if out.Log == nil {
499+
out.Log = &modelpb.Log{}
500+
}
501+
out.Log.Logger = name
502+
}
503+
}
504+
}
505+
506+
func translateSpanScopeMetadata(scope pcommon.InstrumentationScope, out *modelpb.APMEvent) {
507+
translateScopeCommonMetadata(scope, out)
508+
translateScopeFrameworkMetadata(scope, out)
509+
}
510+
511+
func translateMetricsScopeMetadata(scope pcommon.InstrumentationScope, out *modelpb.APMEvent) {
512+
translateScopeCommonMetadata(scope, out)
513+
translateScopeFrameworkMetadata(scope, out)
514+
}
515+
488516
func cleanServiceName(name string) string {
489517
return serviceNameInvalidRegexp.ReplaceAllString(truncate(name), "_")
490518
}

input/otlp/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (c *Consumer) handleScopeMetrics(
159159
for key, ms := range ms {
160160
event := baseEvent.CloneVT()
161161

162-
translateScopeMetadata(in.Scope(), event)
162+
translateMetricsScopeMetadata(in.Scope(), event)
163163

164164
event.Timestamp = modelpb.FromTime(key.timestamp.Add(timeDelta))
165165
metrs := make([]*modelpb.MetricsetSample, 0, len(ms.samples))

input/otlp/traces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func (c *Consumer) convertSpan(
178178
representativeCount := getRepresentativeCountFromTracestateHeader(otelSpan.TraceState().AsRaw())
179179
event := baseEvent.CloneVT()
180180

181-
translateScopeMetadata(otelLibrary, event)
181+
translateSpanScopeMetadata(otelLibrary, event)
182182

183183
initEventLabels(event)
184184
event.Timestamp = modelpb.FromTime(startTime.Add(timeDelta))

0 commit comments

Comments
 (0)