Skip to content

Commit 8ed8ee6

Browse files
committed
add client server example
1 parent 3b3e25e commit 8ed8ee6

4 files changed

Lines changed: 121 additions & 5 deletions

File tree

docs/examples/datadog_exporter/README.rst

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ Installation
1212
pip install opentelemetry-sdk
1313
pip install opentelemetry-ext-datadog
1414
15-
Run the Example
16-
---------------
15+
Basic Example
16+
-------------
1717

1818
* Start Datadog Agent
1919

@@ -27,10 +27,33 @@ Run the Example
2727
-e DD_APM_ENABLED=true \
2828
datadog/agent:latest
2929
30-
* Run the example
30+
* Run example
3131

3232
.. code-block:: sh
3333
3434
python datadog_exporter.py
3535
36-
The traces will be available at http://datadoghq.com/.
36+
Auto-Instrumention Example
37+
--------------------------
38+
39+
* Start Datadog Agent (same as above)
40+
41+
* Install libraries
42+
43+
.. code-block:: sh
44+
45+
pip install opentelemetry-api
46+
pip install opentelemetry-sdk
47+
pip install opentelemetry-ext-datadog
48+
pip install opentelemetry-auto-instrumentation
49+
pip install opentelemetry-ext-flask
50+
pip install flask
51+
pip install requests
52+
53+
* Run auto-instrumentation example
54+
55+
.. code-block:: sh
56+
# start server in a terminal
57+
opentelemetry-auto-instrumentation server.py
58+
# run client in another terminal
59+
python client.py testing
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from sys import argv
16+
17+
from requests import get
18+
19+
from opentelemetry import propagators, trace
20+
from opentelemetry.ext.datadog import DatadogSpanExporter
21+
from opentelemetry.sdk.trace import TracerProvider
22+
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
23+
24+
trace.set_tracer_provider(TracerProvider())
25+
26+
trace.get_tracer_provider().add_span_processor(
27+
BatchExportSpanProcessor(
28+
DatadogSpanExporter(
29+
agent_url="http://localhost:8126", service="example-client"
30+
)
31+
)
32+
)
33+
34+
tracer = trace.get_tracer(__name__)
35+
36+
assert len(argv) == 2
37+
38+
with tracer.start_as_current_span("client"):
39+
40+
with tracer.start_as_current_span("client-server"):
41+
headers = {}
42+
propagators.inject(dict.__setitem__, headers)
43+
requested = get(
44+
"http://localhost:8082/server_request",
45+
params={"param": argv[1]},
46+
headers=headers,
47+
)
48+
49+
assert requested.status_code == 200
50+
print(requested.text)

docs/examples/datadog_exporter/datadog_exporter.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16-
import os
1716

1817
from opentelemetry import trace
1918
from opentelemetry.ext.datadog import DatadogSpanExporter
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from flask import Flask, request
16+
17+
from opentelemetry import trace
18+
from opentelemetry.ext.datadog import DatadogSpanExporter
19+
from opentelemetry.sdk.trace import TracerProvider
20+
from opentelemetry.sdk.trace.export import BatchExportSpanProcessor
21+
22+
app = Flask(__name__)
23+
24+
trace.set_tracer_provider(TracerProvider())
25+
26+
trace.get_tracer_provider().add_span_processor(
27+
BatchExportSpanProcessor(
28+
DatadogSpanExporter(
29+
agent_url="http://localhost:8126", service="example-server"
30+
)
31+
)
32+
)
33+
34+
tracer = trace.get_tracer(__name__)
35+
36+
37+
@app.route("/server_request")
38+
def server_request():
39+
with tracer.start_as_current_span("server-inner"):
40+
return "served: {}".format(request.args.get("param"))
41+
42+
43+
if __name__ == "__main__":
44+
app.run(port=8082)

0 commit comments

Comments
 (0)