Skip to content

Commit

Permalink
Add exporter to Datadog (open-telemetry#572)
Browse files Browse the repository at this point in the history
Add an exporter to Datadog. This implementation makes use of ddtrace to handle the creation of Datadog traces and writing them to the Datadog agent.

Co-Authored-By: Mauricio Vásquez <mauricio@kinvolk.io>
  • Loading branch information
2 people authored and owais committed May 22, 2020
1 parent 4b43c58 commit dfa1b9d
Show file tree
Hide file tree
Showing 18 changed files with 1,242 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ sphinx-rtd-theme~=0.4
sphinx-autodoc-typehints~=1.10.2

# Required by ext packages
ddtrace>=0.34.0
aiohttp ~= 3.0
Deprecated>=1.2.6
django>=2.2
Expand Down
81 changes: 81 additions & 0 deletions docs/examples/datadog_exporter/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
Datadog Exporter Example
========================

These examples show how to use OpenTelemetry to send tracing data to Datadog.


Basic Example
-------------

* Installation

.. code-block:: sh
pip install opentelemetry-api
pip install opentelemetry-sdk
pip install opentelemetry-ext-datadog
* Start Datadog Agent

.. code-block:: sh
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
-v /proc/:/host/proc/:ro \
-v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro \
-p 127.0.0.1:8126:8126/tcp \
-e DD_API_KEY="<DATADOG_API_KEY>" \
-e DD_APM_ENABLED=true \
datadog/agent:latest
* Run example

.. code-block:: sh
python datadog_exporter.py
Auto-Instrumention Example
--------------------------

* Installation

.. code-block:: sh
pip install opentelemetry-api
pip install opentelemetry-sdk
pip install opentelemetry-ext-datadog
pip install opentelemetry-auto-instrumentation
pip install opentelemetry-ext-flask
pip install flask
pip install requests
* Start Datadog Agent

.. code-block:: sh
docker run --rm \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
-v /proc/:/host/proc/:ro \
-v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro \
-p 127.0.0.1:8126:8126/tcp \
-e DD_API_KEY="<DATADOG_API_KEY>" \
-e DD_APM_ENABLED=true \
datadog/agent:latest
* Start server

.. code-block:: sh
opentelemetry-auto-instrumentation python server.py
* Run client

.. code-block:: sh
opentelemetry-auto-instrumentation python client.py testing
* Run client with parameter to raise error

.. code-block:: sh
opentelemetry-auto-instrumentation python client.py error
52 changes: 52 additions & 0 deletions docs/examples/datadog_exporter/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from sys import argv

from requests import get

from opentelemetry import propagators, trace
from opentelemetry.ext.datadog import (
DatadogExportSpanProcessor,
DatadogSpanExporter,
)
from opentelemetry.sdk.trace import TracerProvider

trace.set_tracer_provider(TracerProvider())

trace.get_tracer_provider().add_span_processor(
DatadogExportSpanProcessor(
DatadogSpanExporter(
agent_url="http://localhost:8126", service="example-client"
)
)
)

tracer = trace.get_tracer(__name__)

assert len(argv) == 2

with tracer.start_as_current_span("client"):

with tracer.start_as_current_span("client-server"):
headers = {}
propagators.inject(dict.__setitem__, headers)
requested = get(
"http://localhost:8082/server_request",
params={"param": argv[1]},
headers=headers,
)

assert requested.status_code == 200
print(requested.text)
37 changes: 37 additions & 0 deletions docs/examples/datadog_exporter/datadog_exporter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
#
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from opentelemetry import trace
from opentelemetry.ext.datadog import (
DatadogExportSpanProcessor,
DatadogSpanExporter,
)
from opentelemetry.sdk.trace import TracerProvider

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)

exporter = DatadogSpanExporter(
agent_url="http://localhost:8126", service="example"
)

span_processor = DatadogExportSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)

with tracer.start_as_current_span("foo"):
with tracer.start_as_current_span("bar"):
with tracer.start_as_current_span("baz"):
print("Hello world from OpenTelemetry Python!")
49 changes: 49 additions & 0 deletions docs/examples/datadog_exporter/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from flask import Flask, request

from opentelemetry import trace
from opentelemetry.ext.datadog import (
DatadogExportSpanProcessor,
DatadogSpanExporter,
)
from opentelemetry.sdk.trace import TracerProvider

app = Flask(__name__)

trace.set_tracer_provider(TracerProvider())

trace.get_tracer_provider().add_span_processor(
DatadogExportSpanProcessor(
DatadogSpanExporter(
agent_url="http://localhost:8126", service="example-server"
)
)
)

tracer = trace.get_tracer(__name__)


@app.route("/server_request")
def server_request():
param = request.args.get("param")
with tracer.start_as_current_span("server-inner"):
if param == "error":
raise ValueError("forced server error")
return "served: {}".format(param)


if __name__ == "__main__":
app.run(port=8082)
7 changes: 7 additions & 0 deletions docs/ext/datadog/datadog.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
OpenTelemetry Datadog Exporter
==============================

.. automodule:: opentelemetry.ext.datadog
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions ext/opentelemetry-ext-datadog/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

## Unreleased

- Add exporter to Datadog
([#572](https://github.com/open-telemetry/opentelemetry-python/pull/572))

29 changes: 29 additions & 0 deletions ext/opentelemetry-ext-datadog/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
OpenTelemetry Datadog Exporter
==============================

|pypi|

.. |pypi| image:: https://badge.fury.io/py/opentelemetry-ext-datadog.svg
:target: https://pypi.org/project/opentelemetry-ext-datadog/

This library allows to export tracing data to `Datadog
<https://www.datadoghq.com/>`_. OpenTelemetry span event and links are not
supported.

Installation
------------

::

pip install opentelemetry-ext-datadog


.. _Datadog: https://www.datadoghq.com/
.. _OpenTelemetry: https://github.com/open-telemetry/opentelemetry-python/


References
----------

* `Datadog <https://www.datadoghq.com/>`_
* `OpenTelemetry Project <https://opentelemetry.io/>`_
47 changes: 47 additions & 0 deletions ext/opentelemetry-ext-datadog/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
[metadata]
name = opentelemetry-ext-datadog
description = Datadog Span Exporter for OpenTelemetry
long_description = file: README.rst
long_description_content_type = text/x-rst
author = OpenTelemetry Authors
author_email = cncf-opentelemetry-contributors@lists.cncf.io
url = https://github.com/open-telemetry/opentelemetry-python/ext/opentelemetry-ext-datadog
platforms = any
license = Apache-2.0
classifiers =
Development Status :: 4 - Beta
Intended Audience :: Developers
License :: OSI Approved :: Apache Software License
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3.5
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8

[options]
python_requires = >=3.5
package_dir=
=src
packages=find_namespace:
install_requires =
ddtrace>=0.34.0
opentelemetry-api==0.7.dev0
opentelemetry-sdk==0.7.dev0

[options.packages.find]
where = src
27 changes: 27 additions & 0 deletions ext/opentelemetry-ext-datadog/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import setuptools

BASE_DIR = os.path.dirname(__file__)
VERSION_FILENAME = os.path.join(
BASE_DIR, "src", "opentelemetry", "ext", "datadog", "version.py"
)
PACKAGE_INFO = {}
with open(VERSION_FILENAME) as f:
exec(f.read(), PACKAGE_INFO)

setuptools.setup(version=PACKAGE_INFO["__version__"])
Loading

0 comments on commit dfa1b9d

Please sign in to comment.