Skip to content

Commit af399c7

Browse files
authored
[Python] Custom Export Pipeline Update (#489)
1 parent 47d0f95 commit af399c7

File tree

4 files changed

+118
-2
lines changed

4 files changed

+118
-2
lines changed

sample-apps/python/django_frontend_service/ec2-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ pymysql==1.1.1
44
python-dotenv~=1.0.1
55
requests~=2.25.1
66
schedule~=1.2.1
7+
opentelemetry-sdk>=1.33.1,<2.0.0
78
opentelemetry-api>=1.33.1,<2.0.0

sample-apps/python/django_frontend_service/frontend_service_app/views.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,52 @@
1616

1717
logger = logging.getLogger(__name__)
1818

19+
# Custom export pipeline - only create if specific env vars exist
20+
pipeline_meter = None
21+
if os.environ.get('SERVICE_NAME') and os.environ.get('DEPLOYMENT_ENVIRONMENT_NAME'):
22+
from opentelemetry.sdk.resources import Resource
23+
from opentelemetry.sdk.metrics import MeterProvider
24+
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
25+
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter
26+
from opentelemetry.semconv.resource import ResourceAttributes
27+
28+
service_name = os.environ.get('SERVICE_NAME')
29+
deployment_env = os.environ.get('DEPLOYMENT_ENVIRONMENT_NAME')
30+
pipeline_resource = Resource.create({
31+
#ResourceAttributes.DEPLOYMENT_ENVIRONMENT_NAME maps to dimension 'deployment.name' so "deployment.environment.name" used
32+
#to assign value correctly.
33+
ResourceAttributes.SERVICE_NAME: service_name,
34+
"deployment.environment.name": deployment_env
35+
})
36+
37+
pipeline_metric_exporter = OTLPMetricExporter(endpoint="localhost:4317")
38+
pipeline_metric_reader = PeriodicExportingMetricReader(
39+
exporter=pipeline_metric_exporter,
40+
export_interval_millis=1000
41+
)
42+
43+
pipeline_meter_provider = MeterProvider(
44+
resource=pipeline_resource,
45+
metric_readers=[pipeline_metric_reader]
46+
)
47+
48+
pipeline_meter = pipeline_meter_provider.get_meter("myMeter")
49+
50+
1951
#python equivalent of Meter meter = GlobalOpenTelemetry.getMeter("myMeter"); for custom metrics.
2052
meter = metrics.get_meter("myMeter")
2153
agent_based_counter = meter.create_counter("agent_based_counter", unit="1", description="agent export counter")
2254
agent_based_histogram = meter.create_histogram("agent_based_histogram", description="agent export histogram")
2355
agent_based_gauge = meter.create_up_down_counter("agent_based_gauge", unit="1", description="agent export gauge")
2456

57+
# Create pipeline metrics only if pipeline exists
58+
custom_pipeline_counter = None
59+
custom_pipeline_histogram = None
60+
custom_pipeline_gauge = None
61+
if pipeline_meter:
62+
custom_pipeline_counter = pipeline_meter.create_counter("custom_pipeline_counter", unit="1", description="pipeline export counter")
63+
custom_pipeline_histogram = pipeline_meter.create_histogram("custom_pipeline_histogram", description="pipeline export histogram")
64+
custom_pipeline_gauge = pipeline_meter.create_up_down_counter("custom_pipeline_gauge", unit="1", description="pipeline export gauge")
2565

2666
should_send_local_root_client_call = False
2767
lock = threading.Lock()
@@ -63,6 +103,11 @@ def aws_sdk_call(request):
63103
agent_based_histogram.record(random.randint(100, 1000), {"Operation": "histogram"})
64104
agent_based_gauge.add(random.randint(-10, 10), {"Operation": "gauge"})
65105

106+
if pipeline_meter:
107+
custom_pipeline_counter.add(1, {"Operation": "pipeline_counter"})
108+
custom_pipeline_histogram.record(random.randint(100, 1000), {"Operation": "pipeline_histogram"})
109+
custom_pipeline_gauge.add(random.randint(-10, 10), {"Operation": "pipeline_gauge"})
110+
66111
bucket_name = "e2e-test-bucket-name"
67112

68113
# Add a unique test ID to bucketname to associate buckets to specific test runs

terraform/python/ec2/default/main.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ resource "null_resource" "main_service_setup" {
180180
export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=localhost:4317
181181
export OTEL_EXPORTER_OTLP_METRICS_INSECURE=true
182182
export OTEL_TRACES_SAMPLER=always_on
183-
export OTEL_RESOURCE_ATTRIBUTES="service.name=python-sample-application-${var.test_id},deployment.environment.name=ec2:default"
183+
export SERVICE_NAME='python-sample-application-${var.test_id}'
184+
export DEPLOYMENT_ENVIRONMENT_NAME='ec2:default'
185+
export OTEL_RESOURCE_ATTRIBUTES="service.name=$${SERVICE_NAME},deployment.environment.name=$${DEPLOYMENT_ENVIRONMENT_NAME}"
184186
export AWS_REGION='${var.aws_region}'
185187
python${var.language_version} manage.py migrate
186188
nohup opentelemetry-instrument python${var.language_version} manage.py runserver 0.0.0.0:8000 --noreload &

validator/src/main/resources/expected-data-template/python/ec2/default/aws-otel-custom-metrics.mustache

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,72 @@
156156
value: ANY_VALUE
157157
-
158158
name: cloud.platform
159-
value: aws_ec2
159+
value: aws_ec2
160+
161+
# Export pipeline metrics
162+
-
163+
metricName: custom_pipeline_counter
164+
namespace: {{metricNamespace}}
165+
dimensions:
166+
-
167+
name: deployment.environment.name
168+
value: ec2:default
169+
-
170+
name: service.name
171+
value: {{serviceName}}
172+
-
173+
name: Operation
174+
value: pipeline_counter
175+
-
176+
name: telemetry.sdk.name
177+
value: opentelemetry
178+
-
179+
name: telemetry.sdk.language
180+
value: python
181+
-
182+
name: telemetry.sdk.version
183+
value: ANY_VALUE
184+
-
185+
metricName: custom_pipeline_histogram
186+
namespace: {{metricNamespace}}
187+
dimensions:
188+
-
189+
name: deployment.environment.name
190+
value: ec2:default
191+
-
192+
name: service.name
193+
value: {{serviceName}}
194+
-
195+
name: Operation
196+
value: pipeline_histogram
197+
-
198+
name: telemetry.sdk.name
199+
value: opentelemetry
200+
-
201+
name: telemetry.sdk.language
202+
value: python
203+
-
204+
name: telemetry.sdk.version
205+
value: ANY_VALUE
206+
-
207+
metricName: custom_pipeline_gauge
208+
namespace: {{metricNamespace}}
209+
dimensions:
210+
-
211+
name: deployment.environment.name
212+
value: ec2:default
213+
-
214+
name: service.name
215+
value: {{serviceName}}
216+
-
217+
name: Operation
218+
value: pipeline_gauge
219+
-
220+
name: telemetry.sdk.name
221+
value: opentelemetry
222+
-
223+
name: telemetry.sdk.language
224+
value: python
225+
-
226+
name: telemetry.sdk.version
227+
value: ANY_VALUE

0 commit comments

Comments
 (0)