Skip to content

Commit 07f8146

Browse files
kuba-wuNathanielRN
andauthored
lambda instrumentation - support for flush timeout (#825)
* lambda instrumentation - support for flush timeout * Update instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py Co-authored-by: (Eliseo) Nathaniel Ruiz Nowell <[email protected]> * Update instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py Co-authored-by: (Eliseo) Nathaniel Ruiz Nowell <[email protected]> * fixing lint * fixing django lint Co-authored-by: (Eliseo) Nathaniel Ruiz Nowell <[email protected]>
1 parent 444e0a1 commit 07f8146

File tree

6 files changed

+54
-2
lines changed

6 files changed

+54
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.7.0-0.26b0...HEAD)
99

10+
### Added
11+
12+
- `opentelemetry-instrumentation-aws-lambda` Adds support for configurable flush timeout via `OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT` property. ([#825](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/825))
13+
1014
### Fixed
1115

1216
- `opentelemetry-exporter-richconsole` Fixed attribute error on parentless spans.

instrumentation/opentelemetry-instrumentation-aws-lambda/src/opentelemetry/instrumentation/aws_lambda/__init__.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ def custom_event_context_extractor(lambda_event):
9797
_HANDLER = "_HANDLER"
9898
_X_AMZN_TRACE_ID = "_X_AMZN_TRACE_ID"
9999
ORIG_HANDLER = "ORIG_HANDLER"
100+
OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT = (
101+
"OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT"
102+
)
100103

101104

102105
def _default_event_context_extractor(lambda_event: Any) -> Context:
@@ -167,6 +170,7 @@ def _determine_parent_context(
167170
def _instrument(
168171
wrapped_module_name,
169172
wrapped_function_name,
173+
flush_timeout,
170174
event_context_extractor: Callable[[Any], Context],
171175
tracer_provider: TracerProvider = None,
172176
):
@@ -222,7 +226,7 @@ def _instrumented_lambda_handler_call(
222226
# NOTE: `force_flush` before function quit in case of Lambda freeze.
223227
# Assumes we are using the OpenTelemetry SDK implementation of the
224228
# `TracerProvider`.
225-
_tracer_provider.force_flush()
229+
_tracer_provider.force_flush(flush_timeout)
226230
except Exception: # pylint: disable=broad-except
227231
logger.error(
228232
"TracerProvider was missing `force_flush` method. This is necessary in case of a Lambda freeze and would exist in the OTel SDK implementation."
@@ -262,9 +266,22 @@ def _instrument(self, **kwargs):
262266
self._wrapped_function_name,
263267
) = lambda_handler.rsplit(".", 1)
264268

269+
flush_timeout_env = os.environ.get(
270+
OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT, ""
271+
)
272+
flush_timeout = 30000
273+
try:
274+
flush_timeout = int(flush_timeout_env)
275+
except ValueError:
276+
logger.warning(
277+
"Could not convert OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT value %s to int",
278+
flush_timeout_env,
279+
)
280+
265281
_instrument(
266282
self._wrapped_module_name,
267283
self._wrapped_function_name,
284+
flush_timeout,
268285
event_context_extractor=kwargs.get(
269286
"event_context_extractor", _default_event_context_extractor
270287
),

instrumentation/opentelemetry-instrumentation-aws-lambda/tests/test_aws_lambda_instrumentation_manual.py

+28
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from opentelemetry.instrumentation.aws_lambda import (
2020
_HANDLER,
2121
_X_AMZN_TRACE_ID,
22+
OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT,
2223
AwsLambdaInstrumentor,
2324
)
2425
from opentelemetry.propagate import get_global_textmap
@@ -246,3 +247,30 @@ def custom_event_context_extractor(lambda_event):
246247
self.assertTrue(parent_context.is_remote)
247248

248249
test_env_patch.stop()
250+
251+
def test_lambda_no_error_with_invalid_flush_timeout(self):
252+
253+
test_env_patch = mock.patch.dict(
254+
"os.environ",
255+
{
256+
**os.environ,
257+
# NOT Active Tracing
258+
_X_AMZN_TRACE_ID: MOCK_XRAY_TRACE_CONTEXT_NOT_SAMPLED,
259+
# NOT using the X-Ray Propagator
260+
OTEL_PROPAGATORS: "tracecontext",
261+
OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT: "invalid-timeout-string",
262+
},
263+
)
264+
test_env_patch.start()
265+
266+
AwsLambdaInstrumentor().instrument()
267+
268+
mock_execute_lambda()
269+
270+
spans = self.memory_exporter.get_finished_spans()
271+
272+
assert spans
273+
274+
self.assertEqual(len(spans), 1)
275+
276+
test_env_patch.stop()

instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def __call__(self, request):
5858
response = self.get_response(request)
5959
return self.process_response(request, response)
6060

61-
6261
else:
6362
# Django versions 1.x can use `settings.MIDDLEWARE_CLASSES` and expect
6463
# old-style middlewares, which are created by inheriting from

instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# pylint: disable=E0611
16+
1517
from sys import modules
1618
from unittest.mock import Mock, patch
1719

instrumentation/opentelemetry-instrumentation-django/tests/test_middleware_asgi.py

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# pylint: disable=E0611
16+
1517
from sys import modules
1618
from unittest.mock import Mock, patch
1719

0 commit comments

Comments
 (0)