forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.go
129 lines (109 loc) · 3.84 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
121
122
123
124
125
126
127
128
129
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package ottlscope // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlscope"
import (
"context"
"fmt"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal"
)
var _ internal.ResourceContext = TransformContext{}
var _ internal.InstrumentationScopeContext = TransformContext{}
type TransformContext struct {
instrumentationScope pcommon.InstrumentationScope
resource pcommon.Resource
cache pcommon.Map
}
type Option func(*ottl.Parser[TransformContext])
func NewTransformContext(instrumentationScope pcommon.InstrumentationScope, resource pcommon.Resource) TransformContext {
return TransformContext{
instrumentationScope: instrumentationScope,
resource: resource,
cache: pcommon.NewMap(),
}
}
func (tCtx TransformContext) GetInstrumentationScope() pcommon.InstrumentationScope {
return tCtx.instrumentationScope
}
func (tCtx TransformContext) GetResource() pcommon.Resource {
return tCtx.resource
}
func (tCtx TransformContext) getCache() pcommon.Map {
return tCtx.cache
}
func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySettings component.TelemetrySettings, options ...Option) (ottl.Parser[TransformContext], error) {
p, err := ottl.NewParser[TransformContext](
functions,
parsePath,
telemetrySettings,
ottl.WithEnumParser[TransformContext](parseEnum),
)
if err != nil {
return ottl.Parser[TransformContext]{}, err
}
for _, opt := range options {
opt(&p)
}
return p, nil
}
type StatementsOption func(*ottl.Statements[TransformContext])
func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
return func(s *ottl.Statements[TransformContext]) {
ottl.WithErrorMode[TransformContext](errorMode)(s)
}
}
func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
s := ottl.NewStatements(statements, telemetrySettings)
for _, op := range options {
op(&s)
}
return s
}
func parseEnum(_ *ottl.EnumSymbol) (*ottl.Enum, error) {
return nil, fmt.Errorf("instrumentation scope context does not provide Enum support")
}
func parsePath(val *ottl.Path) (ottl.GetSetter[TransformContext], error) {
if val != nil && len(val.Fields) > 0 {
return newPathGetSetter(val.Fields)
}
return nil, fmt.Errorf("bad path %v", val)
}
func newPathGetSetter(path []ottl.Field) (ottl.GetSetter[TransformContext], error) {
switch path[0].Name {
case "cache":
mapKey := path[0].Keys
if mapKey == nil {
return accessCache(), nil
}
return accessCacheKey(mapKey), nil
case "resource":
return internal.ResourcePathGetSetter[TransformContext](path[1:])
default:
return internal.ScopePathGetSetter[TransformContext](path)
}
}
func accessCache() ottl.StandardGetSetter[TransformContext] {
return ottl.StandardGetSetter[TransformContext]{
Getter: func(ctx context.Context, tCtx TransformContext) (interface{}, error) {
return tCtx.getCache(), nil
},
Setter: func(ctx context.Context, tCtx TransformContext, val interface{}) error {
if m, ok := val.(pcommon.Map); ok {
m.CopyTo(tCtx.getCache())
}
return nil
},
}
}
func accessCacheKey(keys []ottl.Key) ottl.StandardGetSetter[TransformContext] {
return ottl.StandardGetSetter[TransformContext]{
Getter: func(ctx context.Context, tCtx TransformContext) (interface{}, error) {
return internal.GetMapValue(tCtx.getCache(), keys)
},
Setter: func(ctx context.Context, tCtx TransformContext, val interface{}) error {
return internal.SetMapValue(tCtx.getCache(), keys, val)
},
}
}