-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
scope.go
120 lines (108 loc) · 3.77 KB
/
scope.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal"
import (
"context"
"fmt"
"go.opentelemetry.io/collector/pdata/pcommon"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)
type InstrumentationScopeContext interface {
GetInstrumentationScope() pcommon.InstrumentationScope
}
func ScopePathGetSetter[K InstrumentationScopeContext](path ottl.Path[K]) (ottl.GetSetter[K], error) {
if path == nil {
return accessInstrumentationScope[K](), nil
}
switch path.Name() {
case "name":
return accessInstrumentationScopeName[K](), nil
case "version":
return accessInstrumentationScopeVersion[K](), nil
case "attributes":
mapKeys := path.Keys()
if mapKeys == nil {
return accessInstrumentationScopeAttributes[K](), nil
}
return accessInstrumentationScopeAttributesKey[K](mapKeys), nil
case "dropped_attributes_count":
return accessInstrumentationScopeDroppedAttributesCount[K](), nil
default:
return nil, fmt.Errorf("invalid scope path expression %v", path)
}
}
func accessInstrumentationScope[K InstrumentationScopeContext]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (any, error) {
return tCtx.GetInstrumentationScope(), nil
},
Setter: func(ctx context.Context, tCtx K, val any) error {
if newIl, ok := val.(pcommon.InstrumentationScope); ok {
newIl.CopyTo(tCtx.GetInstrumentationScope())
}
return nil
},
}
}
func accessInstrumentationScopeAttributes[K InstrumentationScopeContext]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (any, error) {
return tCtx.GetInstrumentationScope().Attributes(), nil
},
Setter: func(ctx context.Context, tCtx K, val any) error {
if attrs, ok := val.(pcommon.Map); ok {
attrs.CopyTo(tCtx.GetInstrumentationScope().Attributes())
}
return nil
},
}
}
func accessInstrumentationScopeAttributesKey[K InstrumentationScopeContext](keys []ottl.Key[K]) ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (any, error) {
return GetMapValue[K](ctx, tCtx, tCtx.GetInstrumentationScope().Attributes(), keys)
},
Setter: func(ctx context.Context, tCtx K, val any) error {
return SetMapValue[K](ctx, tCtx, tCtx.GetInstrumentationScope().Attributes(), keys, val)
},
}
}
func accessInstrumentationScopeName[K InstrumentationScopeContext]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (any, error) {
return tCtx.GetInstrumentationScope().Name(), nil
},
Setter: func(ctx context.Context, tCtx K, val any) error {
if str, ok := val.(string); ok {
tCtx.GetInstrumentationScope().SetName(str)
}
return nil
},
}
}
func accessInstrumentationScopeVersion[K InstrumentationScopeContext]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (any, error) {
return tCtx.GetInstrumentationScope().Version(), nil
},
Setter: func(ctx context.Context, tCtx K, val any) error {
if str, ok := val.(string); ok {
tCtx.GetInstrumentationScope().SetVersion(str)
}
return nil
},
}
}
func accessInstrumentationScopeDroppedAttributesCount[K InstrumentationScopeContext]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (any, error) {
return int64(tCtx.GetInstrumentationScope().DroppedAttributesCount()), nil
},
Setter: func(ctx context.Context, tCtx K, val any) error {
if i, ok := val.(int64); ok {
tCtx.GetInstrumentationScope().SetDroppedAttributesCount(uint32(i))
}
return nil
},
}
}