|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +import json |
| 6 | +from logging import getLogger |
| 7 | +from threading import Timer |
| 8 | +from typing import Optional, Sequence |
| 9 | + |
| 10 | +from amazon.opentelemetry.distro.sampler.aws_xray_sampling_client import AwsXRaySamplingClient |
| 11 | +from opentelemetry.context import Context |
| 12 | +from opentelemetry.sdk.resources import Resource |
| 13 | +from opentelemetry.sdk.trace.sampling import ALWAYS_OFF, Sampler, SamplingResult |
| 14 | +from opentelemetry.trace import Link, SpanKind |
| 15 | +from opentelemetry.trace.span import TraceState |
| 16 | +from opentelemetry.util.types import Attributes |
| 17 | + |
| 18 | +_logger = getLogger(__name__) |
| 19 | + |
| 20 | +DEFAULT_RULES_POLLING_INTERVAL = 300 |
| 21 | +DEFAULT_TARGET_POLLING_INTERVAL = 10 |
| 22 | +DEFAULT_SAMPLING_PROXY_ENDPOINT = "http://127.0.0.1:2000" |
| 23 | + |
| 24 | +class AwsXRayRemoteSampler(Sampler): |
| 25 | + """ |
| 26 | + Remote Sampler for OpenTelemetry that gets sampling configurations from AWS X-Ray |
| 27 | +
|
| 28 | + Args: |
| 29 | + resource: OpenTelemetry Resource (Optional) |
| 30 | + endpoint: proxy endpoint for AWS X-Ray Sampling (Optional) |
| 31 | + polling_interval: Polling interval for getSamplingRules call (Optional) |
| 32 | + log_level: custom log level configuration for remote sampler (Optional) |
| 33 | + """ |
| 34 | + |
| 35 | + __resource : Resource |
| 36 | + __polling_interval : int |
| 37 | + __xray_client : AwsXRaySamplingClient |
| 38 | + |
| 39 | + def __init__(self, resource=None, endpoint=DEFAULT_SAMPLING_PROXY_ENDPOINT, polling_interval=DEFAULT_RULES_POLLING_INTERVAL, log_level = None): |
| 40 | + # Override default log level |
| 41 | + if log_level is not None: |
| 42 | + _logger.setLevel(log_level) |
| 43 | + |
| 44 | + self.__xray_client = AwsXRaySamplingClient(endpoint, log_level=log_level) |
| 45 | + self.__polling_interval = polling_interval |
| 46 | + self.__resource = resource |
| 47 | + |
| 48 | + self.__start_sampling_rule_poller() |
| 49 | + |
| 50 | + def should_sample( |
| 51 | + self, |
| 52 | + parent_context: Optional["Context"], |
| 53 | + trace_id: int, |
| 54 | + name: str, |
| 55 | + kind: SpanKind = None, |
| 56 | + attributes: Attributes = None, |
| 57 | + links: Sequence["Link"] = None, |
| 58 | + trace_state: "TraceState" = None, |
| 59 | + ) -> "SamplingResult": |
| 60 | + # TODO: add sampling functionality |
| 61 | + return ALWAYS_OFF |
| 62 | + |
| 63 | + def get_description(self) -> str: |
| 64 | + description = "AwsXRayRemoteSampler{remote sampling with AWS X-Ray}" |
| 65 | + return description |
| 66 | + |
| 67 | + def __get_and_update_sampling_rules(self): |
| 68 | + sampling_rules = self.__xray_client.get_sampling_rules() |
| 69 | + |
| 70 | + # TODO: Update sampling rules cache |
| 71 | + _logger.info(f"Got Sampling Rules: {json.dumps([ob.__dict__ for ob in sampling_rules])}") |
| 72 | + |
| 73 | + def __start_sampling_rule_poller(self): |
| 74 | + self.__get_and_update_sampling_rules() |
| 75 | + # Schedule the next sampling rule poll |
| 76 | + self._timer = Timer(self.__polling_interval, self.__start_sampling_rule_poller) |
| 77 | + self._timer.daemon = True |
| 78 | + self._timer.start() |
| 79 | + |
0 commit comments