-
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.
[pkg/telemetryquerylanguage] Remove duplicate code between signals
Signed-off-by: Bogdan Drutu <[email protected]>
- Loading branch information
1 parent
ecca6a8
commit 6f228af
Showing
11 changed files
with
779 additions
and
976 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
pkg/telemetryquerylanguage/contexts/internal/tqlcommon/map.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,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
91
pkg/telemetryquerylanguage/contexts/internal/tqlcommon/resource.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,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)) | ||
} | ||
}, | ||
} | ||
} |
Oops, something went wrong.