Skip to content

Commit 094b2be

Browse files
committed
Added delay after force_flush due to open-telemetry/opentelemetry-specification#1287
1 parent 85b97a7 commit 094b2be

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

python/sample-apps/function/lambda_function.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
from opentelemetry.exporter.otlp.proto.grpc._metric_exporter import (
1111
OTLPMetricExporter,
1212
)
13+
import opentelemetry.exporter.otlp.proto.grpc.version
14+
15+
print( "opentelemetry.exporter.otlp.proto.grpc version", opentelemetry.exporter.otlp.proto.grpc.version.__version__)
16+
1317
from opentelemetry._metrics import (
1418
get_meter_provider,
1519
set_meter_provider,
@@ -22,9 +26,8 @@
2226
provider = MeterProvider(metric_readers=[reader])
2327
set_meter_provider(provider)
2428

25-
2629
meter = get_meter_provider().get_meter("otel_stack_function", "0.1.2")
27-
print(os.environ)
30+
2831

2932
async def fetch(session, url):
3033
async with session.get(url) as response:
@@ -45,14 +48,15 @@ def lambda_handler(event, context):
4548

4649
counter = meter.create_counter(name="first_counter", description="TODO", unit="1",)
4750

51+
i = 0
4852
for bucket in s3.buckets.all():
49-
counter.add(1, attributes={"hello": bucket.name})
50-
print("CounterAdd")
53+
i = i + 1
54+
# print(bucket.name)
55+
56+
counter.add(i, attributes={"invocation": context.aws_request_id})
5157

52-
print(bucket.name)
53-
54-
print("forcing flush")
55-
provider.force_flush()
56-
print("forced flush")
57-
time.sleep(300)
58+
provider.force_flush() # needed to be sure that metrics are sent to OTel before shutting down the worker
59+
time.sleep(1) # This is needed because at the moment there is no API to force the collector to push the message before the Worker is suspended
60+
61+
5862
return {"statusCode": 200, "body": json.dumps(os.environ.get("_X_AMZN_TRACE_ID"))}

python/sample-apps/template.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ Resources:
3131
Environment:
3232
Variables:
3333
AWS_LAMBDA_EXEC_WRAPPER: /opt/python/otel-instrument
34-
OTEL_METRICS_EXPORTER: otlp_proto_http
35-
PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION: python
34+
PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION: python # needed because https://github.com/open-telemetry/opentelemetry-python/issues/2717 / when new release of library is released this should be solved and can be removed
3635
Tracing: Active
3736
Layers:
3837
- !Ref OTelLayer

0 commit comments

Comments
 (0)