Last active
August 23, 2018 23:17
-
-
Save burnsie7/fcfa07958bad508b14fa186af627a53e to your computer and use it in GitHub Desktop.
tracing_api_example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import time | |
import requests | |
import json | |
''' | |
Set the following options in /etc/datadog-agent/datadog.yaml: | |
# Trace Agent Specific Settings | |
apm_config: | |
enabled: true | |
env: testing | |
receiver_port: 8126 | |
If 'env' is not set your traces will be found under 'env:none' in the Datadog APM UI. | |
Once services are created and traces are submitted using the script below, traces shoud appear almost immediately at: | |
https://app.datadoghq.com/apm/traces | |
After a few minutes services and the underlying resources and traces will appear at: | |
https://app.datadoghq.com/apm/services | |
You will need to select the correct environment from the dropdown, in this case 'testing'. | |
You can then explore your services, resources, and traces using the techniques outlined in our documentation: | |
https://docs.datadoghq.com/tracing/visualization/ | |
''' | |
headers = {"Content-Type": "application/json"} | |
# Create service(s). This only needs to be done once. | |
# Creating two here to give an example of multi-service traces. | |
def create_services(): | |
service_data = {"web_service": {"app": "my-app","app_type": "web"}} | |
requests.put('http://localhost:8126/v0.3/services', headers=headers, data = json.dumps(service_data)) | |
service_data = {"backend_service": {"app": "my-app","app_type": "db"}} | |
requests.put('http://localhost:8126/v0.3/services', headers=headers, data = json.dumps(service_data)) | |
def create_trace(): | |
# Set a unique trace_id which will be used by all spans | |
TRACE_ID = random.randint(1,1000000) | |
#Start Parent Span | |
spans = [] | |
PARENT_SPAN_ID = random.randint(1,1000000) | |
PARENT_START = int(time.time() * 1000000000) | |
time.sleep(0.3) | |
# Create Child Spans | |
for i in range(4): | |
SPAN_ID = random.randint(1,1000000) | |
START = int(time.time() * 1000000000) | |
time.sleep(0.3) | |
DURATION= int(time.time() * 1000000000) - START | |
span_name = "child_span_" + str(i) | |
span_data = { | |
"trace_id": TRACE_ID, | |
"span_id": SPAN_ID, | |
"name": span_name, | |
"resource": "db_query", | |
"service": "backend_service", | |
"type": "db", | |
"start": START, | |
"duration": DURATION, | |
"parent_id": PARENT_SPAN_ID | |
} | |
spans.append(span_data) | |
# Finish Parent Span | |
time.sleep(0.3) | |
PARENT_DURATION = int(time.time() * 1000000000) - PARENT_START | |
span_name = "parent_span" | |
span_data = { | |
"trace_id": TRACE_ID, | |
"span_id": PARENT_SPAN_ID, | |
"name": span_name, | |
"resource": "/parent", | |
"service": "web_service", | |
"type": "web", | |
"start": PARENT_START, | |
"duration": PARENT_DURATION | |
} | |
spans.append(span_data) | |
data=[spans] | |
requests.put("http://localhost:8126/v0.3/traces", data=json.dumps(data), headers=headers) | |
create_services() | |
for i in range(10): | |
create_trace() | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment