Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/telemetryquerylanguage] Add TQL mapping functions for InstrumentationScope attributes #13640

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pkg/telemetryquerylanguage/contexts/internal/tqlcommon/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ func ScopePathGetSetter(path []tql.Field) (tql.GetSetter, error) {
return accessInstrumentationScopeName(), nil
case "version":
return accessInstrumentationScopeVersion(), nil
case "attributes":
mapKey := path[0].MapKey
if mapKey == nil {
return accessInstrumentationScopeAttributes(), nil
}
return accessInstrumentationScopeAttributesKey(mapKey), nil
}

return nil, fmt.Errorf("invalid scope path expression %v", path)
Expand All @@ -50,6 +56,30 @@ func accessInstrumentationScope() tql.StandardGetSetter {
}
}

func accessInstrumentationScopeAttributes() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetInstrumentationScope().Attributes()
},
Setter: func(ctx tql.TransformContext, val interface{}) {
if attrs, ok := val.(pcommon.Map); ok {
attrs.CopyTo(ctx.GetInstrumentationScope().Attributes())
}
},
}
}

func accessInstrumentationScopeAttributesKey(mapKey *string) tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return GetMapValue(ctx.GetInstrumentationScope().Attributes(), *mapKey)
},
Setter: func(ctx tql.TransformContext, val interface{}) {
SetMapValue(ctx.GetInstrumentationScope().Attributes(), *mapKey, val)
},
}
}

func accessInstrumentationScopeName() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
Expand Down
207 changes: 205 additions & 2 deletions pkg/telemetryquerylanguage/contexts/internal/tqlcommon/scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/telemetryquerylanguage/tql"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/telemetryquerylanguage/tql/tqltest"
)

func TestScopePathGetSetter(t *testing.T) {
refIS := createInstrumentationScope()

newAttrs := pcommon.NewMap()
newAttrs.UpsertString("hello", "world")
tests := []struct {
name string
path []tql.Field
Expand All @@ -50,9 +53,9 @@ func TestScopePathGetSetter(t *testing.T) {
},
},
orig: refIS.Name(),
newVal: "park",
newVal: "newname",
modified: func(is pcommon.InstrumentationScope) {
is.SetName("park")
is.SetName("newname")
},
},
{
Expand All @@ -68,6 +71,179 @@ func TestScopePathGetSetter(t *testing.T) {
is.SetVersion("next")
},
},
{
name: "attributes",
path: []tql.Field{
{
Name: "attributes",
},
},
orig: refIS.Attributes(),
newVal: newAttrs,
modified: func(is pcommon.InstrumentationScope) {
newAttrs.CopyTo(is.Attributes())
},
},
{
name: "attributes string",
path: []tql.Field{
{
Name: "attributes",
MapKey: tqltest.Strp("str"),
},
},
orig: "val",
newVal: "newVal",
modified: func(is pcommon.InstrumentationScope) {
is.Attributes().UpsertString("str", "newVal")
},
},
{
name: "attributes bool",
path: []tql.Field{
{
Name: "attributes",
MapKey: tqltest.Strp("bool"),
},
},
orig: true,
newVal: false,
modified: func(is pcommon.InstrumentationScope) {
is.Attributes().UpsertBool("bool", false)
},
},
{
name: "attributes int",
path: []tql.Field{
{
Name: "attributes",
MapKey: tqltest.Strp("int"),
},
},
orig: int64(10),
newVal: int64(20),
modified: func(is pcommon.InstrumentationScope) {
is.Attributes().UpsertInt("int", 20)
},
},
{
name: "attributes float",
path: []tql.Field{
{
Name: "attributes",
MapKey: tqltest.Strp("double"),
},
},
orig: 1.2,
newVal: 2.4,
modified: func(is pcommon.InstrumentationScope) {
is.Attributes().UpsertDouble("double", 2.4)
},
},
{
name: "attributes bytes",
path: []tql.Field{
{
Name: "attributes",
MapKey: tqltest.Strp("bytes"),
},
},
orig: []byte{1, 3, 2},
newVal: []byte{2, 3, 4},
modified: func(is pcommon.InstrumentationScope) {
is.Attributes().UpsertBytes("bytes", pcommon.NewImmutableByteSlice([]byte{2, 3, 4}))
},
},
{
name: "attributes array string",
path: []tql.Field{
{
Name: "attributes",
MapKey: tqltest.Strp("arr_str"),
},
},
orig: func() pcommon.Slice {
val, _ := refIS.Attributes().Get("arr_str")
return val.SliceVal()
}(),
newVal: []string{"new"},
modified: func(is pcommon.InstrumentationScope) {
newArr := is.Attributes().UpsertEmptySlice("arr_str")
newArr.AppendEmpty().SetStringVal("new")
},
},
{
name: "attributes array bool",
path: []tql.Field{
{
Name: "attributes",
MapKey: tqltest.Strp("arr_bool"),
},
},
orig: func() pcommon.Slice {
val, _ := refIS.Attributes().Get("arr_bool")
return val.SliceVal()
}(),
newVal: []bool{false},
modified: func(is pcommon.InstrumentationScope) {
newArr := is.Attributes().UpsertEmptySlice("arr_bool")
newArr.AppendEmpty().SetBoolVal(false)
},
},
{
name: "attributes array int",
path: []tql.Field{
{
Name: "attributes",
MapKey: tqltest.Strp("arr_int"),
},
},
orig: func() pcommon.Slice {
val, _ := refIS.Attributes().Get("arr_int")
return val.SliceVal()
}(),
newVal: []int64{20},
modified: func(is pcommon.InstrumentationScope) {
newArr := is.Attributes().UpsertEmptySlice("arr_int")
newArr.AppendEmpty().SetIntVal(20)
},
},
{
name: "attributes array float",
path: []tql.Field{
{
Name: "attributes",
MapKey: tqltest.Strp("arr_float"),
},
},
orig: func() pcommon.Slice {
val, _ := refIS.Attributes().Get("arr_float")
return val.SliceVal()
}(),
newVal: []float64{2.0},
modified: func(is pcommon.InstrumentationScope) {
newArr := is.Attributes().UpsertEmptySlice("arr_float")
newArr.AppendEmpty().SetDoubleVal(2.0)
},
},
{
name: "attributes array bytes",
path: []tql.Field{
{
Name: "attributes",
MapKey: tqltest.Strp("arr_bytes"),
},
},
orig: func() pcommon.Slice {
val, _ := refIS.Attributes().Get("arr_bytes")
return val.SliceVal()
}(),
newVal: [][]byte{{9, 6, 4}},
modified: func(is pcommon.InstrumentationScope) {
newArr := is.Attributes().UpsertEmptySlice("arr_bytes")
newArr.AppendEmpty().SetBytesVal(pcommon.NewImmutableByteSlice([]byte{9, 6, 4}))
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -93,6 +269,33 @@ func createInstrumentationScope() pcommon.InstrumentationScope {
is := pcommon.NewInstrumentationScope()
is.SetName("library")
is.SetVersion("version")

is.Attributes().UpsertString("str", "val")
is.Attributes().UpsertBool("bool", true)
is.Attributes().UpsertInt("int", 10)
is.Attributes().UpsertDouble("double", 1.2)
is.Attributes().UpsertBytes("bytes", pcommon.NewImmutableByteSlice([]byte{1, 3, 2}))

arrStr := is.Attributes().UpsertEmptySlice("arr_str")
arrStr.AppendEmpty().SetStringVal("one")
arrStr.AppendEmpty().SetStringVal("two")

arrBool := is.Attributes().UpsertEmptySlice("arr_bool")
arrBool.AppendEmpty().SetBoolVal(true)
arrBool.AppendEmpty().SetBoolVal(false)

arrInt := is.Attributes().UpsertEmptySlice("arr_int")
arrInt.AppendEmpty().SetIntVal(2)
arrInt.AppendEmpty().SetIntVal(3)

arrFloat := is.Attributes().UpsertEmptySlice("arr_float")
arrFloat.AppendEmpty().SetDoubleVal(1.0)
arrFloat.AppendEmpty().SetDoubleVal(2.0)

arrBytes := is.Attributes().UpsertEmptySlice("arr_bytes")
arrBytes.AppendEmpty().SetBytesVal(pcommon.NewImmutableByteSlice([]byte{1, 2, 3}))
arrBytes.AppendEmpty().SetBytesVal(pcommon.NewImmutableByteSlice([]byte{2, 3, 4}))

return is
}

Expand Down
26 changes: 14 additions & 12 deletions pkg/telemetryquerylanguage/contexts/tqllogs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ In general, the Logs Context supports accessing pdata using the field names from

The following fields are the exception.

| path | field accessed | type |
|-------------------------------|-----------------------------------------------------------------|-------------------------------------------------------------------------|
| resource | resource of the log being processed | pcommon.Resource |
| resource.attributes | resource attributes of the log being processed | pcommon.Map |
| resource.attributes\[""\] | the value of the resource attribute of the log being processed | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| instrumentation_scope | instrumentation scope of the log being processed | pcommon.InstrumentationScope |
| instrumentation_scope.name | name of the instrumentation scope of the log being processed | string |
| instrumentation_scope.version | version of the instrumentation scope of the log being processed | string |
| attributes | attributes of the log being processed | pcommon.Map |
| attributes\[""\] | the value of the attribute of the log being processed | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| trace_id.string | a string representation of the trace id | string |
| span_id.string | a string representation of the span id | string |
| path | field accessed | type |
|----------------------------------------|------------------------------------------------------------------------------------|-------------------------------------------------------------------------|
| resource | resource of the log being processed | pcommon.Resource |
| resource.attributes | resource attributes of the log being processed | pcommon.Map |
| resource.attributes\[""\] | the value of the resource attribute of the log being processed | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| instrumentation_scope | instrumentation scope of the log being processed | pcommon.InstrumentationScope |
| instrumentation_scope.name | name of the instrumentation scope of the log being processed | string |
| instrumentation_scope.version | version of the instrumentation scope of the log being processed | string |
| instrumentation_scope.attributes | instrumentation scope attributes of the data point being processed | pcommon.Map |
| instrumentation_scope.attributes\[""\] | the value of the instrumentation scope attribute of the data point being processed | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| attributes | attributes of the log being processed | pcommon.Map |
| attributes\[""\] | the value of the attribute of the log being processed | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| trace_id.string | a string representation of the trace id | string |
| span_id.string | a string representation of the span id | string |

## Enums

Expand Down
Loading