Skip to content

Commit

Permalink
Changing span unsampled flag to be sampled flag (#260)
Browse files Browse the repository at this point in the history
*Issue #, if available:*

Previously we use `aws.trace.flag.unsampled` mark the span is unsampled.
The change updates this attribute to be `aws.trace.flag.sampled`.


*Description of changes:*


#### span is unsampled:
If span has no span attribute aws.trace.flag.sampled , value type is
boolean and value is false.

#### span is sampled:
If span has no span attribute aws.trace.flag.sampled
or span has span attribute aws.trace.flag.sampled , value type is
boolean and value is true,
or span has span attribute aws.trace.flag.sampled , value type is NOT
boolean

By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.
  • Loading branch information
wangzlei authored Sep 19, 2024
1 parent 2064f2d commit b1fcdb3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
AWS_REMOTE_RESOURCE_IDENTIFIER: str = "aws.remote.resource.identifier"
AWS_SDK_DESCENDANT: str = "aws.sdk.descendant"
AWS_CONSUMER_PARENT_SPAN_KIND: str = "aws.consumer.parent.span.kind"
AWS_TRACE_FLAG_UNSAMPLED: str = "aws.trace.flag.unsampled"
AWS_TRACE_FLAG_SAMPLED: str = "aws.trace.flag.sampled"

# AWS_#_NAME attributes are not supported in python as they are not part of the Semantic Conventions.
# TODO:Move to Semantic Conventions when these attributes are added.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
import logging
from typing import Optional

from amazon.opentelemetry.distro._aws_attribute_keys import AWS_TRACE_FLAG_UNSAMPLED
from amazon.opentelemetry.distro._aws_attribute_keys import AWS_TRACE_FLAG_SAMPLED
from opentelemetry.context import Context
from opentelemetry.sdk.trace import ReadableSpan, Span
from opentelemetry.sdk.trace.export import BatchSpanProcessor as BaseBatchSpanProcessor

logger = logging.getLogger(__name__)

SPANUNSAMPLED_FLAG = "OTEL_AWS_APP_SIGNALS_ENABLED"


class BatchUnsampledSpanProcessor(BaseBatchSpanProcessor):

# pylint: disable=no-self-use
def on_start(self, span: Span, parent_context: Optional[Context] = None) -> None:
if not span.context.trace_flags.sampled:
span.set_attribute(AWS_TRACE_FLAG_UNSAMPLED, True)
span.set_attribute(AWS_TRACE_FLAG_SAMPLED, False)

def on_end(self, span: ReadableSpan) -> None:
if span.context.trace_flags.sampled:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest import TestCase
from unittest.mock import MagicMock, patch

from amazon.opentelemetry.distro._aws_attribute_keys import AWS_TRACE_FLAG_UNSAMPLED
from amazon.opentelemetry.distro._aws_attribute_keys import AWS_TRACE_FLAG_SAMPLED
from amazon.opentelemetry.distro.aws_batch_unsampled_span_processor import BatchUnsampledSpanProcessor
from opentelemetry.trace import TraceFlags

Expand Down Expand Up @@ -43,7 +43,7 @@ def test_on_end_not_sampled(self, mock_span_class):
self.processor.on_end(mock_span2)

self.assertEqual(len(self.processor.queue), 1)
self.assertIn(AWS_TRACE_FLAG_UNSAMPLED, mock_span1.set_attribute.call_args_list[0][0][0])
self.assertIn(AWS_TRACE_FLAG_SAMPLED, mock_span1.set_attribute.call_args_list[0][0][0])

self.processor.shutdown()
mock_span2 = mock_span_class.return_value
Expand Down
7 changes: 7 additions & 0 deletions lambda-layer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ cd lambda-layer

Once the script has successfully run, you will see the deployed Lambda sample app in your AWS account. You can trigger the
Lambda function and view the traces and metrics through the AWS CloudWatch Console.

## Configuration

By default the layer enable botocore and aws-lambda instrumentation libraries only for better Lambda cold start performance. To
enable all opentelemetry python
supported libraries you can set environment variable `OTEL_PYTHON_DISABLED_INSTRUMENTATIONS=none`. Refer to details in
[OpenTelemetry Python Disabling Specific Instrumentations](Disabling Specific Instrumentations)

0 comments on commit b1fcdb3

Please sign in to comment.