Skip to content

Commit a0590da

Browse files
dricrossagarakan
authored andcommitted
Replace telegraf prometheus plugin (#1709)
1 parent a2fc23a commit a0590da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+541
-1094
lines changed

plugins/plugins.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
//Enable cloudwatch-agent process plugins
88
_ "github.com/aws/amazon-cloudwatch-agent/plugins/processors/ecsdecorator"
99
_ "github.com/aws/amazon-cloudwatch-agent/plugins/processors/k8sdecorator"
10+
_ "github.com/aws/amazon-cloudwatch-agent/plugins/processors/prometheusadapter"
1011

1112
// Enabled cloudwatch-agent input plugins
1213
_ "github.com/aws/amazon-cloudwatch-agent/plugins/inputs/logfile"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package prometheusadapter
5+
6+
import (
7+
"go.opentelemetry.io/collector/component"
8+
)
9+
10+
type Config struct{}
11+
12+
// Verify Config implements Processor interface.
13+
var _ component.Config = (*Config)(nil)
14+
15+
func (cfg *Config) Validate() error {
16+
return nil
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package prometheusadapter
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"go.opentelemetry.io/collector/confmap"
11+
)
12+
13+
func TestUnmarshalDefaultConfig(t *testing.T) {
14+
factory := NewFactory()
15+
cfg := factory.CreateDefaultConfig()
16+
assert.NoError(t, confmap.New().Unmarshal(cfg))
17+
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
18+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package prometheusadapter
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"go.opentelemetry.io/collector/component"
11+
"go.opentelemetry.io/collector/consumer"
12+
"go.opentelemetry.io/collector/processor"
13+
"go.opentelemetry.io/collector/processor/processorhelper"
14+
)
15+
16+
const (
17+
stability = component.StabilityLevelBeta
18+
)
19+
20+
var (
21+
TypeStr, _ = component.NewType("prometheusadapter")
22+
processorCapabilities = consumer.Capabilities{MutatesData: true}
23+
)
24+
25+
func NewFactory() processor.Factory {
26+
return processor.NewFactory(
27+
TypeStr,
28+
createDefaultConfig,
29+
processor.WithMetrics(createMetricsProcessor, stability))
30+
}
31+
32+
func createDefaultConfig() component.Config {
33+
return &Config{}
34+
}
35+
36+
func createMetricsProcessor(
37+
ctx context.Context,
38+
set processor.Settings,
39+
cfg component.Config,
40+
nextConsumer consumer.Metrics,
41+
) (processor.Metrics, error) {
42+
processorConfig, ok := cfg.(*Config)
43+
if !ok {
44+
return nil, fmt.Errorf("configuration parsing error")
45+
}
46+
47+
metricsProcessor := newPrometheusAdapterProcessor(processorConfig, set.Logger)
48+
49+
return processorhelper.NewMetrics(
50+
ctx,
51+
set,
52+
cfg,
53+
nextConsumer,
54+
metricsProcessor.processMetrics,
55+
processorhelper.WithCapabilities(processorCapabilities))
56+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package prometheusadapter
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
"go.opentelemetry.io/collector/component/componenttest"
13+
"go.opentelemetry.io/collector/consumer/consumertest"
14+
"go.opentelemetry.io/collector/pipeline"
15+
"go.opentelemetry.io/collector/processor/processortest"
16+
)
17+
18+
func TestCreateDefaultConfig(t *testing.T) {
19+
factory := NewFactory()
20+
require.NotNil(t, factory)
21+
22+
cfg := factory.CreateDefaultConfig()
23+
assert.NotNil(t, cfg, "failed to create default config")
24+
assert.NoError(t, componenttest.CheckConfigStruct(cfg))
25+
}
26+
27+
func TestCreateProcessor(t *testing.T) {
28+
factory := NewFactory()
29+
require.NotNil(t, factory)
30+
31+
cfg := factory.CreateDefaultConfig()
32+
setting := processortest.NewNopSettings()
33+
34+
tProcessor, err := factory.CreateTraces(context.Background(), setting, cfg, consumertest.NewNop())
35+
assert.Equal(t, err, pipeline.ErrSignalNotSupported)
36+
assert.Nil(t, tProcessor)
37+
38+
mProcessor, err := factory.CreateMetrics(context.Background(), setting, cfg, consumertest.NewNop())
39+
assert.NoError(t, err)
40+
assert.NotNil(t, mProcessor)
41+
42+
lProcessor, err := factory.CreateLogs(context.Background(), setting, cfg, consumertest.NewNop())
43+
assert.Equal(t, err, pipeline.ErrSignalNotSupported)
44+
assert.Nil(t, lProcessor)
45+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package prometheusadapter
5+
6+
import (
7+
"context"
8+
9+
"go.opentelemetry.io/collector/pdata/pmetric"
10+
"go.uber.org/zap"
11+
)
12+
13+
type prometheusAdapterProcessor struct {
14+
*Config
15+
logger *zap.Logger
16+
}
17+
18+
func newPrometheusAdapterProcessor(config *Config, logger *zap.Logger) *prometheusAdapterProcessor {
19+
d := &prometheusAdapterProcessor{
20+
Config: config,
21+
logger: logger,
22+
}
23+
return d
24+
}
25+
26+
func (d *prometheusAdapterProcessor) processMetrics(_ context.Context, md pmetric.Metrics) (pmetric.Metrics, error) {
27+
return md, nil
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package prometheusadapter
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"go.uber.org/zap"
11+
)
12+
13+
func TestProcessMetricsForPrometheusAdapter(t *testing.T) {
14+
logger, _ := zap.NewDevelopment()
15+
pap := newPrometheusAdapterProcessor(createDefaultConfig().(*Config), logger)
16+
17+
assert.NotNil(t, pap)
18+
}

receiver/adapter/plugins_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func scrapeMetrics(as *assert.Assertions, ctx context.Context, receiver *Adapted
124124
}
125125

126126
func validateMetricName(as *assert.Assertions, plugin string, expectedResourceMetricsName []string, actualOtelSlMetrics pmetric.MetricSlice) {
127-
as.Equal(len(expectedResourceMetricsName), actualOtelSlMetrics.Len(), "Number of metrics did not match!")
127+
as.Equal(len(expectedResourceMetricsName), actualOtelSlMetrics.Len(), "Number of metrics did not match! expected %+v\nactual: %+v", expectedResourceMetricsName, actualOtelSlMetrics)
128128

129129
matchMetrics := actualOtelSlMetrics.Len()
130130
for _, expectedMetric := range expectedResourceMetricsName {

service/defaultcomponents/components.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import (
6565
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/ec2tagger"
6666
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/gpuattributes"
6767
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/kueueattributes"
68+
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/prometheusadapter"
6869
"github.com/aws/amazon-cloudwatch-agent/processor/rollupprocessor"
6970
"github.com/aws/amazon-cloudwatch-agent/receiver/awsebsnvmereceiver"
7071
)
@@ -112,6 +113,7 @@ func Factories() (otelcol.Factories, error) {
112113
metricsgenerationprocessor.NewFactory(),
113114
metricstransformprocessor.NewFactory(),
114115
probabilisticsamplerprocessor.NewFactory(),
116+
prometheusadapter.NewFactory(),
115117
resourceprocessor.NewFactory(),
116118
resourcedetectionprocessor.NewFactory(),
117119
rollupprocessor.NewFactory(),

service/defaultcomponents/components_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func TestComponents(t *testing.T) {
5757
"k8sattributes",
5858
"memory_limiter",
5959
"metricstransform",
60+
"prometheusadapter",
6061
"resourcedetection",
6162
"resource",
6263
"rollup",

0 commit comments

Comments
 (0)