Skip to content

Commit dd31b2f

Browse files
committed
Create awscloudwatchmetricstreamsencodingextension
Create the component skeleton and basic config tests. Unmarshallers will be implemented in a follow up PR.
1 parent f9b5841 commit dd31b2f

File tree

20 files changed

+567
-1
lines changed

20 files changed

+567
-1
lines changed

.github/ISSUE_TEMPLATE/bug_report.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ body:
9696
- extension/cgroupruntime
9797
- extension/encoding
9898
- extension/encoding/avrologencoding
99+
- extension/encoding/awscloudwatchmetricstreamsencoding
99100
- extension/encoding/googlecloudlogentryencoding
100101
- extension/encoding/jaegerencoding
101102
- extension/encoding/jsonlogencoding

.github/ISSUE_TEMPLATE/feature_request.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ body:
9090
- extension/cgroupruntime
9191
- extension/encoding
9292
- extension/encoding/avrologencoding
93+
- extension/encoding/awscloudwatchmetricstreamsencoding
9394
- extension/encoding/googlecloudlogentryencoding
9495
- extension/encoding/jaegerencoding
9596
- extension/encoding/jsonlogencoding

.github/ISSUE_TEMPLATE/other.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ body:
9090
- extension/cgroupruntime
9191
- extension/encoding
9292
- extension/encoding/avrologencoding
93+
- extension/encoding/awscloudwatchmetricstreamsencoding
9394
- extension/encoding/googlecloudlogentryencoding
9495
- extension/encoding/jaegerencoding
9596
- extension/encoding/jsonlogencoding

.github/ISSUE_TEMPLATE/unmaintained.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ body:
9595
- extension/cgroupruntime
9696
- extension/encoding
9797
- extension/encoding/avrologencoding
98+
- extension/encoding/awscloudwatchmetricstreamsencoding
9899
- extension/encoding/googlecloudlogentryencoding
99100
- extension/encoding/jaegerencoding
100101
- extension/encoding/jsonlogencoding
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include ../../../Makefile.Common
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# AWS CloudWatch Metric streams encoding extension
2+
3+
<!-- status autogenerated section -->
4+
| Status | |
5+
| ------------- |-----------|
6+
| Stability | [development] |
7+
| Distributions | [] |
8+
| Issues | [![Open issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aopen%20label%3Aextension%2Fawscloudwatchmetricstreamsencoding%20&label=open&color=orange&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aopen+is%3Aissue+label%3Aextension%2Fawscloudwatchmetricstreamsencoding) [![Closed issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aclosed%20label%3Aextension%2Fawscloudwatchmetricstreamsencoding%20&label=closed&color=blue&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aclosed+is%3Aissue+label%3Aextension%2Fawscloudwatchmetricstreamsencoding) |
9+
| [Code Owners](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/CONTRIBUTING.md#becoming-a-code-owner) | [@axw](https://www.github.com/axw) |
10+
11+
[development]: https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/component-stability.md#development
12+
<!-- end autogenerated section -->
13+
14+
This extension unmarshals metrics encoded in formats produced by
15+
[Amazon CloudWatch Metric Streams](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Metric-Streams.html).
16+
17+
The extension can be configured to unmarshal metrics in either the JSON or OpenTelemetry 1.0.0 output formats.
18+
At this time the OpenTelemetry 0.7.0 output format is unsupported, there are no plans to add support for it.
19+
20+
Example for OpenTelemetry 1.0.0 format:
21+
```yaml
22+
extensions:
23+
awscloudwatchmetricstreams_encoding:
24+
format: opentelemetry1.0
25+
```
26+
27+
Example for JSON format:
28+
```yaml
29+
extensions:
30+
awscloudwatchmetricstreams_encoding:
31+
format: json
32+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package awscloudwatchmetricstreamsencodingextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awscloudwatchmetricstreamsencodingextension"
5+
import (
6+
"fmt"
7+
8+
"go.opentelemetry.io/collector/confmap/xconfmap"
9+
)
10+
11+
var _ xconfmap.Validator = (*Config)(nil)
12+
13+
const (
14+
formatJSON = "json"
15+
formatOpenTelemetry10 = "opentelemetry1.0"
16+
)
17+
18+
var supportedFormats = []string{formatOpenTelemetry10, formatJSON}
19+
20+
type Config struct {
21+
// Format defines the CloudWatch Metric Streams format.
22+
//
23+
// Valid values are "json" and "opentelemetry1.0"; opentelemetry0.7 is unsupported.
24+
//
25+
// See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudwatch-metricstream.html#cfn-cloudwatch-metricstream-outputformat
26+
Format string `mapstructure:"format"`
27+
}
28+
29+
func (cfg *Config) Validate() error {
30+
switch cfg.Format {
31+
case "":
32+
return fmt.Errorf("format unspecified, expected one of %q", supportedFormats)
33+
case formatJSON, formatOpenTelemetry10:
34+
// valid
35+
default:
36+
return fmt.Errorf("unsupported format %q, expected one of %q", cfg.Format, supportedFormats)
37+
}
38+
return nil
39+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package awscloudwatchmetricstreamsencodingextension
5+
6+
import (
7+
"path/filepath"
8+
"strings"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
"go.opentelemetry.io/collector/component"
14+
"go.opentelemetry.io/collector/confmap/confmaptest"
15+
"go.opentelemetry.io/collector/confmap/xconfmap"
16+
17+
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awscloudwatchmetricstreamsencodingextension/internal/metadata"
18+
)
19+
20+
func TestLoadConfig(t *testing.T) {
21+
t.Parallel()
22+
23+
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
24+
require.NoError(t, err)
25+
26+
tests := []struct {
27+
id component.ID
28+
expected component.Config
29+
expectedErr string
30+
}{
31+
{
32+
id: component.NewIDWithName(metadata.Type, ""),
33+
expectedErr: `format unspecified, expected one of ["opentelemetry1.0" "json"]`,
34+
},
35+
{
36+
id: component.NewIDWithName(metadata.Type, "json"),
37+
expected: &Config{
38+
Format: formatJSON,
39+
},
40+
},
41+
{
42+
id: component.NewIDWithName(metadata.Type, "opentelemetry1.0"),
43+
expected: &Config{
44+
Format: formatOpenTelemetry10,
45+
},
46+
},
47+
{
48+
id: component.NewIDWithName(metadata.Type, "opentelemetry0.7"),
49+
expectedErr: `unsupported format "opentelemetry0.7", expected one of ["opentelemetry1.0" "json"]`,
50+
},
51+
}
52+
53+
for _, tt := range tests {
54+
name := strings.ReplaceAll(tt.id.String(), "/", "_")
55+
t.Run(name, func(t *testing.T) {
56+
factory := NewFactory()
57+
cfg := factory.CreateDefaultConfig()
58+
59+
sub, err := cm.Sub(tt.id.String())
60+
require.NoError(t, err)
61+
require.NoError(t, sub.Unmarshal(cfg))
62+
63+
err = xconfmap.Validate(cfg)
64+
if tt.expectedErr != "" {
65+
assert.Error(t, err)
66+
assert.EqualError(t, err, tt.expectedErr)
67+
} else {
68+
assert.NoError(t, err)
69+
assert.Equal(t, tt.expected, cfg)
70+
}
71+
})
72+
}
73+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//go:generate mdatagen metadata.yaml
5+
6+
// Package awscloudwatchmetricstreamsencodingextension provides an encoding extension
7+
// capable of unmarshalling metrics produced by CloudWatch Metric Streams.
8+
package awscloudwatchmetricstreamsencodingextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awscloudwatchmetricstreamsencodingextension"
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package awscloudwatchmetricstreamsencodingextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awscloudwatchmetricstreamsencodingextension"
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"go.opentelemetry.io/collector/component"
11+
"go.opentelemetry.io/collector/pdata/pmetric"
12+
13+
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding"
14+
)
15+
16+
var _ encoding.MetricsUnmarshalerExtension = (*encodingExtension)(nil)
17+
18+
type encodingExtension struct {
19+
pmetric.Unmarshaler
20+
}
21+
22+
func newExtension(cfg *Config) (*encodingExtension, error) {
23+
switch cfg.Format {
24+
case formatJSON:
25+
return &encodingExtension{Unmarshaler: formatJSONUnmarshaler{}}, nil
26+
case formatOpenTelemetry10:
27+
return &encodingExtension{Unmarshaler: formatOpenTelemetry10Unmarshaler{}}, nil
28+
default:
29+
// Format will have been validated by Config.Validate,
30+
// so we'll only get here if we haven't handled a valid
31+
// format.
32+
return nil, fmt.Errorf("unimplemented format %q", cfg.Format)
33+
}
34+
}
35+
36+
func (*encodingExtension) Start(_ context.Context, _ component.Host) error {
37+
return nil
38+
}
39+
40+
func (*encodingExtension) Shutdown(_ context.Context) error {
41+
return nil
42+
}
43+
44+
type formatJSONUnmarshaler struct{}
45+
46+
func (formatJSONUnmarshaler) UnmarshalMetrics([]byte) (pmetric.Metrics, error) {
47+
// TODO implement
48+
return pmetric.Metrics{}, fmt.Errorf("UnmarshalMetrics unimplemented for format %q", formatJSON)
49+
}
50+
51+
type formatOpenTelemetry10Unmarshaler struct{}
52+
53+
func (formatOpenTelemetry10Unmarshaler) UnmarshalMetrics([]byte) (pmetric.Metrics, error) {
54+
// TODO implement
55+
return pmetric.Metrics{}, fmt.Errorf("UnmarshalMetrics unimplemented for format %q", formatOpenTelemetry10)
56+
}

0 commit comments

Comments
 (0)