Skip to content

Commit

Permalink
Working version for Lambda - Prometheus integration - pabcol@
Browse files Browse the repository at this point in the history
  • Loading branch information
pcolazurdo committed Jun 8, 2022
1 parent 15412bd commit 5593c75
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
27 changes: 27 additions & 0 deletions python/sample-apps/function/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,29 @@
import aiohttp
import asyncio
import boto3
import os
import time

from opentelemetry import _metrics
from opentelemetry.exporter.otlp.proto.grpc._metric_exporter import (
OTLPMetricExporter,
)
from opentelemetry._metrics import (
get_meter_provider,
set_meter_provider,
)
from opentelemetry.sdk._metrics import MeterProvider
from opentelemetry.sdk._metrics.export import PeriodicExportingMetricReader

exporter = OTLPMetricExporter(insecure=True)
reader = PeriodicExportingMetricReader(exporter)
provider = MeterProvider(metric_readers=[reader])
set_meter_provider(provider)


meter = get_meter_provider().get_meter("otel_stack_function", "0.1.2")
print(os.environ)

async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
Expand All @@ -21,8 +42,14 @@ def lambda_handler(event, context):

loop = asyncio.get_event_loop()
loop.run_until_complete(callAioHttp())

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

for bucket in s3.buckets.all():
counter.add(1, attributes={"hello": bucket.name})
print("CounterAdd")

print(bucket.name)

time.sleep(300)
return {"statusCode": 200, "body": json.dumps(os.environ.get("_X_AMZN_TRACE_ID"))}
5 changes: 4 additions & 1 deletion python/sample-apps/function/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
aiohttp
aiohttp
opentelemetry-api
opentelemetry-sdk
opentelemetry-exporter-otlp-proto-grpc
Empty file modified python/sample-apps/run.sh
100644 → 100755
Empty file.
12 changes: 10 additions & 2 deletions python/sample-apps/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,27 @@ Resources:
Type: AWS::Serverless::Function
Properties:
Handler: lambda_function.lambda_handler
Runtime: python3.9
Runtime: python3.7
CodeUri: ./function
Description: Build OTel Python Lambda Sample from scratch
MemorySize: 512
Timeout: 15
Timeout: 310
Policies:
- AWSLambdaBasicExecutionRole
- AWSLambda_ReadOnlyAccess
- AWSXrayWriteOnlyAccess
- AmazonS3FullAccess
- Statement:
- Sid: AMPRemoteWrite
Effect: Allow
Action:
- aps:RemoteWrite
Resource: '*'
Environment:
Variables:
AWS_LAMBDA_EXEC_WRAPPER: /opt/python/otel-instrument
OTEL_METRICS_EXPORTER: otlp_proto_http
PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION: python
Tracing: Active
Layers:
- !Ref OTelLayer
Expand Down

0 comments on commit 5593c75

Please sign in to comment.