Skip to content

Commit

Permalink
Add support for automatic instrumentation and injection
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Dec 10, 2019
1 parent 105fe2f commit 4314f46
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 1 deletion.
27 changes: 27 additions & 0 deletions ext/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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.

"OpenTelemetry extension specification"

from abc import ABC, abstractmethod


class OpenTelemetryExtension(ABC):
"OpenTelemetry extension base class"

@abstractmethod
def patch(self, *args, **kwargs):
"Monkey patch a OpenTelemetry extension"

pass
11 changes: 10 additions & 1 deletion ext/opentelemetry-ext-flask/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@
with open(VERSION_FILENAME) as f:
exec(f.read(), PACKAGE_INFO)

setuptools.setup(version=PACKAGE_INFO["__version__"])
setuptools.setup(
version=PACKAGE_INFO["__version__"],
entry_points={
"opentelemetry_extension": [
"opentelemetry_extension_flask = "
"opentelemetry-ext-flask.flask:"
"OpenTelemetryFlaskExtension"
]
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from flask import request as flask_request

import opentelemetry.ext.wsgi as otel_wsgi
from opentelemetry.ext import OpenTelemetryExtension
from opentelemetry import propagators, trace
from opentelemetry.util import time_ns

Expand All @@ -16,6 +17,12 @@
_ENVIRON_ACTIVATION_KEY = object()


class OpenTelemetryExtensionFlask(OpenTelemetryExtension):

def patch(self):

pass

def instrument_app(flask):
"""Makes the passed-in Flask object traced by OpenTelemetry.
Expand Down
10 changes: 10 additions & 0 deletions opentelemetry-sdk/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@
"/tree/master/opentelemetry-sdk"
),
zip_safe=False,
entry_points={
"console_scripts": [
"opentelemetry_python_autoagent = opentelemetry."
"scripts.opentelemetry_python_autoagent:run"
],
"opentelemetry_extension": [
"opentelemetry_extension = opentelemetry.ext:"
"OpenTelemetryExtension"
]
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pkg_resources import iter_entry_points
from logging import getLogger

_LOGGER = getLogger(__file__)


for entry_point in iter_entry_points(
group='opentelemetry_extension'
):

try:

entry_point.load()().patch()

except Exception as error:

_LOG.warning(
(
"Unable to patch extension {extension}, this "
"error happened while attempting to patch: "
"{error}. Execution may continue but no "
"automatic instrumentation will be performed on "
"{extension}."
).format(extension=entry_point.name, error=error)
)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3

from sys import exit, argv
from os import execl
import os
from os.path import dirname, join
from distutils.spawn import find_executable


def run():

os.environ['PYTHONPATH'] = join(dirname(__file__), 'initialize')

python3 = find_executable('python3')

execl(python3, python3, argv[1])

exit(0)

0 comments on commit 4314f46

Please sign in to comment.