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

[AWSX-678] Restructure files in the lambda forwarder #742

Merged
merged 8 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import boto3

from base_tags_cache import (
from caching.base_tags_cache import (
BaseTagsCache,
logger,
sanitize_aws_tag_string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from botocore.exceptions import ClientError

from base_tags_cache import (
from caching.base_tags_cache import (
GET_RESOURCES_LAMBDA_FILTER,
BaseTagsCache,
logger,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from botocore.exceptions import ClientError

from base_tags_cache import (
from caching.base_tags_cache import (
BaseTagsCache,
logger,
parse_get_resources_response_for_tags_by_arn,
Expand Down
4 changes: 1 addition & 3 deletions aws/logs_monitoring/enhanced_lambda_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
import logging
import re
import datetime

from time import time

from lambda_cache import LambdaTagsCache
from caching.lambda_cache import LambdaTagsCache

ENHANCED_METRICS_NAMESPACE_PREFIX = "aws.lambda.enhanced"

Expand Down
119 changes: 119 additions & 0 deletions aws/logs_monitoring/forwarder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import logging
import json
import os

from telemetry import (
DD_FORWARDER_TELEMETRY_NAMESPACE_PREFIX,
get_forwarder_telemetry_tags,
)
from datadog_lambda.metric import lambda_stats
from trace_forwarder.connection import TraceConnection
from logs.logs import (
DatadogScrubber,
DatadogBatcher,
DatadogClient,
DatadogHTTPClient,
DatadogTCPClient,
)
from logs.logs_helpers import filter_logs
from settings import (
DD_API_KEY,
DD_USE_TCP,
DD_NO_SSL,
DD_SKIP_SSL_VALIDATION,
DD_URL,
DD_PORT,
SCRUBBING_RULE_CONFIGS,
INCLUDE_AT_MATCH,
EXCLUDE_AT_MATCH,
DD_TRACE_INTAKE_URL,
)

logger = logging.getLogger()
logger.setLevel(logging.getLevelName(os.environ.get("DD_LOG_LEVEL", "INFO").upper()))
trace_connection = TraceConnection(
DD_TRACE_INTAKE_URL, DD_API_KEY, DD_SKIP_SSL_VALIDATION
)


def forward_logs(logs):
"""Forward logs to Datadog"""
if logger.isEnabledFor(logging.DEBUG):
logger.debug(f"Forwarding {len(logs)} logs")
logs_to_forward = filter_logs(
[json.dumps(log, ensure_ascii=False) for log in logs],
include_pattern=INCLUDE_AT_MATCH,
exclude_pattern=EXCLUDE_AT_MATCH,
)
scrubber = DatadogScrubber(SCRUBBING_RULE_CONFIGS)
if DD_USE_TCP:
batcher = DatadogBatcher(256 * 1000, 256 * 1000, 1)
cli = DatadogTCPClient(DD_URL, DD_PORT, DD_NO_SSL, DD_API_KEY, scrubber)
else:
batcher = DatadogBatcher(512 * 1000, 4 * 1000 * 1000, 400)
cli = DatadogHTTPClient(
DD_URL, DD_PORT, DD_NO_SSL, DD_SKIP_SSL_VALIDATION, DD_API_KEY, scrubber
)

with DatadogClient(cli) as client:
for batch in batcher.batch(logs_to_forward):
try:
client.send(batch)
except Exception:
logger.exception(f"Exception while forwarding log batch {batch}")
else:
if logger.isEnabledFor(logging.DEBUG):
logger.debug(f"Forwarded log batch: {json.dumps(batch)}")

lambda_stats.distribution(
"{}.logs_forwarded".format(DD_FORWARDER_TELEMETRY_NAMESPACE_PREFIX),
len(logs_to_forward),
tags=get_forwarder_telemetry_tags(),
)


def forward_metrics(metrics):
"""
Forward custom metrics submitted via logs to Datadog in a background thread
using `lambda_stats` that is provided by the Datadog Python Lambda Layer.
"""
if logger.isEnabledFor(logging.DEBUG):
logger.debug(f"Forwarding {len(metrics)} metrics")

for metric in metrics:
try:
lambda_stats.distribution(
metric["m"], metric["v"], timestamp=metric["e"], tags=metric["t"]
)
except Exception:
logger.exception(f"Exception while forwarding metric {json.dumps(metric)}")
else:
if logger.isEnabledFor(logging.DEBUG):
logger.debug(f"Forwarded metric: {json.dumps(metric)}")

lambda_stats.distribution(
"{}.metrics_forwarded".format(DD_FORWARDER_TELEMETRY_NAMESPACE_PREFIX),
len(metrics),
tags=get_forwarder_telemetry_tags(),
)


def forward_traces(trace_payloads):
if logger.isEnabledFor(logging.DEBUG):
logger.debug(f"Forwarding {len(trace_payloads)} traces")

try:
trace_connection.send_traces(trace_payloads)
except Exception:
logger.exception(
f"Exception while forwarding traces {json.dumps(trace_payloads)}"
)
else:
if logger.isEnabledFor(logging.DEBUG):
logger.debug(f"Forwarded traces: {json.dumps(trace_payloads)}")

lambda_stats.distribution(
"{}.traces_forwarded".format(DD_FORWARDER_TELEMETRY_NAMESPACE_PREFIX),
len(trace_payloads),
tags=get_forwarder_telemetry_tags(),
)
Loading
Loading