Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cloudwatch encoding #37222

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ body:
- extension/cgroupruntime
- extension/encoding
- extension/encoding/avrologencoding
- extension/encoding/cloudwatchencoding
- extension/encoding/jaegerencoding
- extension/encoding/jsonlogencoding
- extension/encoding/otlpencoding
Expand Down Expand Up @@ -159,6 +160,7 @@ body:
- pkg/status
- pkg/translator/azure
- pkg/translator/azurelogs
- pkg/translator/cloudwatch
- pkg/translator/jaeger
- pkg/translator/loki
- pkg/translator/opencensus
Expand Down
2 changes: 2 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ body:
- extension/cgroupruntime
- extension/encoding
- extension/encoding/avrologencoding
- extension/encoding/cloudwatchencoding
- extension/encoding/jaegerencoding
- extension/encoding/jsonlogencoding
- extension/encoding/otlpencoding
Expand Down Expand Up @@ -153,6 +154,7 @@ body:
- pkg/status
- pkg/translator/azure
- pkg/translator/azurelogs
- pkg/translator/cloudwatch
- pkg/translator/jaeger
- pkg/translator/loki
- pkg/translator/opencensus
Expand Down
2 changes: 2 additions & 0 deletions .github/ISSUE_TEMPLATE/other.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ body:
- extension/cgroupruntime
- extension/encoding
- extension/encoding/avrologencoding
- extension/encoding/cloudwatchencoding
- extension/encoding/jaegerencoding
- extension/encoding/jsonlogencoding
- extension/encoding/otlpencoding
Expand Down Expand Up @@ -153,6 +154,7 @@ body:
- pkg/status
- pkg/translator/azure
- pkg/translator/azurelogs
- pkg/translator/cloudwatch
- pkg/translator/jaeger
- pkg/translator/loki
- pkg/translator/opencensus
Expand Down
2 changes: 2 additions & 0 deletions .github/ISSUE_TEMPLATE/unmaintained.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ body:
- extension/cgroupruntime
- extension/encoding
- extension/encoding/avrologencoding
- extension/encoding/cloudwatchencoding
- extension/encoding/jaegerencoding
- extension/encoding/jsonlogencoding
- extension/encoding/otlpencoding
Expand Down Expand Up @@ -158,6 +159,7 @@ body:
- pkg/status
- pkg/translator/azure
- pkg/translator/azurelogs
- pkg/translator/cloudwatch
- pkg/translator/jaeger
- pkg/translator/loki
- pkg/translator/opencensus
Expand Down
1 change: 1 addition & 0 deletions extension/encoding/cloudwatchencodingextension/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../../Makefile.Common
22 changes: 22 additions & 0 deletions extension/encoding/cloudwatchencodingextension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Cloudwatch encoding extension

<!-- status autogenerated section -->
| Status | |
| ------------- |-----------|
| Stability | [development] |
| Distributions | [] |
| Issues | [![Open issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aopen%20label%3Aextension%2Fcloudwatchencoding%20&label=open&color=orange&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aopen+is%3Aissue+label%3Aextension%2Fcloudwatchencoding) [![Closed issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aclosed%20label%3Aextension%2Fcloudwatchencoding%20&label=closed&color=blue&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aclosed+is%3Aissue+label%3Aextension%2Fcloudwatchencoding) |
| [Code Owners](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/CONTRIBUTING.md#becoming-a-code-owner) | [@constanca-m](https://www.github.com/constanca-m), [@axw](https://www.github.com/axw) |

[development]: https://github.com/open-telemetry/opentelemetry-collector#development
<!-- end autogenerated section -->

The `cloudwatch_encoding` extension is used to unmarshal:
- cloudwatch logs into `plogs.Log`, if used in a logs receiver
- cloudwatch metrics into `pmetrics.Metric`, if used in a metrics receiver

Example:
```yaml
extensions:
cloudwatch_encoding:
```
35 changes: 35 additions & 0 deletions extension/encoding/cloudwatchencodingextension/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package cloudwatchencodingextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/cloudwatchencodingextension"

import (
"fmt"

"go.opentelemetry.io/collector/component"
)

type contentEncoding string

const (
NoEncoding = ""
GZipEncoded = "gzip"
)

type Config struct {
Encoding contentEncoding `mapstructure:"content_encoding"`
}

func createDefaultConfig() component.Config {
return &Config{}
}

func (c *Config) Validate() error {
switch c.Encoding {
case NoEncoding:
case GZipEncoded:
default:
return fmt.Errorf("unknown content encoding %q", c.Encoding)
}
return nil
}
59 changes: 59 additions & 0 deletions extension/encoding/cloudwatchencodingextension/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package cloudwatchencodingextension

import (
"errors"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap/confmaptest"

"github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/cloudwatchencodingextension/internal/metadata"
)

func TestLoadConfig(t *testing.T) {
t.Parallel()

defaultCfg := createDefaultConfig().(*Config)

tests := []struct {
id component.ID
expected component.Config
expectedErr error
}{
{
id: component.NewID(metadata.Type),
expected: defaultCfg,
},
{
id: component.NewIDWithName(metadata.Type, "invalid_encoding"),
expectedErr: errors.New("unknown content encoding \"invalid\""),
},
}

for _, tt := range tests {
t.Run(tt.id.String(), func(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)

factory := NewFactory()
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub(tt.id.String())
require.NoError(t, err)
require.NoError(t, sub.Unmarshal(cfg))

if tt.expectedErr != nil {
err = component.ValidateConfig(cfg)
require.Equal(t, tt.expectedErr, err)
return
}

require.NoError(t, component.ValidateConfig(cfg))
require.Equal(t, tt.expected, cfg)
})
}
}
5 changes: 5 additions & 0 deletions extension/encoding/cloudwatchencodingextension/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:generate mdatagen metadata.yaml
package cloudwatchencodingextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/cloudwatchencodingextension"
79 changes: 79 additions & 0 deletions extension/encoding/cloudwatchencodingextension/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package cloudwatchencodingextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/cloudwatchencodingextension"

import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/extension"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/cloudwatch"
)

var (
_ encoding.LogsUnmarshalerExtension = (*cloudwatchExtension)(nil)
_ encoding.MetricsUnmarshalerExtension = (*cloudwatchExtension)(nil)
)

type cloudwatchExtension struct {
config *Config
logger *zap.Logger
}

func createExtension(_ context.Context, settings extension.Settings, config component.Config) (extension.Extension, error) {
return &cloudwatchExtension{
config: config.(*Config),
logger: settings.Logger,
}, nil
}

func (c *cloudwatchExtension) Start(_ context.Context, _ component.Host) error {
return nil
}

func (c *cloudwatchExtension) Shutdown(_ context.Context) error {
return nil
}

func decompress(buf []byte, encoding contentEncoding) ([]byte, error) {
switch encoding {
case NoEncoding:
return buf, nil
case GZipEncoded:
reader, err := gzip.NewReader(bytes.NewReader(buf))
if err != nil {
return nil, fmt.Errorf("failed to create gzip reader: %w", err)
}
defer reader.Close()
return io.ReadAll(reader)
default:
// not possible, prevented by config.Validate
return nil, nil
}
}

func (c *cloudwatchExtension) UnmarshalLogs(buf []byte) (plog.Logs, error) {
data, err := decompress(buf, c.config.Encoding)
if err != nil {
return plog.Logs{}, err
}
return cloudwatch.UnmarshalLogs(data)
}

func (c *cloudwatchExtension) UnmarshalMetrics(buf []byte) (pmetric.Metrics, error) {
data, err := decompress(buf, c.config.Encoding)
if err != nil {
return pmetric.Metrics{}, err
}
return cloudwatch.UnmarshalMetrics(data)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package cloudwatchencodingextension

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
)

func TestExtension_Start_Shutdown(t *testing.T) {
extension := &cloudwatchExtension{}

err := extension.Start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err)

err = extension.Shutdown(context.Background())
require.NoError(t, err)
}
19 changes: 19 additions & 0 deletions extension/encoding/cloudwatchencodingextension/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package cloudwatchencodingextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/cloudwatchencodingextension"

import (
"go.opentelemetry.io/collector/extension"

"github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/cloudwatchencodingextension/internal/metadata"
)

func NewFactory() extension.Factory {
return extension.NewFactory(
metadata.Type,
createDefaultConfig,
createExtension,
metadata.ExtensionStability,
)
}
19 changes: 19 additions & 0 deletions extension/encoding/cloudwatchencodingextension/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package cloudwatchencodingextension

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/extension/extensiontest"
)

func TestCreateExtension(t *testing.T) {
cfg := createDefaultConfig().(*Config)
ext, err := createExtension(context.Background(), extensiontest.NewNopSettings(), cfg)
require.NoError(t, err)
require.NotNil(t, ext)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading