forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfactory.go
86 lines (73 loc) · 2.84 KB
/
factory.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package spanprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/spanprocessor"
import (
"context"
"errors"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/processorhelper"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/spanprocessor/internal/metadata"
)
const (
// status represents span status
statusCodeUnset = "Unset"
statusCodeError = "Error"
statusCodeOk = "Ok"
)
var processorCapabilities = consumer.Capabilities{MutatesData: true}
// errMissingRequiredField is returned when a required field in the config
// is not specified.
// TODO https://github.com/open-telemetry/opentelemetry-collector/issues/215
//
// Move this to the error package that allows for span name and field to be specified.
var (
errMissingRequiredField = errors.New("error creating \"span\" processor: either \"from_attributes\" or \"to_attributes\" must be specified in \"name:\" or \"setStatus\" must be specified")
errIncorrectStatusCode = errors.New("error creating \"span\" processor: \"status\" must have specified \"code\" as \"Ok\" or \"Error\" or \"Unset\"")
errIncorrectStatusDescription = errors.New("error creating \"span\" processor: \"description\" can be specified only for \"code\" \"Error\"")
)
// NewFactory returns a new factory for the Span processor.
func NewFactory() processor.Factory {
return processor.NewFactory(
metadata.Type,
createDefaultConfig,
processor.WithTraces(createTracesProcessor, metadata.TracesStability))
}
func createDefaultConfig() component.Config {
return &Config{}
}
func createTracesProcessor(
ctx context.Context,
set processor.Settings,
cfg component.Config,
nextConsumer consumer.Traces,
) (processor.Traces, error) {
// 'from_attributes' or 'to_attributes' under 'name' has to be set for the span
// processor to be valid. If not set and not enforced, the processor would do no work.
oCfg := cfg.(*Config)
if len(oCfg.Rename.FromAttributes) == 0 &&
(oCfg.Rename.ToAttributes == nil || len(oCfg.Rename.ToAttributes.Rules) == 0) &&
oCfg.SetStatus == nil {
return nil, errMissingRequiredField
}
if oCfg.SetStatus != nil {
if oCfg.SetStatus.Code != statusCodeUnset && oCfg.SetStatus.Code != statusCodeError && oCfg.SetStatus.Code != statusCodeOk {
return nil, errIncorrectStatusCode
}
if len(oCfg.SetStatus.Description) > 0 && oCfg.SetStatus.Code != statusCodeError {
return nil, errIncorrectStatusDescription
}
}
sp, err := newSpanProcessor(*oCfg)
if err != nil {
return nil, err
}
return processorhelper.NewTracesProcessor(
ctx,
set,
cfg,
nextConsumer,
sp.processTraces,
processorhelper.WithCapabilities(processorCapabilities))
}