Skip to content

Commit

Permalink
[pkg/telemetryquerylanguage] Remove duplicate code between signals
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Aug 26, 2022
1 parent ecca6a8 commit 6f228af
Show file tree
Hide file tree
Showing 11 changed files with 779 additions and 976 deletions.
40 changes: 40 additions & 0 deletions pkg/telemetryquerylanguage/contexts/internal/tqlcommon/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tqlcommon // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/telemetryquerylanguage/contexts/internal/tqlcommon"

import (
"go.opentelemetry.io/collector/pdata/pcommon"
)

func GetMapValue(attrs pcommon.Map, mapKey string) interface{} {
val, ok := attrs.Get(mapKey)
if !ok {
return nil
}
return GetValue(val)
}

func SetMapValue(attrs pcommon.Map, mapKey string, val interface{}) {
var value pcommon.Value
switch val.(type) {
case []string, []bool, []int64, []float64, [][]byte:
value = pcommon.NewValueSlice()
default:
value = pcommon.NewValueEmpty()
}

SetValue(value, val)
attrs.Upsert(mapKey, value)
}
91 changes: 91 additions & 0 deletions pkg/telemetryquerylanguage/contexts/internal/tqlcommon/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tqlcommon // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/telemetryquerylanguage/contexts/internal/tqlcommon"

import (
"fmt"

"go.opentelemetry.io/collector/pdata/pcommon"

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

func ResourcePathGetSetter(path []tql.Field) (tql.GetSetter, error) {
if len(path) == 0 {
return accessResource(), nil
}
switch path[0].Name {
case "attributes":
mapKey := path[0].MapKey
if mapKey == nil {
return accessResourceAttributes(), nil
}
return accessResourceAttributesKey(mapKey), nil
case "dropped_attributes_count":
return accessDroppedAttributesCount(), nil
}

return nil, fmt.Errorf("invalid resource path expression %v", path)
}

func accessResource() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return ctx.GetResource()
},
Setter: func(ctx tql.TransformContext, val interface{}) {
if newRes, ok := val.(pcommon.Resource); ok {
newRes.CopyTo(ctx.GetResource())
}
},
}
}

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

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

func accessDroppedAttributesCount() tql.StandardGetSetter {
return tql.StandardGetSetter{
Getter: func(ctx tql.TransformContext) interface{} {
return int64(ctx.GetResource().DroppedAttributesCount())
},
Setter: func(ctx tql.TransformContext, val interface{}) {
if i, ok := val.(int64); ok {
ctx.GetResource().SetDroppedAttributesCount(uint32(i))
}
},
}
}
Loading

0 comments on commit 6f228af

Please sign in to comment.