Skip to content

Commit edbad98

Browse files
authored
Parse host.ip from OTel metadata (#314)
1 parent 5179047 commit edbad98

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

input/otlp/metadata.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"strings"
2525

2626
"go.opentelemetry.io/collector/pdata/pcommon"
27-
semconv "go.opentelemetry.io/collector/semconv/v1.5.0"
27+
semconv "go.opentelemetry.io/collector/semconv/v1.25.0"
2828

2929
"github.com/elastic/apm-data/model/modelpb"
3030
)
@@ -131,7 +131,7 @@ func translateResourceMetadata(resource pcommon.Resource, out *modelpb.APMEvent)
131131
out.Container = modelpb.ContainerFromVTPool()
132132
}
133133
out.Container.ImageName = truncate(v.Str())
134-
case semconv.AttributeContainerImageTag:
134+
case "container.image.tag":
135135
if out.Container == nil {
136136
out.Container = modelpb.ContainerFromVTPool()
137137
}
@@ -185,6 +185,17 @@ func translateResourceMetadata(resource pcommon.Resource, out *modelpb.APMEvent)
185185
out.Host = modelpb.HostFromVTPool()
186186
}
187187
out.Host.Architecture = truncate(v.Str())
188+
case semconv.AttributeHostIP:
189+
if out.Host == nil {
190+
out.Host = modelpb.HostFromVTPool()
191+
}
192+
out.Host.Ip = pSliceToType[*modelpb.IP](v.Slice(), func(v pcommon.Value) (*modelpb.IP, bool) {
193+
ip, err := modelpb.ParseIP(v.Str())
194+
if err != nil {
195+
return nil, false
196+
}
197+
return ip, true
198+
})
188199

189200
// process.*
190201
case semconv.AttributeProcessPID:
@@ -479,11 +490,19 @@ func ifaceAttributeValue(v pcommon.Value) interface{} {
479490
}
480491

481492
func ifaceAttributeValueSlice(slice pcommon.Slice) []interface{} {
482-
values := make([]interface{}, slice.Len())
483-
for i := range values {
484-
values[i] = ifaceAttributeValue(slice.At(i))
493+
return pSliceToType[interface{}](slice, func(v pcommon.Value) (interface{}, bool) {
494+
return ifaceAttributeValue(v), true
495+
})
496+
}
497+
498+
func pSliceToType[T any](slice pcommon.Slice, f func(pcommon.Value) (T, bool)) []T {
499+
result := make([]T, 0, slice.Len())
500+
for i := 0; i < slice.Len(); i++ {
501+
if v, ok := f(slice.At(i)); ok {
502+
result = append(result, v)
503+
}
485504
}
486-
return values
505+
return result
487506
}
488507

489508
// initEventLabels initializes an event-specific labels from an event.

input/otlp/metadata_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ func TestResourceConventions(t *testing.T) {
202202
"host.id": "host_id",
203203
"host.type": "host_type",
204204
"host.arch": "host_arch",
205+
"host.ip": []interface{}{"10.244.0.1", "172.19.0.2", "fc00:f853:ccd:e793::2"},
205206
},
206207
expected: &modelpb.APMEvent{
207208
Agent: &defaultAgent,
@@ -211,6 +212,13 @@ func TestResourceConventions(t *testing.T) {
211212
Id: "host_id",
212213
Type: "host_type",
213214
Architecture: "host_arch",
215+
Ip: func() []*modelpb.IP {
216+
ips := make([]*modelpb.IP, 3)
217+
ips[0] = modelpb.MustParseIP("10.244.0.1")
218+
ips[1] = modelpb.MustParseIP("172.19.0.2")
219+
ips[2] = modelpb.MustParseIP("fc00:f853:ccd:e793::2")
220+
return ips
221+
}(),
214222
},
215223
},
216224
},

0 commit comments

Comments
 (0)