Skip to main content

Implements Runtime Insights Support for Python Project

Project description

opentelem-python

Note: This package is part of the Runtime Insights Early Access Program.

Purpose

This package allows Python projects to leverage Codecov's Runtime Insights feature.

More information about Runtime Insights can be found in Codecov's public documentation.

Requirements and Pre-requisites

  1. A repository that is active on Codecov
  2. A profiling token for that repository, obtainable from Codecov.
  3. Python version >=3.6

Installation

First, install the package:

pip install codecovopentelem

Second, include the following snippet in your application. Where this is snippet is placed varies depending on the application, see Integration Examples below.

The snippet:

from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    ConsoleSpanExporter,
    BatchSpanProcessor,
    SimpleSpanProcessor,
)
from opentelemetry import trace

from codecovopentelem import (
    CoverageSpanFilter,
    get_codecov_opentelemetry_instances,
)
from utils.time import format_time

provider = TracerProvider()
trace.set_tracer_provider(provider)

"""
CONFIGURATION
"""
current_version = "your-application-version"
current_env = "your-application-envrionment"
code=f"{current_version}:{current_env}"
export_rate = 0.01
repository_token="your-repository-profiling-token"
untracked_export_rate = 0

generator, exporter = get_codecov_opentelemetry_instances(
    repository_token=repository_token,
    version_identifier=current_version,
    sample_rate=export_rate,
    filters={
        CoverageSpanFilter.span_kind_filter: [
            trace.SpanKind.SERVER,
            trace.SpanKind.CONSUMER,
        ],
    },
    code=code,
    untracked_export_rate=untracked_export_rate,
    environment=current_env,
)
provider.add_span_processor(generator)
provider.add_span_processor(BatchSpanProcessor(exporter))

Integration Examples

The specifics of how this library is integrated into your project depends on the project itself. This section contains a few common, framework specific, integration approaches along with the general integration approach at the end.

Note that these examples demonstrate possible ways to incorporate this package into your project. As always, your specific needs may vary.

Flask

In a Flask application, you could place the code snippet in your application's app.py file before the call to initialize your Flask app, like so:

from opentelemetry.instrumentation.flask import FlaskInstrumentor

# Snippet Code ...
# Other Startup Code ...

app = Flask(
    __name__,
    static_url_path='',
    static_folder='',
    template_folder='templates',
)

FlaskInstrumentor().instrument_app(app)

# app.route(...)
# ...

app.run(host='0.0.0.0', port=8080)

Django

In Django, you can place this snippet in your application's <application_name>/wsgi.py file:

import os

from django.core.wsgi import get_wsgi_application

from utils.config import get_settings_module

os.environ.setdefault("DJANGO_SETTINGS_MODULE", get_settings_module())

#... Other Startup Code

from opentelemetry.instrumentation.django import DjangoInstrumentor
try:
    # Snippet Code
except UnableToStartProcessorException:
    # Handle the Exception...

DjangoInstrumentor().instrument()


application = get_wsgi_application()

Note that this example also demonstrates how to integrate using a try/except.

General Integration

If you are not using Django or Flask integration is still possible using the above code snippet. How to do this may vary greatly depending on your use case. In general, though, the code snippet should be placed wherever you would put your application's OpenTelemetry startup code. In lieu of that, this code should be incorporated in such a way that it is part of your application's startup process.

Configuration

  • current_version -- (Required) The current version of the application. This can be semver, a commit SHA, or whatever is meaningful to you, but it should uniquely identify the particular version of the code.
  • current_env -- (Required) The environment in which the application is currently running. Typically "production", but can be other values as well (e.g., "local" / "dev" for testing during setup of the package, "test" for instrumenting in your test environment, etc.)
  • code -- A unique identifier for the current deployment across all environments where it may be deployed. Conventionally, this is a combination of version number and environment name, but can be anything as long as it is unique in each environment for the version being deployed.
  • export_rate -- (Required. Min: 0, Max: 1) The percentage of your application's calls that are instrumented using this package. Using this package does incur some performance overhead, and instrumenting 100% of calls is not required. Therefore, for most applications, it is recommended to use 0.01 to 0.05 as the default value. However, low traffic applications may want to use a larger number (such as 0.1 or more).
  • repository_token -- (Required) The identifying token for this repository. Currently only obtainable by being selected for Codecov's Runtime Insights Early Access Program. It should be treated as a sensitive credential (e.g., not committed to source control, etc.)
  • untracked_export_rate -- Currently unused, should remain at 0.

If desired, the filters parameter can also be changed to provide different filtering on any valid OpenTelemetry SpanKind as defined by the specification.

Codecov.yml Changes

You will need to update your codecov.yml as follows:

comment:
  layout: 'reach,diff,flags,tree,betaprofiling'
  show_critical_paths: true

You can read more about the codecov.yml in Codecov's public documentation. If you do not have a codecov.yml in your project, you can create the file in the root of your project and add the above configuration.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

codecovopentelem-0.0.6.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

codecovopentelem-0.0.6-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file codecovopentelem-0.0.6.tar.gz.

File metadata

  • Download URL: codecovopentelem-0.0.6.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for codecovopentelem-0.0.6.tar.gz
Algorithm Hash digest
SHA256 7ef36139582661671f9030367b05799cfb06a1d553ee1dd7deedc6cc62bde50f
MD5 0070a30cd35dbd72b609fb9600845c1e
BLAKE2b-256 7aef92ebdf192ba385bd6b8c1f9eb602d579c44986760af9459cd340ca61650e

See more details on using hashes here.

File details

Details for the file codecovopentelem-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: codecovopentelem-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for codecovopentelem-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 16d98d126d82ecad1a92bb2958d9b81a51ac93c19ac6ad56d67c933df8d797df
MD5 9ac86cfc5bf558580c1731344d491293
BLAKE2b-256 506ce3faf1379848653329576efea7a781bd2db9588638b2941d5c8246a018f2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page