-
Notifications
You must be signed in to change notification settings - Fork 666
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing an example app instrumented with opentelemetry (#129)
Creating an example app that showcases how an application integrates with opentelemetry.
- Loading branch information
1 parent
17bef26
commit 21c1c4a
Showing
12 changed files
with
192 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
OpenTelemetry Example Application | ||
================================= | ||
|
||
This package is a complete example of an application instrumented with OpenTelemetry. | ||
|
||
The purpose is to provide a reference for consumers that demonstrates how to use the OpenTelemetry API and SDK in a variety of use cases and how to set it up. | ||
|
||
|
||
References | ||
---------- | ||
|
||
* `OpenTelemetry Project <https://opentelemetry.io/>`_ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright 2019, 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 setuptools | ||
|
||
setuptools.setup( | ||
name="opentelemetry-example-app", | ||
version="0.1.dev0", | ||
author="OpenTelemetry Authors", | ||
author_email="cncf-opentelemetry-contributors@lists.cncf.io", | ||
classifiers=[ | ||
"Development Status :: 3 - Alpha", | ||
"Intended Audience :: Developers", | ||
"License :: OSI Approved :: Apache Software License", | ||
"Programming Language :: Python", | ||
"Programming Language :: Python :: 3", | ||
"Programming Language :: Python :: 3.4", | ||
"Programming Language :: Python :: 3.5", | ||
"Programming Language :: Python :: 3.6", | ||
"Programming Language :: Python :: 3.7", | ||
], | ||
description="OpenTelemetry Python API", | ||
include_package_data=True, | ||
long_description=open("README.rst").read(), | ||
install_requires=[ | ||
"typing; python_version<'3.5'", | ||
"opentelemetry-api", | ||
"opentelemetry-sdk", | ||
"opentelemetry-ext-http-requests", | ||
"opentelemetry-ext-wsgi", | ||
"flask", | ||
"requests", | ||
], | ||
license="Apache-2.0", | ||
package_dir={"": "src"}, | ||
packages=setuptools.find_namespace_packages(where="src"), | ||
url=( | ||
"https://github.com/open-telemetry/opentelemetry-python" | ||
"/tree/master/opentelemetry-example-app" | ||
), | ||
zip_safe=False, | ||
) |
Empty file.
69 changes: 69 additions & 0 deletions
69
opentelemetry-example-app/src/opentelemetry_example_app/flask_example.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright 2019, 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. | ||
# | ||
""" | ||
This module serves as an example to integrate with flask, using | ||
the requests library to perform downstream requests | ||
""" | ||
import time | ||
|
||
import flask | ||
|
||
import opentelemetry.ext.http_requests | ||
from opentelemetry import trace | ||
from opentelemetry.ext.wsgi import OpenTelemetryMiddleware | ||
from opentelemetry.sdk.trace import Tracer | ||
|
||
|
||
def configure_opentelemetry(flask_app: flask.Flask): | ||
"""Configure a flask application to use OpenTelemetry. | ||
This activates the specific components: | ||
* sets tracer to the SDK's Tracer | ||
* enables requests integration on the Tracer | ||
* uses a WSGI middleware to enable configuration | ||
TODO: | ||
* processors? | ||
* exporters? | ||
* propagators? | ||
""" | ||
# Start by configuring all objects required to ensure | ||
# a complete end to end workflow. | ||
# the preferred implementation of these objects must be set, | ||
# as the opentelemetry-api defines the interface with a no-op | ||
# implementation. | ||
trace.set_preferred_tracer_implementation(lambda _: Tracer()) | ||
# Integrations are the glue that binds the OpenTelemetry API | ||
# and the frameworks and libraries that are used together, automatically | ||
# creating Spans and propagating context as appropriate. | ||
opentelemetry.ext.http_requests.enable(trace.tracer()) | ||
flask_app.wsgi_app = OpenTelemetryMiddleware(flask_app.wsgi_app) | ||
|
||
|
||
app = flask.Flask(__name__) | ||
|
||
|
||
@app.route("/") | ||
def hello(): | ||
# emit a trace that measures how long the | ||
# sleep takes | ||
with trace.tracer().start_span("sleep"): | ||
time.sleep(0.001) | ||
return "hello" | ||
|
||
|
||
configure_opentelemetry(app) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2019, 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. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import unittest | ||
|
||
import opentelemetry_example_app.flask_example as flask_example | ||
|
||
|
||
class TestFlaskExample(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.app = flask_example.app | ||
|
||
def test_full_path(self): | ||
with self.app.test_client() as client: | ||
response = client.get("/") | ||
assert response.data.decode() == "hello" |
This file contains 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
This file contains 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
This file contains 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