-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathtranslator.go
167 lines (146 loc) · 7.25 KB
/
translator.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package host
import (
"fmt"
"log"
"slices"
"strings"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
"github.com/aws/amazon-cloudwatch-agent/translator/config"
"github.com/aws/amazon-cloudwatch-agent/translator/context"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/common"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/exporter/awscloudwatch"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/exporter/awsemf"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/exporter/prometheusremotewrite"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/extension/agenthealth"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/extension/sigv4auth"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/processor/awsentity"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/processor/batchprocessor"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/processor/cumulativetodeltaprocessor"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/processor/ec2taggerprocessor"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/processor/k8sattributesprocessor"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/processor/metricsdecorator"
"github.com/aws/amazon-cloudwatch-agent/translator/translate/otel/processor/rollupprocessor"
"github.com/aws/amazon-cloudwatch-agent/translator/util/ecsutil"
)
type translator struct {
name string
common.DestinationProvider
receivers common.TranslatorMap[component.Config]
}
var supportedEntityProcessorDestinations = [...]string{
common.DefaultDestination,
common.CloudWatchKey,
common.CloudWatchLogsKey,
}
var _ common.Translator[*common.ComponentTranslators] = (*translator)(nil)
// NewTranslator creates a new host pipeline translator. The receiver types
// passed in are converted to config.ComponentIDs, sorted, and used directly
// in the translated pipeline.
func NewTranslator(
name string,
receivers common.TranslatorMap[component.Config],
opts ...common.TranslatorOption,
) common.Translator[*common.ComponentTranslators] {
t := &translator{name: name, receivers: receivers}
for _, opt := range opts {
opt(t)
}
if t.Destination() != "" {
t.name += "/" + t.Destination()
}
return t
}
func (t translator) ID() component.ID {
return component.NewIDWithName(component.DataTypeMetrics, t.name)
}
// Translate creates a pipeline if metrics section exists.
func (t translator) Translate(conf *confmap.Conf) (*common.ComponentTranslators, error) {
if conf == nil || t.receivers.Len() == 0 {
return nil, fmt.Errorf("no receivers configured in pipeline %s", t.name)
}
var entityProcessor common.Translator[component.Config]
var ec2TaggerEnabled bool
translators := common.ComponentTranslators{
Receivers: t.receivers,
Processors: common.NewTranslatorMap[component.Config](),
Exporters: common.NewTranslatorMap[component.Config](),
Extensions: common.NewTranslatorMap[component.Config](),
}
if strings.HasPrefix(t.name, common.PipelineNameHostDeltaMetrics) || strings.HasPrefix(t.name, common.PipelineNameHostOtlpMetrics) {
log.Printf("D! delta processor required because metrics with diskio or net are set")
translators.Processors.Set(cumulativetodeltaprocessor.NewTranslator(common.WithName(t.name), cumulativetodeltaprocessor.WithDefaultKeys()))
}
if t.Destination() != common.CloudWatchLogsKey {
if conf.IsSet(common.ConfigKey(common.MetricsKey, common.AppendDimensionsKey)) {
log.Printf("D! ec2tagger processor required because append_dimensions is set")
translators.Processors.Set(ec2taggerprocessor.NewTranslator())
ec2TaggerEnabled = true
}
mdt := metricsdecorator.NewTranslator(metricsdecorator.WithIgnorePlugins(common.JmxKey))
if mdt.IsSet(conf) {
log.Printf("D! metric decorator required because measurement fields are set")
translators.Processors.Set(mdt)
}
}
currentContext := context.CurrentContext()
switch determinePipeline(t.name) {
case common.PipelineNameHostOtlpMetrics:
// TODO: For OTLP, the entity processor is only on K8S for now. Eventually this should be added to EC2
if currentContext.KubernetesMode() != "" {
translators.Processors.Set(k8sattributesprocessor.NewTranslatorWithName(t.name))
entityProcessor = awsentity.NewTranslatorWithEntityType(awsentity.Service, common.OtlpKey, false)
}
case common.PipelineNameHostCustomMetrics:
if !currentContext.RunInContainer() {
entityProcessor = awsentity.NewTranslatorWithEntityType(awsentity.Service, "telegraf", true)
}
case common.PipelineNameHost, common.PipelineNameHostDeltaMetrics:
if !currentContext.RunInContainer() {
entityProcessor = awsentity.NewTranslatorWithEntityType(awsentity.Resource, "", ec2TaggerEnabled)
}
}
validDestination := slices.Contains(supportedEntityProcessorDestinations[:], t.Destination())
// ECS is not in scope for entity association, so we only add the entity processor in non-ECS platforms
isECS := ecsutil.GetECSUtilSingleton().IsECS()
if entityProcessor != nil && currentContext.Mode() == config.ModeEC2 && !isECS && validDestination {
translators.Processors.Set(entityProcessor)
}
switch t.Destination() {
case common.DefaultDestination, common.CloudWatchKey:
translators.Exporters.Set(awscloudwatch.NewTranslator())
translators.Extensions.Set(agenthealth.NewTranslator(component.DataTypeMetrics, []string{agenthealth.OperationPutMetricData}))
translators.Extensions.Set(agenthealth.NewTranslatorWithStatusCode(component.MustNewType("statuscode"), nil, true))
case common.AMPKey:
if conf.IsSet(common.MetricsAggregationDimensionsKey) {
translators.Processors.Set(rollupprocessor.NewTranslator())
}
translators.Processors.Set(batchprocessor.NewTranslatorWithNameAndSection(t.name, common.MetricsKey))
translators.Exporters.Set(prometheusremotewrite.NewTranslatorWithName(common.AMPKey))
translators.Extensions.Set(sigv4auth.NewTranslator())
case common.CloudWatchLogsKey:
translators.Processors.Set(batchprocessor.NewTranslatorWithNameAndSection(t.name, common.LogsKey))
translators.Exporters.Set(awsemf.NewTranslator())
translators.Extensions.Set(agenthealth.NewTranslator(component.DataTypeLogs, []string{agenthealth.OperationPutLogEvents}))
translators.Extensions.Set(agenthealth.NewTranslatorWithStatusCode(component.MustNewType("statuscode"), nil, true))
default:
return nil, fmt.Errorf("pipeline (%s) does not support destination (%s) in configuration", t.name, t.Destination())
}
return &translators, nil
}
func determinePipeline(name string) string {
// The conditionals have to be done in a certain order because PipelineNameHost is just "host", whereas
// the other constants are prefixed with "host"
if strings.HasPrefix(name, common.PipelineNameHostDeltaMetrics) {
return common.PipelineNameHostDeltaMetrics
} else if strings.HasPrefix(name, common.PipelineNameHostOtlpMetrics) {
return common.PipelineNameHostOtlpMetrics
} else if strings.HasPrefix(name, common.PipelineNameHostCustomMetrics) {
return common.PipelineNameHostCustomMetrics
} else if strings.HasPrefix(name, common.PipelineNameHost) {
return common.PipelineNameHost
}
return ""
}