Skip to content

Commit ab096b4

Browse files
committed
Move flush timeout calculation into its own function
1 parent a166208 commit ab096b4

File tree

2 files changed

+61
-14
lines changed

2 files changed

+61
-14
lines changed

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,31 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches
433433
)
434434

435435

436+
def determine_flush_timeout() -> int:
437+
"""
438+
Determine the flush timeout for the Lambda function.
439+
440+
Returns: Flush timeout in milliseconds
441+
"""
442+
443+
flush_timeout_env = os.environ.get(
444+
OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT, None
445+
)
446+
447+
flush_timeout = 30000
448+
449+
try:
450+
if flush_timeout_env is not None:
451+
flush_timeout = int(flush_timeout_env)
452+
except ValueError:
453+
logger.warning(
454+
"Could not convert OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT value %s to int",
455+
flush_timeout_env,
456+
)
457+
458+
return flush_timeout
459+
460+
436461
class AwsLambdaInstrumentor(BaseInstrumentor):
437462
def instrumentation_dependencies(self) -> Collection[str]:
438463
return _instruments
@@ -462,19 +487,6 @@ def _instrument(self, **kwargs):
462487
self._wrapped_function_name,
463488
) = lambda_handler.rsplit(".", 1)
464489

465-
flush_timeout_env = os.environ.get(
466-
OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT, None
467-
)
468-
flush_timeout = 30000
469-
try:
470-
if flush_timeout_env is not None:
471-
flush_timeout = int(flush_timeout_env)
472-
except ValueError:
473-
logger.warning(
474-
"Could not convert OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT value %s to int",
475-
flush_timeout_env,
476-
)
477-
478490
disable_aws_context_propagation = kwargs.get(
479491
"disable_aws_context_propagation", False
480492
) or os.getenv(
@@ -488,7 +500,7 @@ def _instrument(self, **kwargs):
488500
_instrument(
489501
self._wrapped_module_name,
490502
self._wrapped_function_name,
491-
flush_timeout,
503+
flush_timeout=determine_flush_timeout(),
492504
event_context_extractor=kwargs.get(
493505
"event_context_extractor", _default_event_context_extractor
494506
),
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from opentelemetry.instrumentation.aws_lambda import determine_flush_timeout
16+
from opentelemetry.instrumentation.aws_lambda import (
17+
OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT,
18+
)
19+
from unittest.mock import patch
20+
21+
22+
class TestDetermineFlushTimeout:
23+
default = 30000
24+
timeout_var = OTEL_INSTRUMENTATION_AWS_LAMBDA_FLUSH_TIMEOUT
25+
26+
def test_flush_timeout_default(self):
27+
assert determine_flush_timeout() == self.default
28+
29+
@patch.dict("os.environ", {timeout_var: "1000"})
30+
def test_flush_timeout_env_var(self):
31+
assert determine_flush_timeout() == 1000
32+
33+
@patch.dict("os.environ", {timeout_var: "abc"})
34+
def test_flush_timeout_env_var_invalid(self):
35+
assert determine_flush_timeout() == self.default

0 commit comments

Comments
 (0)