Skip to content

Commit 7a39141

Browse files
authored
Add environment variable to support backend storage tag enrichment (#1006)
* feat(aws): AWSX-1566 Adding storage tag HTTP header Signed-off-by: Vincent Boutour <vincent.boutour@datadoghq.com> * feat(aws): AWSX-1566 Adding new variable in the cloudformation template Signed-off-by: Vincent Boutour <vincent.boutour@datadoghq.com> * feat: AWSX-1566 Restoring fetch in the integration test to see metrics Signed-off-by: Vincent Boutour <vincent.boutour@datadoghq.com> * fixup! feat: AWSX-1566 Restoring fetch in the integration test to see metrics Signed-off-by: Vincent Boutour <vincent.boutour@datadoghq.com> * docs(aws): AWSX-1566 Refine documentation around the new variable Signed-off-by: Vincent Boutour <vincent.boutour@datadoghq.com> --------- Signed-off-by: Vincent Boutour <vincent.boutour@datadoghq.com>
1 parent f7fb1e1 commit 7a39141

15 files changed

+105
-21
lines changed

aws/logs_monitoring/README.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ You can run the Forwarder in a VPC private subnet and send data to Datadog over
278278
3. When installing the Forwarder with the CloudFormation template:
279279
1. Set `DdUseVPC` to `true`.
280280
2. Set `VPCSecurityGroupIds` and `VPCSubnetIds` based on your VPC settings.
281-
3. Set `DdFetchLambdaTags`, `DdFetchStepFunctionsTags` and `DdFetchS3Tags` to `false`, because AWS Resource Groups Tagging API doesn't support PrivateLink.
281+
3. Set `DdFetchLambdaTags`, `DdFetchStepFunctionsTags`, and `DdFetchS3Tags` to `false`, because AWS Resource Groups Tagging API doesn't support PrivateLink.
282282
283283
### AWS VPC and proxy support
284284
@@ -287,7 +287,7 @@ If you must deploy the Forwarder to a VPC without direct public internet access,
287287
1. Unless the Forwarder is deployed to a public subnet, follow the [instructions][15] to add endpoints for Secrets Manager and S3 to the VPC, so that the Forwarder can access those services.
288288
2. Update your proxy with following configurations ([HAProxy][17] or [NGINX][18]). If you are using another proxy, or Web Proxy, allowlist the Datadog domain, for example: `.{{< region-param key="dd_site" code="true" >}}`.
289289
3. When installing the Forwarder with the CloudFormation template, set `DdUseVPC`, `VPCSecurityGroupIds`, and `VPCSubnetIds`.
290-
4. Ensure the `DdFetchLambdaTags`, `DdFetchStepFunctionsTags` and `DdFetchS3Tags` options are disabled, because AWS VPC does not yet offer an endpoint for the Resource Groups Tagging API.
290+
4. Ensure the `DdFetchLambdaTags`, `DdFetchStepFunctionsTags`, and `DdFetchS3Tags` options are disabled, because AWS VPC does not yet offer an endpoint for the Resource Groups Tagging API.
291291
5. If you are using HAProxy or NGINX:
292292
293293
- Set `DdApiUrl` to `http://<proxy_host>:3834` or `https://<proxy_host>:3834`.
@@ -404,17 +404,23 @@ To test different patterns against your logs, turn on [debug logs](#troubleshoot
404404
405405
### Advanced (optional)
406406
407+
`DdEnrichS3Tags`
408+
: Enabled by default. When enabled, instructs the Datadog backend to automatically enrich logs originating from S3 buckets with the tags associated with those buckets. This approach offers the same tag enrichment as `DdFetchS3Tags` but defers the operation after log ingestion, reducing Forwarder overhead. Requires [Resource Collection](https://docs.datadoghq.com/integrations/amazon-web-services/#resource-collection) to be enabled in your AWS integration.
409+
410+
`DdEnrichCloudwatchTags`
411+
: Enabled by default. When enabled, instructs the Datadog backend to automatically enrich logs originating from CloudWatch LogGroups with the tags associated with those log groups. This approach offers the same tag enrichment as `DdFetchLogGroupTags` but defers the operation after log ingestion, reducing Forwarder overhead. Requires [Resource Collection](https://docs.datadoghq.com/integrations/amazon-web-services/#resource-collection) to be enabled in your AWS integration.
412+
407413
`DdFetchLambdaTags`
408414
: Let the Forwarder fetch Lambda tags using GetResources API calls and apply them to logs, metrics, and traces. If set to true, permission `tag:GetResources` will be automatically added to the Lambda execution IAM role.
409415
410416
`DdFetchLogGroupTags`
411-
: Let the forwarder fetch Log Group tags using ListTagsLogGroup and apply them to logs, metrics, and traces. If set to true, permission `logs:ListTagsForResource` will be automatically added to the Lambda execution IAM role.
417+
: **[DEPRECATED, use DdEnrichCloudwatchTags]** Let the forwarder fetch Log Group tags using ListTagsLogGroup and apply them to logs, metrics, and traces. If set to true, permission `logs:ListTagsForResource` will be automatically added to the Lambda execution IAM role.
412418
413419
`DdFetchStepFunctionsTags`
414420
: Let the Forwarder fetch Step Functions tags using GetResources API calls and apply them to logs and traces (if Step Functions tracing is enabled). If set to true, permission `tag:GetResources` will be automatically added to the Lambda execution IAM role.
415421
416422
`DdFetchS3Tags`
417-
: Let the Forwarder fetch S3 tags using GetResources API calls and apply them to logs and traces. If set to true, permission `tag:GetResources` will be automatically added to the Lambda execution IAM role.
423+
: **[DEPRECATED, use DdEnrichS3Tags]** Let the Forwarder fetch S3 tags using GetResources API calls and apply them to logs and traces. If set to true, permission `tag:GetResources` will be automatically added to the Lambda execution IAM role.
418424
419425
`DdStepFunctionsTraceEnabled`
420426
: Set to true to enable tracing for all Step Functions.
@@ -567,11 +573,17 @@ To test different patterns against your logs, turn on [debug logs](#troubleshoot
567573
568574
### Advanced (optional)
569575
576+
`DD_ENRICH_S3_TAGS`
577+
: Enabled by default. When enabled, instructs the Datadog backend to automatically enrich logs originating from S3 buckets with the tags associated with those buckets. This approach offers the same tag enrichment as `DD_FETCH_S3_TAGS` but defers the operation after log ingestion, reducing Forwarder overhead. Requires https://docs.datadoghq.com/integrations/amazon-web-services/#resource-collection to be enabled in your AWS integration.
578+
579+
`DD_ENRICH_CLOUDWATCH_TAGS`
580+
: Enabled by default. When enabled, instructs the Datadog backend to automatically enrich logs originating from Cloudwatch LogGroup with the tags associated with those log groups. This approach offers the same tag enrichment as `DD_FETCH_LOG_GROUP_TAGS` but defers the operation after log ingestion, reducing Forwarder overhead. Requires https://docs.datadoghq.com/integrations/amazon-web-services/#resource-collection to be enabled in your AWS integration.
581+
570582
`DD_FETCH_LAMBDA_TAGS`
571583
: Let the Forwarder fetch Lambda tags using GetResources API calls and apply them to logs, metrics, and traces. If set to true, permission `tag:GetResources` will be automatically added to the Lambda execution IAM role.
572584
573585
`DD_FETCH_LOG_GROUP_TAGS`
574-
: Let the forwarder fetch Log Group tags using ListTagsLogGroup and apply them to logs, metrics, and traces. If set to true, permission `logs:ListTagsForResource` will be automatically added to the Lambda execution IAM role.
586+
: [DEPRECATED, use DD_ENRICH_CLOUDWATCH_TAGS] Let the forwarder fetch Log Group tags using ListTagsLogGroup and apply them to logs, metrics, and traces. If set to true, permission `logs:ListTagsForResource` will be automatically added to the Lambda execution IAM role.
575587
576588
`DD_FETCH_STEP_FUNCTIONS_TAGS`
577589
: Let the Forwarder fetch Step Functions tags using GetResources API calls and apply them to logs and traces (if Step Functions tracing is enabled). If set to true, permission `tag:GetResources` will be automatically added to the Lambda execution IAM role.

aws/logs_monitoring/logs/datadog_http_client.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,41 @@
44
# Copyright 2021 Datadog, Inc.
55

66

7-
import os
87
import logging
9-
8+
import os
109
from concurrent.futures import as_completed
10+
1111
from requests_futures.sessions import FuturesSession
12-
from logs.helpers import compress_logs
13-
from logs.exceptions import ScrubbingException
1412

13+
from logs.exceptions import ScrubbingException
14+
from logs.helpers import compress_logs
1515
from settings import (
16-
DD_USE_COMPRESSION,
1716
DD_COMPRESSION_LEVEL,
18-
DD_MAX_WORKERS,
1917
DD_FORWARDER_VERSION,
18+
DD_MAX_WORKERS,
19+
DD_USE_COMPRESSION,
20+
get_enrich_cloudwatch_tags,
21+
get_enrich_s3_tags,
2022
)
2123

2224
logger = logging.getLogger()
2325
logger.setLevel(logging.getLevelName(os.environ.get("DD_LOG_LEVEL", "INFO").upper()))
2426

2527

28+
def get_dd_storage_tag_header():
29+
storage_tag = ""
30+
31+
if get_enrich_s3_tags():
32+
storage_tag += "s3"
33+
34+
if get_enrich_cloudwatch_tags():
35+
if storage_tag != "":
36+
storage_tag += ","
37+
storage_tag += "cloudwatch"
38+
39+
return storage_tag
40+
41+
2642
class DatadogHTTPClient(object):
2743
"""
2844
Client that sends a batch of logs over HTTP.
@@ -37,6 +53,10 @@ class DatadogHTTPClient(object):
3753
_HEADERS["DD-EVP-ORIGIN"] = "aws_forwarder"
3854
_HEADERS["DD-EVP-ORIGIN-VERSION"] = DD_FORWARDER_VERSION
3955

56+
storage_tag = get_dd_storage_tag_header()
57+
if storage_tag != "":
58+
_HEADERS["DD-STORAGE-TAG"] = storage_tag
59+
4060
def __init__(
4161
self, host, port, no_ssl, skip_ssl_validation, api_key, scrubber, timeout=10
4262
):

aws/logs_monitoring/settings.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,23 @@ def __init__(self, name, pattern, placeholder, enabled=True):
250250
)
251251

252252

253+
DD_ENRICH_S3_TAGS = get_env_var("DD_ENRICH_S3_TAGS", default="true", boolean=True)
254+
255+
DD_ENRICH_CLOUDWATCH_TAGS = get_env_var(
256+
"DD_ENRICH_CLOUDWATCH_TAGS", default="true", boolean=True
257+
)
258+
259+
if DD_FETCH_S3_TAGS and DD_ENRICH_S3_TAGS:
260+
logger.warn(
261+
"Enabling both DD_FETCH_S3_TAGS and DD_ENRICH_S3_TAGS might be unwanted"
262+
)
263+
264+
if DD_FETCH_LOG_GROUP_TAGS and DD_ENRICH_CLOUDWATCH_TAGS:
265+
logger.warn(
266+
"Enabling both DD_FETCH_LOG_GROUP_TAGS and DD_ENRICH_CLOUDWATCH_TAGS might be unwanted"
267+
)
268+
269+
253270
def get_fetch_s3_tags():
254271
return DD_FETCH_S3_TAGS
255272

@@ -266,6 +283,14 @@ def get_fetch_step_functions_tags():
266283
return DD_FETCH_STEP_FUNCTIONS_TAGS
267284

268285

286+
def get_enrich_s3_tags():
287+
return DD_ENRICH_S3_TAGS
288+
289+
290+
def get_enrich_cloudwatch_tags():
291+
return DD_ENRICH_CLOUDWATCH_TAGS
292+
293+
269294
DD_SOURCE = "ddsource"
270295
DD_CUSTOM_TAGS = "ddtags"
271296
DD_SERVICE = "service"

aws/logs_monitoring/template.yaml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ Parameters:
7575
Type: String
7676
Default: ""
7777
Description: Add custom tags to forwarded logs, comma-delimited string, no trailing comma, e.g., env:prod,stack:classic
78+
DdEnrichS3Tags:
79+
Type: String
80+
Default: true
81+
AllowedValues:
82+
- true
83+
- false
84+
Description: Instruct Datadog backend to enrich a log coming from a S3 bucket with the tag attached to this bucket. Datadog AWS Resource Collection needs to be enabled.
85+
DdEnrichCloudwatchTags:
86+
Type: String
87+
Default: true
88+
AllowedValues:
89+
- true
90+
- false
91+
Description: Instruct Datadog backend to enrich a log coming from a Cloudwatch logGroup with the tag attached to this logGroup. Datadog AWS Resource Collection needs to be enabled.
7892
DdFetchLambdaTags:
7993
Type: String
8094
Default: true
@@ -88,7 +102,7 @@ Parameters:
88102
AllowedValues:
89103
- true
90104
- false
91-
Description: Let the forwarder fetch Log Group tags using ListTagsLogGroup and apply them to logs, metrics and traces. If set to true, permission logs:ListTagsLogGroup will be automatically added to the Lambda execution IAM role. The tags are cached in memory and S3 so that they'll only be fetched when the function cold starts or when the TTL (1 hour) expires. The forwarder increments the aws.lambda.enhanced.list_tags_log_group_api_call metric for each API call made.
105+
Description: (DEPRECATED in favor of DdEnrichCloudwatchTags) Let the forwarder fetch Log Group tags using ListTagsLogGroup and apply them to logs, metrics and traces. If set to true, permission logs:ListTagsLogGroup will be automatically added to the Lambda execution IAM role. The tags are cached in memory and S3 so that they'll only be fetched when the function cold starts or when the TTL (1 hour) expires. The forwarder increments the aws.lambda.enhanced.list_tags_log_group_api_call metric for each API call made.
92106
DdFetchStepFunctionsTags:
93107
Type: String
94108
Default: true
@@ -98,11 +112,11 @@ Parameters:
98112
Description: Let the forwarder fetch Step Functions tags using GetResources API calls and apply them to logs, metrics and traces. If set to true, permission tag:GetResources will be automatically added to the Lambda execution IAM role. The tags are cached in memory and S3 so that they'll only be fetched when the function cold starts or when the TTL (1 hour) expires. The forwarder increments the aws.lambda.enhanced.get_resources_api_calls metric for each API call made.
99113
DdFetchS3Tags:
100114
Type: String
101-
Default: true
115+
Default: false
102116
AllowedValues:
103117
- true
104118
- false
105-
Description: Let the forwarder fetch S3 buckets tags using GetResources API calls and apply them to S3 based logs. If set to true, permission tag:GetResources will be automatically added to the Lambda execution IAM role. The tags are cached in memory and S3 so that they'll only be fetched when the function cold starts or when the TTL (1 hour) expires. The forwarder increments the aws.lambda.enhanced.get_resources_api_calls metric for each API call made.
119+
Description: (DEPRECATED in favor of DdEnrichS3Tags) Let the forwarder fetch S3 buckets tags using GetResources API calls and apply them to S3 based logs. If set to true, permission tag:GetResources will be automatically added to the Lambda execution IAM role. The tags are cached in memory and S3 so that they'll only be fetched when the function cold starts or when the TTL (1 hour) expires. The forwarder increments the aws.lambda.enhanced.get_resources_api_calls metric for each API call made.
106120
DdNoSsl:
107121
Type: String
108122
Default: false
@@ -448,11 +462,13 @@ Resources:
448462
- !Ref DdTags
449463
- !Ref AWS::NoValue
450464
DD_TAGS_CACHE_TTL_SECONDS: !Ref TagsCacheTTLSeconds
465+
DD_ENRICH_S3_TAGS: !Ref DdEnrichS3Tags
466+
DD_ENRICH_CLOUDWATCH_TAGS: !Ref DdEnrichCloudwatchTags
467+
DD_FETCH_S3_TAGS: !Ref DdFetchS3Tags
451468
DD_FETCH_LAMBDA_TAGS: !If
452469
- SetDdFetchLambdaTags
453470
- !Ref DdFetchLambdaTags
454471
- !Ref AWS::NoValue
455-
DD_FETCH_S3_TAGS: !Ref DdFetchS3Tags
456472
DD_FETCH_LOG_GROUP_TAGS: !If
457473
- SetDdFetchLogGroupTags
458474
- !Ref DdFetchLogGroupTags
@@ -1018,6 +1034,8 @@ Metadata:
10181034
- Label:
10191035
default: Advanced (Optional)
10201036
Parameters:
1037+
- DdEnrichS3Tags
1038+
- DdEnrichCloudwatchTags
10211039
- DdFetchLambdaTags
10221040
- DdFetchLogGroupTags
10231041
- DdFetchStepFunctionsTags

aws/logs_monitoring/tools/build_bundle.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ docker_build_zip() {
6464
# between different python runtimes.
6565
temp_dir=$(mktemp -d)
6666

67-
docker buildx build --platform linux/arm64 --file "${DIR}/Dockerfile_bundle" -t "datadog-bundle:$1" .. --no-cache --build-arg "runtime=${PYTHON_VERSION}"
67+
docker buildx build --platform linux/arm64 --file "${DIR}/Dockerfile_bundle" -t "datadog-bundle:$1" .. --no-cache --build-arg "runtime=${PYTHON_VERSION}"
6868

6969
# Run the image by runtime tag, tar its generated `python` directory to sdout,
7070
# then extract it to a temp directory.

aws/logs_monitoring/tools/integration_tests/docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ services:
4040
DD_USE_COMPRESSION: "false"
4141
DD_ADDITIONAL_TARGET_LAMBDAS: "${EXTERNAL_LAMBDAS}"
4242
DD_S3_BUCKET_NAME: "${DD_S3_BUCKET_NAME}"
43-
DD_FETCH_LAMBDA_TAGS: "true"
44-
DD_FETCH_LOG_GROUP_TAGS: "true"
45-
DD_FETCH_STEP_FUNCTIONS_TAGS: "false" # intentionally set false to allow integration test for step function logs to run without hitting aws
43+
DD_FETCH_LAMBDA_TAGS: "${DD_FETCH_LAMBDA_TAGS:-false}"
44+
DD_FETCH_LOG_GROUP_TAGS: "${DD_FETCH_LOG_GROUP_TAGS:-false}"
45+
DD_FETCH_STEP_FUNCTIONS_TAGS: "${DD_FETCH_STEP_FUNCTIONS_TAGS:-false}"
4646
DD_STORE_FAILED_EVENTS: "false"
4747
DD_TRACE_ENABLED: "true"
4848
expose:

aws/logs_monitoring/tools/integration_tests/integration_tests.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
# Unless explicitly stated otherwise all files in this repository are licensed
44
# under the Apache License Version 2.0.
@@ -21,6 +21,7 @@ SNAPS=($SNAPSHOT_DIR)
2121
ADDITIONAL_LAMBDA=false
2222
CACHE_TEST=false
2323
DD_FETCH_LAMBDA_TAGS="true"
24+
DD_FETCH_LOG_GROUP_TAGS="true"
2425
DD_FETCH_STEP_FUNCTIONS_TAGS="true"
2526

2627
script_start_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
@@ -37,7 +38,6 @@ for arg in "$@"; do
3738
shift
3839
;;
3940

40-
4141
# -u or --update
4242
# Update the snapshots to reflect this test run
4343
-u | --update)
@@ -152,6 +152,7 @@ LOG_LEVEL=${LOG_LEVEL} \
152152
AWS_ACCOUNT_ID=${AWS_ACCOUNT_ID} \
153153
SNAPSHOTS_DIR_NAME="./${SNAPSHOTS_DIR_NAME}" \
154154
DD_FETCH_LAMBDA_TAGS=${DD_FETCH_LAMBDA_TAGS} \
155+
DD_FETCH_LOG_GROUP_TAGS=${DD_FETCH_LOG_GROUP_TAGS} \
155156
DD_FETCH_STEP_FUNCTIONS_TAGS=${DD_FETCH_STEP_FUNCTIONS_TAGS} \
156157
docker compose up --build --abort-on-container-exit
157158

aws/logs_monitoring/tools/integration_tests/snapshots/cloudwatch_customized_log_group_lambda_invocation.json~snapshot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"DD-API-KEY": "abcdefghijklmnopqrstuvwxyz012345",
7676
"DD-EVP-ORIGIN": "aws_forwarder",
7777
"DD-EVP-ORIGIN-VERSION": "<redacted from snapshot>",
78+
"DD-STORAGE-TAG": "s3,cloudwatch",
7879
"Host": "recorder:8080",
7980
"User-Agent": "<redacted from snapshot>",
8081
"traceparent": "<redacted from snapshot>",

aws/logs_monitoring/tools/integration_tests/snapshots/cloudwatch_log.json~snapshot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"DD-API-KEY": "abcdefghijklmnopqrstuvwxyz012345",
4949
"DD-EVP-ORIGIN": "aws_forwarder",
5050
"DD-EVP-ORIGIN-VERSION": "<redacted from snapshot>",
51+
"DD-STORAGE-TAG": "s3,cloudwatch",
5152
"Host": "recorder:8080",
5253
"User-Agent": "<redacted from snapshot>",
5354
"traceparent": "<redacted from snapshot>",

aws/logs_monitoring/tools/integration_tests/snapshots/cloudwatch_log_cloudtrail.json~snapshot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"DD-API-KEY": "abcdefghijklmnopqrstuvwxyz012345",
8585
"DD-EVP-ORIGIN": "aws_forwarder",
8686
"DD-EVP-ORIGIN-VERSION": "<redacted from snapshot>",
87+
"DD-STORAGE-TAG": "s3,cloudwatch",
8788
"Host": "recorder:8080",
8889
"User-Agent": "<redacted from snapshot>",
8990
"traceparent": "<redacted from snapshot>",

0 commit comments

Comments
 (0)