-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor otlp model specific logic into processors
- Loading branch information
Showing
9 changed files
with
617 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package objmodel | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
) | ||
|
||
// Map processes a pcommon.Map into key value pairs and adds them to Elasticsearch | ||
// document. Only map types are recursively processed. Map also allows remapping | ||
// keys by passing in a key remapper. Any key remapped via the key remapper to | ||
// an empty string is not added to the resulting document. | ||
type Map struct { | ||
pcommon.Map | ||
|
||
keyRemapper func(string) string | ||
} | ||
|
||
var emptyRemapper = func(k string) string { | ||
return k | ||
} | ||
|
||
// NewMapProcessor creates a new processor of processing pcommon.Map. | ||
func NewMapProcessor(m pcommon.Map, remapper func(string) string) Map { | ||
if remapper == nil { | ||
remapper = emptyRemapper | ||
} | ||
return Map{Map: m, keyRemapper: remapper} | ||
} | ||
|
||
// Len gives the number of entries that will be added to the Document. This | ||
// is an approximate figure as it doesn't count for entries removed via remapper. | ||
func (m Map) Len() int { | ||
return lenMap(m.Map) | ||
} | ||
|
||
// Process iterates over the map and adds the required fields into the document. | ||
// The keys could be remapped to another key as per the remapper function. | ||
func (m Map) Process(doc *Document, key string) { | ||
processMap(m.Map, m.keyRemapper, doc, key) | ||
} | ||
|
||
func lenMap(m pcommon.Map) int { | ||
var count int | ||
m.Range(func(_ string, v pcommon.Value) bool { | ||
switch v.Type() { | ||
case pcommon.ValueTypeEmpty: | ||
// Only maps are expanded in the document | ||
case pcommon.ValueTypeMap: | ||
count += lenMap(v.Map()) | ||
default: | ||
count += 1 | ||
} | ||
return true | ||
}) | ||
return count | ||
} | ||
|
||
func processMap( | ||
m pcommon.Map, | ||
keyRemapper func(string) string, | ||
doc *Document, | ||
key string, | ||
) { | ||
m.Range(func(k string, v pcommon.Value) bool { | ||
k = keyRemapper(flattenKey(key, k)) | ||
if k == "" { | ||
// any empty value for a remapped metric key | ||
// will be skipped | ||
return true | ||
} | ||
|
||
switch v.Type() { | ||
case pcommon.ValueTypeMap: | ||
processMap(v.Map(), keyRemapper, doc, k) | ||
default: | ||
doc.Add(k, ValueFromAttribute(v)) | ||
} | ||
return true | ||
}) | ||
} |
116 changes: 116 additions & 0 deletions
116
exporter/elasticsearchexporter/internal/objmodel/map_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package objmodel | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
) | ||
|
||
func TestMap(t *testing.T) { | ||
key := "test" | ||
for _, tc := range []struct { | ||
name string | ||
m pcommon.Map | ||
keyRemapper func(string) string | ||
expectedLen int | ||
expectedDoc Document | ||
}{ | ||
{ | ||
name: "empty", | ||
m: pcommon.NewMap(), | ||
expectedDoc: Document{}, | ||
}, | ||
{ | ||
name: "map", | ||
m: func() pcommon.Map { | ||
m := pcommon.NewMap() | ||
m.FromRaw(map[string]interface{}{ | ||
"str": "abc", | ||
"num": 1.1, | ||
"bool": true, | ||
"slice": []any{1, 2.1}, | ||
"map": map[string]any{ | ||
"str": "def", | ||
"num": 2, | ||
"bool": false, | ||
"slice": []any{3, 4}, | ||
}, | ||
}) | ||
return m | ||
}(), | ||
expectedLen: 8, | ||
expectedDoc: func() Document { | ||
var doc Document | ||
doc.Add(key+".str", StringValue("abc")) | ||
doc.Add(key+".num", DoubleValue(1.1)) | ||
doc.Add(key+".bool", BoolValue(true)) | ||
doc.Add(key+".slice", ArrValue(IntValue(1), DoubleValue(2.1))) | ||
doc.Add(key+".map.str", StringValue("def")) | ||
doc.Add(key+".map.num", IntValue(2)) | ||
doc.Add(key+".map.bool", BoolValue(false)) | ||
doc.Add(key+".map.slice", ArrValue(IntValue(3), IntValue(4))) | ||
|
||
doc.Sort() | ||
return doc | ||
}(), | ||
}, | ||
{ | ||
name: "map_with_remapper", | ||
m: func() pcommon.Map { | ||
m := pcommon.NewMap() | ||
m.FromRaw(map[string]interface{}{ | ||
"str": "abc", | ||
"num": 1.1, | ||
"bool": true, | ||
"slice": []any{1, 2.1}, | ||
"map": map[string]any{ | ||
"str": "def", | ||
"num": 2, | ||
"bool": false, | ||
"slice": []any{3, 4}, | ||
}, | ||
}) | ||
return m | ||
}(), | ||
keyRemapper: func(k string) string { | ||
switch k { | ||
case "test.str": | ||
return "" // should be ignored | ||
case "test.map.num": | ||
return "k.map.num" | ||
} | ||
return k | ||
}, | ||
// expected len is approximate and doesn't accout for entries | ||
// removed via the key remapper | ||
expectedLen: 8, | ||
expectedDoc: func() Document { | ||
var doc Document | ||
doc.Add(key+".num", DoubleValue(1.1)) | ||
doc.Add(key+".bool", BoolValue(true)) | ||
doc.Add(key+".slice", ArrValue(IntValue(1), DoubleValue(2.1))) | ||
doc.Add(key+".map.str", StringValue("def")) | ||
doc.Add("k.map.num", IntValue(2)) | ||
doc.Add(key+".map.bool", BoolValue(false)) | ||
doc.Add(key+".map.slice", ArrValue(IntValue(3), IntValue(4))) | ||
|
||
doc.Sort() | ||
return doc | ||
}(), | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
var actual Document | ||
p := NewMapProcessor(tc.m, tc.keyRemapper) | ||
p.Process(&actual, key) | ||
actual.Sort() | ||
|
||
assert.Equal(t, tc.expectedLen, p.Len()) | ||
assert.Equal(t, tc.expectedDoc, actual) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.