|
1 | 1 | import socket |
2 | 2 | import threading |
3 | 3 | import time |
| 4 | +from flask import Flask, jsonify |
4 | 5 |
|
5 | 6 | from amazon.opentelemetry.exporters.otlp.udp import OTLPUdpSpanExporter |
6 | 7 | from opentelemetry import trace |
7 | 8 | from opentelemetry.sdk.trace import TracerProvider |
8 | 9 | from opentelemetry.sdk.trace.export import BatchSpanProcessor |
9 | 10 |
|
| 11 | +app = Flask(__name__) |
10 | 12 |
|
11 | 13 | # Set up tracer provider |
12 | 14 | tracer_provider = TracerProvider() |
|
17 | 19 | span_processor = BatchSpanProcessor(exporter) |
18 | 20 | tracer_provider.add_span_processor(span_processor) |
19 | 21 |
|
20 | | -# Create a span for testing with various attributes |
| 22 | +# Get tracer |
21 | 23 | tracer = trace.get_tracer(__name__) |
22 | | -with tracer.start_as_current_span("test_parent_span") as parent: |
23 | | - parent.set_attribute("service.name", "validation-app") |
24 | | - parent.set_attribute("test.attribute", "test_value") |
25 | | - parent.add_event("test-event", {"event.data": "some data"}) |
26 | 24 |
|
27 | | - # Add a child span |
28 | | - with tracer.start_as_current_span("test_child_span") as child: |
29 | | - child.set_attribute("child.attribute", "child_value") |
30 | | - print("Created spans with attributes and events") |
31 | | - |
32 | | -# Force flush to ensure spans are exported immediately |
33 | | -success = tracer_provider.force_flush() |
34 | | -print(f"Force flush {'succeeded' if success else 'failed'}") |
35 | | - |
36 | | -# Give some time for the UDP packet to be processed |
37 | | -time.sleep(2) |
38 | | - |
39 | | -# Shutdown |
40 | | -tracer_provider.shutdown() |
| 25 | +@app.route('/trace', methods=['GET']) |
| 26 | +def create_trace(): |
| 27 | + # Create a span for testing with various attributes |
| 28 | + tracer = trace.get_tracer(__name__) |
| 29 | + with tracer.start_as_current_span("test_parent_span") as parent: |
| 30 | + parent.set_attribute("service.name", "validation-app") |
| 31 | + parent.set_attribute("test.attribute", "test_value") |
| 32 | + parent.add_event("test-event", {"event.data": "some data"}) |
| 33 | + |
| 34 | + # Get the trace ID |
| 35 | + trace_id = format(parent.get_span_context().trace_id, '032x') |
| 36 | + |
| 37 | + # Add a child span |
| 38 | + with tracer.start_as_current_span("test_child_span") as child: |
| 39 | + child.set_attribute("child.attribute", "child_value") |
| 40 | + print("Created spans with attributes and events") |
| 41 | + |
| 42 | + # Force flush to ensure spans are exported immediately |
| 43 | + success = tracer_provider.force_flush() |
| 44 | + print(f"Force flush {'succeeded' if success else 'failed'}") |
| 45 | + |
| 46 | + return jsonify({ |
| 47 | + "trace_id": trace_id |
| 48 | + }) |
| 49 | + |
| 50 | +if __name__ == '__main__': |
| 51 | + app.run(port=8080) |
0 commit comments