Skip to content

Commit

Permalink
set event labels as default
Browse files Browse the repository at this point in the history
  • Loading branch information
rubvs committed Aug 20, 2024
1 parent 58ccdeb commit de289eb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
28 changes: 9 additions & 19 deletions input/otlp/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,21 @@ func translateResourceMetadata(resource pcommon.Resource, out *modelpb.APMEvent)
out.Host = modelpb.HostFromVTPool()
}
out.Host.Architecture = truncate(v.Str())
// TODO: Don't think this is being tested.
case semconv.AttributeHostIP:
if out.Host == nil {
out.Host = modelpb.HostFromVTPool()
}
out.Host.Ip = pSliceToType[*modelpb.IP](v.Slice(), func(v pcommon.Value) (*modelpb.IP, bool) {
ip, err := modelpb.ParseIP(v.Str())
slice := v.Slice()
result := make([]*modelpb.IP, 0, slice.Len())
for i := 0; i < slice.Len(); i++ {
ip, err := modelpb.ParseIP(slice.At(i).Str())
if err != nil {
return nil, false
panic(err)
}
return ip, true
})
result = append(result, ip)
}
out.Host.Ip = result

// process.*
case semconv.AttributeProcessPID:
Expand Down Expand Up @@ -320,7 +324,6 @@ func translateResourceMetadata(resource pcommon.Resource, out *modelpb.APMEvent)
out.DataStream = modelpb.DataStreamFromVTPool()
}
out.DataStream.Namespace = v.Str()

default:
if out.Labels == nil {
out.Labels = make(modelpb.Labels)
Expand Down Expand Up @@ -482,24 +485,13 @@ func cleanServiceName(name string) string {
return serviceNameInvalidRegexp.ReplaceAllString(truncate(name), "_")
}

func pSliceToType[T any](slice pcommon.Slice, f func(pcommon.Value) (T, bool)) []T {
result := make([]T, 0, slice.Len())
for i := 0; i < slice.Len(); i++ {
if v, ok := f(slice.At(i)); ok {
result = append(result, v)
}
}
return result
}

// initEventLabels initializes an event-specific labels from an event.
func initEventLabels(e *modelpb.APMEvent) {
e.Labels = modelpb.Labels(e.Labels).Clone()
e.NumericLabels = modelpb.NumericLabels(e.NumericLabels).Clone()
}

func setLabel(key string, event *modelpb.APMEvent, v pcommon.Value) {

switch v.Type() {
case pcommon.ValueTypeStr:
modelpb.Labels(event.Labels).Set(key, truncate(v.Str()))
Expand All @@ -517,10 +509,8 @@ func setLabel(key string, event *modelpb.APMEvent, v pcommon.Value) {
switch s.At(0).Type() {
case pcommon.ValueTypeStr:
result := make([]string, 0, s.Len())
fmt.Println("len", s.Len())
for i := 0; i < s.Len(); i++ {
r := s.At(i)
fmt.Println("r:", r.Str())
if r.Type() == pcommon.ValueTypeStr {
result = append(result, r.Str())
}
Expand Down
9 changes: 3 additions & 6 deletions input/otlp/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,6 @@ func TranslateSpan(spanKind ptrace.SpanKind, attributes pcommon.Map, event *mode

k := replaceDots(kDots)
switch v.Type() {
case pcommon.ValueTypeSlice:
setLabel(k, event, v)
case pcommon.ValueTypeBool:
switch kDots {
case semconv.AttributeMessagingTempDestination:
Expand All @@ -652,8 +650,6 @@ func TranslateSpan(spanKind ptrace.SpanKind, attributes pcommon.Map, event *mode
default:
setLabel(k, event, v)
}
case pcommon.ValueTypeDouble:
setLabel(k, event, v)
case pcommon.ValueTypeInt:
switch kDots {
case "http.status_code", attributeHttpResponseStatusCode:
Expand Down Expand Up @@ -836,10 +832,11 @@ func TranslateSpan(spanKind ptrace.SpanKind, attributes pcommon.Map, event *mode
event.DataStream = modelpb.DataStreamFromVTPool()
}
event.DataStream.Namespace = stringval

default:
modelpb.Labels(event.Labels).Set(k, stringval)
setLabel(k, event, v)
}
default:
setLabel(k, event, v)
}
return true
})
Expand Down

0 comments on commit de289eb

Please sign in to comment.