Skip to content

Commit 75af98a

Browse files
authored
Added ForceFlush Implementation (#272)
### Description: Passed the force_flush function from MeteringProvider to the AwsSpanMetricProcessor to forceFlush remaining metrics on shutdown to the cwAgent/Collector. Compared to java/dotnet, looks like python already flushes all the metrics and traces on shutdown even without the force flush implementation. I'm adding it tho for consistency with other languages. ### Tesing: Increased the metricExporter interval and the BatchSpanProcessor delay to 10 minutes using: ``` OTEL_METRIC_EXPORT_INTERVAL=600000 \ OTEL_BSP_SCHEDULE_DELAY=600000 \ ``` With and without the force flush implementation, exiting the [sample app](https://github.com/aws-observability/aws-otel-python-instrumentation/blob/main/sample-applications/simple-client-server/server_automatic_s3client.py) flushed both the traces and the metrics to the collector. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 4f36d47 commit 75af98a

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_span_metrics_processor.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3-
from typing import Dict, Optional
3+
from typing import Callable, Dict, Optional
44

55
from typing_extensions import override
66

@@ -45,19 +45,29 @@ class AwsSpanMetricsProcessor(SpanProcessor):
4545
_generator: MetricAttributeGenerator
4646
_resource: Resource
4747

48+
_force_flush_function: Callable
49+
50+
# no op function to act as a default function in case forceFlushFunction was
51+
# not supplied to the the constructor.
52+
# pylint: disable=no-self-use
53+
def _no_op_function(self, timeout_millis: float = None) -> bool:
54+
return True
55+
4856
def __init__(
4957
self,
5058
error_histogram: Histogram,
5159
fault_histogram: Histogram,
5260
latency_histogram: Histogram,
5361
generator: MetricAttributeGenerator,
5462
resource: Resource,
63+
force_flush_function: Callable = _no_op_function,
5564
):
5665
self._error_histogram = error_histogram
5766
self._fault_histogram = fault_histogram
5867
self._latency_histogram = latency_histogram
5968
self._generator = generator
6069
self._resource = resource
70+
self._force_flush_function = force_flush_function
6171

6272
# pylint: disable=no-self-use
6373
@override
@@ -78,8 +88,8 @@ def shutdown(self) -> None:
7888

7989
# pylint: disable=no-self-use
8090
@override
81-
def force_flush(self, timeout_millis: int = None) -> bool:
82-
return True
91+
def force_flush(self, timeout_millis: float = 10_000) -> bool:
92+
return self._force_flush_function(timeout_millis)
8393

8494
def _record_metrics(self, span: ReadableSpan, attributes: BoundedAttributes) -> None:
8595
# Only record metrics if non-empty attributes are returned.

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/aws_span_metrics_processor_builder.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,10 @@ def build(self) -> AwsSpanMetricsProcessor:
6363
latency_histogram.name = _LATENCY
6464

6565
return AwsSpanMetricsProcessor(
66-
error_histogram, fault_histogram, latency_histogram, self._generator, self._resource
66+
error_histogram,
67+
fault_histogram,
68+
latency_histogram,
69+
self._generator,
70+
self._resource,
71+
self._meter_provider.force_flush,
6772
)

0 commit comments

Comments
 (0)