Package: codecovopentelem
opentelem-pythonNote: This package is part of theRuntime Insights Early Access Program.PurposeThis package allows Python projects to leverage Codecov'sRuntime Insightsfeature.More information about Runtime Insights can be foundin Codecov's public documentation.Requirements and Pre-requisitesA repository that is active onCodecovA profiling token for that repository, obtainable from Codecov.Python version >=3.6InstallationFirst, install the package:pip install codecovopentelemSecond, include the following snippet in your application. Where this is snippet is placed varies depending on the application, seeIntegration Examplesbelow.The snippet:fromopentelemetry.sdk.traceimportTracerProviderfromopentelemetry.sdk.trace.exportimport(ConsoleSpanExporter,BatchSpanProcessor,SimpleSpanProcessor,)fromopentelemetryimporttracefromcodecovopentelemimport(CoverageSpanFilter,get_codecov_opentelemetry_instances,)fromutils.timeimportformat_timeprovider=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.01repository_token="your-repository-profiling-token"untracked_export_rate=0generator,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 ExamplesThe 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 demonstratepossibleways to incorporate this package into your project. As always, your specific needs may vary.FlaskIn a Flask application, you could place the code snippet in your application'sapp.pyfilebeforethe call to initialize your Flask app, like so:fromopentelemetry.instrumentation.flaskimportFlaskInstrumentor# 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)DjangoIn Django, you can place this snippet in your application's<application_name>/wsgi.pyfile:importosfromdjango.core.wsgiimportget_wsgi_applicationfromutils.configimportget_settings_moduleos.environ.setdefault("DJANGO_SETTINGS_MODULE",get_settings_module())#... Other Startup Codefromopentelemetry.instrumentation.djangoimportDjangoInstrumentortry:# Snippet CodeexceptUnableToStartProcessorException:# Handle the Exception...DjangoInstrumentor().instrument()application=get_wsgi_application()Note that this example also demonstrates how to integrate using atry/except.General IntegrationIf 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.Configurationcurrent_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'sRuntime 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, thefiltersparameter can also be changed to provide different filtering on any valid OpenTelemetrySpanKindasdefined by the specification.Codecov.yml ChangesYou will need to update yourcodecov.ymlas follows:comment:
  layout: 'reach,diff,flags,tree,betaprofiling'
  show_critical_paths: trueYou can read more about the codecov.yml in Codecov's public documentation. If you do not have acodecov.ymlin your project, you can create the file in the root of your project and add the above configuration.