-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[extension/awslogs_encoding] Add support for cloudwatch logs coming from subscription filter #38821
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
Merged
atoulme
merged 6 commits into
open-telemetry:main
from
constanca-m:aws-subscription-unmarshaler
Mar 21, 2025
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
069899a
[extension/awslogs_encoding] Add support for cloudwatch logs coming f…
constanca-m 59eb7a2
change to pointer again
constanca-m 2e38ad1
fix go.mod and scope name
constanca-m b2528b2
Address comments
constanca-m 2666279
upate go.mod
constanca-m a2319f2
upate go.sum
constanca-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Use this changelog template to create an entry for release notes. | ||
|
|
||
| # One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
| change_type: enhancement | ||
|
|
||
| # The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
| component: awslogsencodingextension | ||
|
|
||
| # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
| note: Add support for cloudwatch logs coming from subscription filters. | ||
|
|
||
| # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
| issues: [38820] | ||
|
|
||
| # (Optional) One or more lines of additional information to render under the primary note. | ||
| # These lines will be padded with 2 spaces and then inserted directly into the document. | ||
| # Use pipe (|) for multiline entries. | ||
| subtext: | ||
|
|
||
| # If your change doesn't affect end users or the exported elements of any package, | ||
| # you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
| # Optional: The change log or logs in which this entry should be included. | ||
| # e.g. '[user]' or '[user, api]' | ||
| # Include 'user' if the change is relevant to end users. | ||
| # Include 'api' if there is a change to a library API. | ||
| # Default: '[user]' | ||
| change_logs: [user] |
17 changes: 0 additions & 17 deletions
17
extension/encoding/awslogsencodingextension/cloudwatchlogssubscriptionfilter.go
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
140 changes: 140 additions & 0 deletions
140
.../encoding/awslogsencodingextension/unmarshaler/subscription-filter/subscription_filter.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package subscriptionfilter // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awslogsencodingextension/unmarshaler/subscription-filter" | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "compress/gzip" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/aws/aws-lambda-go/events" | ||
| jsoniter "github.com/json-iterator/go" | ||
constanca-m marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "go.opentelemetry.io/collector/component" | ||
| "go.opentelemetry.io/collector/pdata/pcommon" | ||
| "go.opentelemetry.io/collector/pdata/plog" | ||
| conventions "go.opentelemetry.io/collector/semconv/v1.27.0" | ||
|
|
||
| "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/awslogsencodingextension/internal/metadata" | ||
| ) | ||
|
|
||
| var ( | ||
| errEmptyOwner = errors.New("cloudwatch log with message type 'DATA_MESSAGE' has empty owner field") | ||
| errEmptyLogGroup = errors.New("cloudwatch log with message type 'DATA_MESSAGE' has empty log group field") | ||
| errEmptyLogStream = errors.New("cloudwatch log with message type 'DATA_MESSAGE' has empty log stream field") | ||
| ) | ||
|
|
||
| func validateLog(log events.CloudwatchLogsData) error { | ||
| switch log.MessageType { | ||
| case "DATA_MESSAGE": | ||
| if log.Owner == "" { | ||
| return errEmptyOwner | ||
| } | ||
| if log.LogGroup == "" { | ||
| return errEmptyLogGroup | ||
| } | ||
| if log.LogStream == "" { | ||
| return errEmptyLogStream | ||
| } | ||
| case "CONTROL_MESSAGE": | ||
| default: | ||
| return fmt.Errorf("cloudwatch log has invalid message type %q", log.MessageType) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| type subscriptionFilterUnmarshaler struct { | ||
| buildInfo component.BuildInfo | ||
|
|
||
| // We need to decompress the records. | ||
| // Creating a new gzip.Reader every time is expensive. | ||
| // We can keep the gzip in cache, and take advantage | ||
| // of the continuously AWS environment or the AWS | ||
| // warm starts, in case of lambda. | ||
| // See https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html. | ||
constanca-m marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| gzipPool sync.Pool | ||
| } | ||
|
|
||
| func NewSubscriptionFilterUnmarshaler(buildInfo component.BuildInfo) plog.Unmarshaler { | ||
| return &subscriptionFilterUnmarshaler{ | ||
| buildInfo: buildInfo, | ||
| gzipPool: sync.Pool{}, | ||
| } | ||
| } | ||
|
|
||
| var _ plog.Unmarshaler = (*subscriptionFilterUnmarshaler)(nil) | ||
|
|
||
| // UnmarshalLogs deserializes the given record as CloudWatch Logs events | ||
| // into a plog.Logs, grouping logs by owner (account ID), log group, and | ||
| // log stream. Logs are assumed to be gzip-compressed as specified at | ||
| // https://docs.aws.amazon.com/firehose/latest/dev/writing-with-cloudwatch-logs.html. | ||
| func (f *subscriptionFilterUnmarshaler) UnmarshalLogs(compressedRecord []byte) (plog.Logs, error) { | ||
| var errDecompress error | ||
| record, ok := f.gzipPool.Get().(*gzip.Reader) | ||
constanca-m marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if !ok { | ||
| record, errDecompress = gzip.NewReader(bytes.NewReader(compressedRecord)) | ||
| } else { | ||
| errDecompress = record.Reset(bytes.NewReader(compressedRecord)) | ||
| } | ||
| if errDecompress != nil { | ||
| if record != nil { | ||
| f.gzipPool.Put(record) | ||
| } | ||
| return plog.Logs{}, fmt.Errorf("failed to decompress record: %w", errDecompress) | ||
| } | ||
| defer func() { | ||
| _ = record.Close() | ||
| f.gzipPool.Put(record) | ||
| }() | ||
|
|
||
| decompressedRecord, errRead := io.ReadAll(record) | ||
| if errRead != nil { | ||
| return plog.Logs{}, fmt.Errorf("failed to read decompressed record: %w", errRead) | ||
| } | ||
|
|
||
| var cwLog events.CloudwatchLogsData | ||
| if err := jsoniter.ConfigFastest.Unmarshal(decompressedRecord, &cwLog); err != nil { | ||
constanca-m marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return plog.Logs{}, fmt.Errorf("error unmarshaling data: %w", err) | ||
| } | ||
|
|
||
| if cwLog.MessageType == "CONTROL_MESSAGE" { | ||
| return plog.Logs{}, nil | ||
| } | ||
|
|
||
| if err := validateLog(cwLog); err != nil { | ||
| return plog.Logs{}, fmt.Errorf("invalid cloudwatch log: %w", err) | ||
| } | ||
|
|
||
| return f.createLogs(cwLog), nil | ||
| } | ||
|
|
||
| // createLogs create plog.Logs from the cloudwatchLog | ||
| func (f *subscriptionFilterUnmarshaler) createLogs( | ||
| cwLog events.CloudwatchLogsData, | ||
| ) plog.Logs { | ||
| logs := plog.NewLogs() | ||
| rl := logs.ResourceLogs().AppendEmpty() | ||
| resourceAttrs := rl.Resource().Attributes() | ||
| resourceAttrs.PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAWS) | ||
| resourceAttrs.PutStr(conventions.AttributeCloudAccountID, cwLog.Owner) | ||
| resourceAttrs.PutEmptySlice(conventions.AttributeAWSLogGroupNames).AppendEmpty().SetStr(cwLog.LogGroup) | ||
| resourceAttrs.PutEmptySlice(conventions.AttributeAWSLogStreamNames).AppendEmpty().SetStr(cwLog.LogStream) | ||
| resourceAttrs.PutStr(conventions.AttributeAWSLogGroupNames, cwLog.LogGroup) | ||
| resourceAttrs.PutStr(conventions.AttributeAWSLogStreamNames, cwLog.LogStream) | ||
|
|
||
| sl := rl.ScopeLogs().AppendEmpty() | ||
| sl.Scope().SetName(metadata.ScopeName) | ||
| sl.Scope().SetVersion(f.buildInfo.Version) | ||
| for _, event := range cwLog.LogEvents { | ||
| logRecord := sl.LogRecords().AppendEmpty() | ||
| // pcommon.Timestamp is a time specified as UNIX Epoch time in nanoseconds | ||
| // but timestamp in cloudwatch logs are in milliseconds. | ||
| logRecord.SetTimestamp(pcommon.Timestamp(event.Timestamp * int64(time.Millisecond))) | ||
| logRecord.Body().SetStr(event.Message) | ||
| } | ||
| return logs | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you move this to an internal package?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason this is public is so other components can use the
NewSubscriptionFilterUnmarshalerfunction directly. For example, this would enable us to remove the cwlog code in awsfirehosereceiver.Rather than forcing users to configure an extension, there would be a special case where the receiver will instantiate the unmarshaler directly. We could also internally create an extension, but it seems a bit unnecessarily complicated.