-
Notifications
You must be signed in to change notification settings - Fork 486
/
tail_sampling.go
108 lines (88 loc) · 3.38 KB
/
tail_sampling.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
// Package tail_sampling provides an otelcol.processor.tail_sampling component.
package tail_sampling
import (
"fmt"
"time"
"github.com/grafana/agent/component"
"github.com/grafana/agent/component/otelcol"
"github.com/grafana/agent/component/otelcol/processor"
"github.com/grafana/agent/pkg/river"
tsp "github.com/open-telemetry/opentelemetry-collector-contrib/processor/tailsamplingprocessor"
otelcomponent "go.opentelemetry.io/collector/component"
otelconfig "go.opentelemetry.io/collector/config"
)
func init() {
component.Register(component.Registration{
Name: "otelcol.processor.tail_sampling",
Args: Arguments{},
Exports: otelcol.ConsumerExports{},
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
fact := tsp.NewFactory()
return processor.New(opts, fact, args.(Arguments))
},
})
}
// Arguments configures the otelcol.processor.tail_sampling component.
type Arguments struct {
PolicyCfgs []PolicyCfg `river:"policy,block"`
DecisionWait time.Duration `river:"decision_wait,attr,optional"`
NumTraces uint64 `river:"num_traces,attr,optional"`
ExpectedNewTracesPerSec uint64 `river:"expected_new_traces_per_sec,attr,optional"`
// Output configures where to send processed data. Required.
Output *otelcol.ConsumerArguments `river:"output,block"`
}
var (
_ processor.Arguments = Arguments{}
_ river.Unmarshaler = (*Arguments)(nil)
)
// DefaultArguments holds default settings for Arguments.
var DefaultArguments = Arguments{
DecisionWait: 30 * time.Second,
NumTraces: 50000,
ExpectedNewTracesPerSec: 0,
}
// UnmarshalRiver implements river.Unmarshaler. It applies defaults to args and
// validates settings provided by the user.
func (args *Arguments) UnmarshalRiver(f func(interface{}) error) error {
*args = DefaultArguments
type arguments Arguments
if err := f((*arguments)(args)); err != nil {
return err
}
if args.DecisionWait.Milliseconds() <= 0 {
return fmt.Errorf("decision_wait must be greater than zero")
}
if args.NumTraces <= 0 {
return fmt.Errorf("num_traces must be greater than zero")
}
return nil
}
// Convert implements processor.Arguments.
func (args Arguments) Convert() (otelconfig.Processor, error) {
// TODO: Get rid of mapstructure once tailsamplingprocessor.Config has all public types
var otelConfig tsp.Config
var otelPolicyCfgs []tsp.PolicyCfg
for _, policyCfg := range args.PolicyCfgs {
otelPolicyCfgs = append(otelPolicyCfgs, policyCfg.Convert())
}
mustDecodeMapStructure(map[string]interface{}{
"decision_wait": args.DecisionWait,
"num_traces": args.NumTraces,
"expected_new_traces_per_sec": args.ExpectedNewTracesPerSec,
"policies": otelPolicyCfgs,
}, &otelConfig)
otelConfig.ProcessorSettings = otelconfig.NewProcessorSettings(otelconfig.NewComponentID("tail_sampling"))
return &otelConfig, nil
}
// Extensions implements processor.Arguments.
func (args Arguments) Extensions() map[otelconfig.ComponentID]otelcomponent.Extension {
return nil
}
// Exporters implements processor.Arguments.
func (args Arguments) Exporters() map[otelconfig.DataType]map[otelconfig.ComponentID]otelcomponent.Exporter {
return nil
}
// NextConsumers implements processor.Arguments.
func (args Arguments) NextConsumers() *otelcol.ConsumerArguments {
return args.Output
}