-
Notifications
You must be signed in to change notification settings - Fork 789
botocore: Add AWS_LAMBDA_RESOURCE_MAPPING_ID Semantic Convention Support for AWS Lambda SDK #3800
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
base: main
Are you sure you want to change the base?
Changes from all commits
905fcfe
2ef3c8b
83747fb
4f7f9fb
9b8cec5
bc3d1d2
03718d6
5c73a7f
3b65c20
b8f3a5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,18 +16,27 @@ | |
import inspect | ||
import json | ||
import re | ||
from typing import Dict | ||
from typing import Dict, Final | ||
|
||
from opentelemetry.instrumentation.botocore.extensions.types import ( | ||
_AttributeMapT, | ||
_AwsSdkCallContext, | ||
_AwsSdkExtension, | ||
_BotocoreInstrumentorContext, | ||
_BotoResultT, | ||
) | ||
from opentelemetry.propagate import inject | ||
from opentelemetry.semconv._incubating.attributes.aws_attributes import ( | ||
AWS_LAMBDA_RESOURCE_MAPPING_ID, | ||
) | ||
from opentelemetry.semconv.trace import SpanAttributes | ||
from opentelemetry.trace.span import Span | ||
|
||
# Work is underway to add these two keys to the SemConv AWS registry, in line with other AWS resources. | ||
# https://github.com/open-telemetry/semantic-conventions/blob/main/docs/registry/attributes/aws.md#amazon-lambda-attributes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We usually don't add attributes before they're added to semconv, do you want to wait for these to get there or move the usage of these attributes to another PR? |
||
AWS_LAMBDA_FUNCTION_ARN: Final = "aws.lambda.function.arn" | ||
AWS_LAMBDA_FUNCTION_NAME: Final = "aws.lambda.function.name" | ||
lukeina2z marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
class _LambdaOperation(abc.ABC): | ||
@classmethod | ||
|
@@ -68,12 +77,16 @@ def extract_attributes( | |
) | ||
attributes[SpanAttributes.FAAS_INVOKED_REGION] = call_context.region | ||
|
||
@classmethod | ||
def _parse_function_name(cls, call_context: _AwsSdkCallContext): | ||
@staticmethod | ||
def _parse_function_name(call_context: _AwsSdkCallContext): | ||
function_name_or_arn = call_context.params.get("FunctionName") | ||
lukeina2z marked this conversation as resolved.
Show resolved
Hide resolved
|
||
matches = cls.ARN_LAMBDA_PATTERN.match(function_name_or_arn) | ||
function_name = matches.group(1) | ||
return function_name_or_arn if function_name is None else function_name | ||
if function_name_or_arn is None: | ||
return None | ||
matches = _OpInvoke.ARN_LAMBDA_PATTERN.match(function_name_or_arn) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why you are moving to a staticmethod while still referencing class attributes but not an issue for me 😅 |
||
if matches: | ||
function_name = matches.group(1) | ||
return function_name if function_name else function_name_or_arn | ||
return function_name_or_arn | ||
|
||
@classmethod | ||
def before_service_call(cls, call_context: _AwsSdkCallContext, span: Span): | ||
|
@@ -115,6 +128,13 @@ def __init__(self, call_context: _AwsSdkCallContext): | |
self._op = _OPERATION_MAPPING.get(call_context.operation) | ||
|
||
def extract_attributes(self, attributes: _AttributeMapT): | ||
function_name = _OpInvoke._parse_function_name(self._call_context) | ||
if function_name: | ||
attributes[AWS_LAMBDA_FUNCTION_NAME] = function_name | ||
resource_mapping_id = self._call_context.params.get("UUID") | ||
if resource_mapping_id: | ||
attributes[AWS_LAMBDA_RESOURCE_MAPPING_ID] = resource_mapping_id | ||
|
||
if self._op is None: | ||
return | ||
|
||
|
@@ -127,3 +147,20 @@ def before_service_call( | |
return | ||
|
||
self._op.before_service_call(self._call_context, span) | ||
|
||
def on_success( | ||
self, | ||
span: Span, | ||
result: _BotoResultT, | ||
instrumentor_context: _BotocoreInstrumentorContext, | ||
): | ||
resource_mapping_id = result.get("UUID") | ||
if resource_mapping_id: | ||
span.set_attribute( | ||
AWS_LAMBDA_RESOURCE_MAPPING_ID, resource_mapping_id | ||
) | ||
|
||
lambda_configuration = result.get("Configuration", {}) | ||
function_arn = lambda_configuration.get("FunctionArn") | ||
if function_arn: | ||
span.set_attribute(AWS_LAMBDA_FUNCTION_ARN, function_arn) |
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.