Skip to content

Commit

Permalink
Translate OTEL log's InstrumentationScope as logger name
Browse files Browse the repository at this point in the history
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 [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 <[email protected]>
  • Loading branch information
oldium committed Dec 18, 2024
1 parent c7b04dc commit ef4aef9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion input/otlp/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (c *Consumer) convertLogRecord(
event := baseEvent.CloneVT()
initEventLabels(event)

translateScopeMetadata(scope, event)
translateLogScopeMetadata(scope, event)

if record.Timestamp() == 0 {
event.Timestamp = modelpb.FromTime(record.ObservedTimestamp().AsTime().Add(timeDelta))
Expand Down
30 changes: 29 additions & 1 deletion input/otlp/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func translateResourceMetadata(resource pcommon.Resource, out *modelpb.APMEvent)
}
}

func translateScopeMetadata(scope pcommon.InstrumentationScope, out *modelpb.APMEvent) {
func translateScopeCommonMetadata(scope pcommon.InstrumentationScope, out *modelpb.APMEvent) {
scope.Attributes().Range(func(k string, v pcommon.Value) bool {
switch k {
// data_stream.*
Expand All @@ -473,7 +473,9 @@ func translateScopeMetadata(scope pcommon.InstrumentationScope, out *modelpb.APM
}
return true
})
}

func translateScopeFrameworkMetadata(scope pcommon.InstrumentationScope, out *modelpb.APMEvent) {
if name := scope.Name(); name != "" {
if out.Service == nil {
out.Service = &modelpb.Service{}
Expand All @@ -485,6 +487,32 @@ func translateScopeMetadata(scope pcommon.InstrumentationScope, out *modelpb.APM
}
}

func translateLogScopeMetadata(scope pcommon.InstrumentationScope, out *modelpb.APMEvent) {
translateScopeCommonMetadata(scope, out)
if version := scope.Version(); version != "" {
// If the version is set, we assume the name is an instrumentation library name
translateScopeFrameworkMetadata(scope, out)
} else {
// If the version is not set, we assume the name is a logger name
if name := scope.Name(); name != "" {
if out.Log == nil {
out.Log = &modelpb.Log{}
}
out.Log.Logger = name
}
}
}

func translateSpanScopeMetadata(scope pcommon.InstrumentationScope, out *modelpb.APMEvent) {
translateScopeCommonMetadata(scope, out)
translateScopeFrameworkMetadata(scope, out)
}

func translateMetricsScopeMetadata(scope pcommon.InstrumentationScope, out *modelpb.APMEvent) {
translateScopeCommonMetadata(scope, out)
translateScopeFrameworkMetadata(scope, out)
}

func cleanServiceName(name string) string {
return serviceNameInvalidRegexp.ReplaceAllString(truncate(name), "_")
}
Expand Down
2 changes: 1 addition & 1 deletion input/otlp/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (c *Consumer) handleScopeMetrics(
for key, ms := range ms {
event := baseEvent.CloneVT()

translateScopeMetadata(in.Scope(), event)
translateMetricsScopeMetadata(in.Scope(), event)

event.Timestamp = modelpb.FromTime(key.timestamp.Add(timeDelta))
metrs := make([]*modelpb.MetricsetSample, 0, len(ms.samples))
Expand Down
2 changes: 1 addition & 1 deletion input/otlp/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (c *Consumer) convertSpan(
representativeCount := getRepresentativeCountFromTracestateHeader(otelSpan.TraceState().AsRaw())
event := baseEvent.CloneVT()

translateScopeMetadata(otelLibrary, event)
translateSpanScopeMetadata(otelLibrary, event)

initEventLabels(event)
event.Timestamp = modelpb.FromTime(startTime.Add(timeDelta))
Expand Down

0 comments on commit ef4aef9

Please sign in to comment.