Skip to content

Automatic exporter/provider setup for opentelemetry-instrument command.#1036

Merged
codeboten merged 1 commit intoopen-telemetry:masterfrom
owais:otlp-env-config
Nov 20, 2020
Merged

Automatic exporter/provider setup for opentelemetry-instrument command.#1036
codeboten merged 1 commit intoopen-telemetry:masterfrom
owais:otlp-env-config

Conversation

@owais
Copy link
Contributor

@owais owais commented Aug 23, 2020

This PR adds support to the opentelemetry-instrument command to automatically configure a tracer provider and exporter. By default configures the OTLP exporter (like other Otel auto-instrumentations. e.g, Java: https://github.com/open-telemetry/opentelemetry-java-instrumentation#getting-started). It also allows using a different in-built or 3rd party via a CLI argument or env variable.

Details can be found on opentelemetry-instrumentation's README package.

Fixes #663

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

  • Ships with automated tests
  • Tested with a demo app

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@owais owais requested a review from a team August 23, 2020 23:16
@owais owais force-pushed the otlp-env-config branch 2 times, most recently from 1568630 to 05a1fed Compare August 29, 2020 15:05
@owais owais changed the title POC: actual auto-instrumentation Automatic exporter/provider setup for opentelemetry-instrument command. Aug 29, 2020
@owais owais force-pushed the otlp-env-config branch 2 times, most recently from 53a5233 to 0ec2526 Compare August 29, 2020 15:10
owais added a commit to owais/opentelemetry-python that referenced this pull request Aug 29, 2020
open-telemetry#1036

This commit adds support to the opentelemetry-instrument command to
automatically configure a tracer provider and exporter. By default,
it configures the OTLP exporter (like other Otel auto-instrumentations.
e.g, Java: https://github.com/open-telemetry/opentelemetry-java-instrumentation#getting-started).
It also allows using a different in-built or 3rd party via a CLI argument or env variable.

Details can be found on opentelemetry-instrumentation's README package.

Fixes open-telemetry#663
owais added a commit to owais/opentelemetry-python that referenced this pull request Aug 29, 2020
open-telemetry#1036

This commit adds support to the opentelemetry-instrument command to
automatically configure a tracer provider and exporter. By default,
it configures the OTLP exporter (like other Otel auto-instrumentations.
e.g, Java: https://github.com/open-telemetry/opentelemetry-java-instrumentation#getting-started).
It also allows using a different in-built or 3rd party via a CLI argument or env variable.

Details can be found on opentelemetry-instrumentation's README package.

Fixes open-telemetry#663
owais added a commit to owais/opentelemetry-python that referenced this pull request Aug 29, 2020
open-telemetry#1036

This commit adds support to the opentelemetry-instrument command to
automatically configure a tracer provider and exporter. By default,
it configures the OTLP exporter (like other Otel auto-instrumentations.
e.g, Java: https://github.com/open-telemetry/opentelemetry-java-instrumentation#getting-started).
It also allows using a different in-built or 3rd party via a CLI argument or env variable.

Details can be found on opentelemetry-instrumentation's README package.

Fixes open-telemetry#663
owais added a commit to owais/opentelemetry-python that referenced this pull request Aug 29, 2020
open-telemetry#1036

This commit adds support to the opentelemetry-instrument command to
automatically configure a tracer provider and exporter. By default,
it configures the OTLP exporter (like other Otel auto-instrumentations.
e.g, Java: https://github.com/open-telemetry/opentelemetry-java-instrumentation#getting-started).
It also allows using a different in-built or 3rd party via a CLI argument or env variable.

Details can be found on opentelemetry-instrumentation's README package.

Fixes open-telemetry#663
owais added a commit to owais/opentelemetry-python that referenced this pull request Aug 29, 2020
open-telemetry#1036

This commit adds support to the opentelemetry-instrument command to
automatically configure a tracer provider and exporter. By default,
it configures the OTLP exporter (like other Otel auto-instrumentations.
e.g, Java: https://github.com/open-telemetry/opentelemetry-java-instrumentation#getting-started).
It also allows using a different in-built or 3rd party via a CLI argument or env variable.

Details can be found on opentelemetry-instrumentation's README package.

Fixes open-telemetry#663
owais added a commit to owais/opentelemetry-python that referenced this pull request Aug 29, 2020
open-telemetry#1036

This commit adds support to the opentelemetry-instrument command to
automatically configure a tracer provider and exporter. By default,
it configures the OTLP exporter (like other Otel auto-instrumentations.
e.g, Java: https://github.com/open-telemetry/opentelemetry-java-instrumentation#getting-started).
It also allows using a different in-built or 3rd party via a CLI argument or env variable.

Details can be found on opentelemetry-instrumentation's README package.

Fixes open-telemetry#663
owais added a commit to owais/opentelemetry-python that referenced this pull request Aug 29, 2020
open-telemetry#1036

This commit adds support to the opentelemetry-instrument command to
automatically configure a tracer provider and exporter. By default,
it configures the OTLP exporter (like other Otel auto-instrumentations.
e.g, Java: https://github.com/open-telemetry/opentelemetry-java-instrumentation#getting-started).
It also allows using a different in-built or 3rd party via a CLI argument or env variable.

Details can be found on opentelemetry-instrumentation's README package.

Fixes open-telemetry#663
owais added a commit to owais/opentelemetry-python that referenced this pull request Aug 29, 2020
open-telemetry#1036

This commit adds support to the opentelemetry-instrument command to
automatically configure a tracer provider and exporter. By default,
it configures the OTLP exporter (like other Otel auto-instrumentations.
e.g, Java: https://github.com/open-telemetry/opentelemetry-java-instrumentation#getting-started).
It also allows using a different in-built or 3rd party via a CLI argument or env variable.

Details can be found on opentelemetry-instrumentation's README package.

Fixes open-telemetry#663
owais added a commit to owais/opentelemetry-python that referenced this pull request Aug 29, 2020
open-telemetry#1036

This commit adds support to the opentelemetry-instrument command to
automatically configure a tracer provider and exporter. By default,
it configures the OTLP exporter (like other Otel auto-instrumentations.
e.g, Java: https://github.com/open-telemetry/opentelemetry-java-instrumentation#getting-started).
It also allows using a different in-built or 3rd party via a CLI argument or env variable.

Details can be found on opentelemetry-instrumentation's README package.

Fixes open-telemetry#663
owais added a commit to owais/opentelemetry-python that referenced this pull request Aug 29, 2020
open-telemetry#1036

This commit adds support to the opentelemetry-instrument command to
automatically configure a tracer provider and exporter. By default,
it configures the OTLP exporter (like other Otel auto-instrumentations.
e.g, Java: https://github.com/open-telemetry/opentelemetry-java-instrumentation#getting-started).
It also allows using a different in-built or 3rd party via a CLI argument or env variable.

Details can be found on opentelemetry-instrumentation's README package.

Fixes open-telemetry#663
owais added a commit to owais/opentelemetry-python that referenced this pull request Aug 29, 2020
open-telemetry#1036

This commit adds support to the opentelemetry-instrument command to
automatically configure a tracer provider and exporter. By default,
it configures the OTLP exporter (like other Otel auto-instrumentations.
e.g, Java: https://github.com/open-telemetry/opentelemetry-java-instrumentation#getting-started).
It also allows using a different in-built or 3rd party via a CLI argument or env variable.

Details can be found on opentelemetry-instrumentation's README package.

Fixes open-telemetry#663
owais added a commit to owais/opentelemetry-python that referenced this pull request Aug 29, 2020
open-telemetry#1036

This commit adds support to the opentelemetry-instrument command to
automatically configure a tracer provider and exporter. By default,
it configures the OTLP exporter (like other Otel auto-instrumentations.
e.g, Java: https://github.com/open-telemetry/opentelemetry-java-instrumentation#getting-started).
It also allows using a different in-built or 3rd party via a CLI argument or env variable.

Details can be found on opentelemetry-instrumentation's README package.

Fixes open-telemetry#663
@owais owais force-pushed the otlp-env-config branch 5 times, most recently from dac8060 to e2a1a04 Compare October 2, 2020 14:45
@owais
Copy link
Contributor Author

owais commented Oct 2, 2020

Update:

This PR now only support two configuration options.

  1. --exporter or OTEL_EXPORTER

This options takes a comma separated list of exporter names and automatically configures these exporters. In addition to the well-known exporters (ones present in this repo), it also accepts qualified Python paths to valid exporters. It accepts metric exporters but does nothing with them right now. Support for auto configuring metrics pipeline will be added later.

  1. --service-name or SERVICE_NAME

This value is used to configure the given exporter and/or provider with a service name.

@open-telemetry/python-approvers

@owais
Copy link
Contributor Author

owais commented Oct 5, 2020

Further simplified this by removing new features added to the bootstrap command (ability to install exporters). I'll follow up with a separate PR for that.

}


def _import(import_path: str) -> any:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I recommend not relying on import paths but to use standard entry points? This is an example.


if (
hasattr(sys, "argv")
and sys.argv[0].split(os.path.sep)[-1] == "celery"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be moved to the celery instrumentation package?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do that and then pass argv to a function from the celery instrumentation package but I don't see much benefit in that. It is a special case in real life so probably we should live with the fact and allow it. Also, we initialize entire tracing pipeline here and instrument all packages, not just celery so not sure if celery instrumentation having this would be the right choice.

Also, this is orthogonal to celery instrumentation really. Users might not have celery instrumentation installed but might still want their celery process to be traced (manually or through other instrumentation packages) and that's what this helps with. Without this, batch span exporter does not work with celery workers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR has been open for a while, so in order to move forward I am ok with this as it is right now. Nevertheless, I think we should definitely keep the "core" instrumentation completely separated from the implementation details of any and all of the instrumentation packages. This probably means allowing all the instrumentation packages to be allowed to run a preregistered function or something similar that includes this kind of code.


_DEFAULT_EXPORTER = symbols.exporter_otlp

known_exporters = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand this correctly, this is mapping certain strings to an import path of classes that already exist in our repo, with the purpose of instantiating these classes when their matching keys are passed as arguments via CLI. Using entry points would save us from having to implement this by hand, as this is basically what the entry points mechanism does, it associates a certain string defined in the setup.cfg to a Python object, which can be the class. This approach has the advantage that it keeps the plugins (the code related to the specific library or exporter in this case) separated from the code of the "core" opentelemetry-instrumentation component.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. That might work. I like that it means installing any compatible package will automatically add support for the specific package to the instrument command. I'll see if it has everything we need.

@NathanielRN
Copy link
Contributor

Hello! I have a PR to move some files you have in this PR to the Contrib repo, please let me know if this gets merged before the PR in the Contrib repo. Please see https://github.com/open-telemetry/opentelemetry-python-contrib/pulls/83

@owais
Copy link
Contributor Author

owais commented Oct 22, 2020

@NathanielRN This PR is only touching the base instrumentation package. I believe this is staying in core and not moving to contrib or is the plan to move this as well?

@NathanielRN
Copy link
Contributor

@owais I see that it's also changing instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py which would be moving to the Contrib Repo in open-telemetry/opentelemetry-python-contrib#83 so just want to make sure we catch that change so that it makes it into the Contrib master.

@owais
Copy link
Contributor Author

owais commented Oct 22, 2020

It's just auto-reformat by black. I'll drop these changes when I refactor so nothing to worry about. @NathanielRN

@owais
Copy link
Contributor Author

owais commented Nov 10, 2020

@ocelotl updated to use entry points. Thanks for the tip.

@owais
Copy link
Contributor Author

owais commented Nov 10, 2020

In order to simplify this and use iter points to not hard code knowledge about exporters, I had to temporarily remove support for Datadog exporter. My understanding is that GA does not require this feature to support contrib or 3rd party exporters so we should be good to go. I'll follow up with a separate PR to allow special cases like the Datadog exporter to work with this feature as well.

Copy link
Contributor

@ocelotl ocelotl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting just a typo to be fixed 👍


if (
hasattr(sys, "argv")
and sys.argv[0].split(os.path.sep)[-1] == "celery"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR has been open for a while, so in order to move forward I am ok with this as it is right now. Nevertheless, I think we should definitely keep the "core" instrumentation completely separated from the implementation details of any and all of the instrumentation packages. This probably means allowing all the instrumentation packages to be allowed to run a preregistered function or something similar that includes this kind of code.

Copy link
Contributor

@ocelotl ocelotl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @owais!

@NathanielRN
Copy link
Contributor

Really excited for this change! I added a PR to this branch to add the ability to set a Custom IDs Generator, would be nice to get these configs all in at once! owais#1

Comment on lines +9 to +10
- Fixed boostrap command to correctly install opentelemetry-instrumentation-falcon instead of opentelemetry-instrumentation-flask. ([#1138](https://github.com/open-telemetry/opentelemetry-python/pull/1138))
- Added support for `OTEL_EXPORTER` to the `opentelemetry-instrument` command ([#1036](https://github.com/open-telemetry/opentelemetry-python/pull/1036))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably move the PR references to be on a new line to be consistent with the other Changelog messages.

Comment on lines +40 to +41
of the well-known exporter names (see below) or a fully
qualified Python import path to a span exporter implementation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean I can set exporter opentelemetry.exporter.jaeger:JaegerSpanExporter?

Sorry for my misunderstanding, but when I look at the code it looks like it will error out if it doesn't match ep.name (where ep is an entry point).

exporters = {
    ep.name: ep for ep in iter_entry_points("opentelemetry_exporter")
}

for exporter_name in exporter_names:
    entry_point = exporters.get(exporter_name, None)
    if not entry_point:
        raise RuntimeError(
            "Requested exporter not found: {0}".format(exporter_name)
        )

So doesn't it have to be otlp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. This PR supported what the documentation says but I ended up removing it in order to simplify the implementation. Looks like I missed this piece of documentation when I removed the feature.

logger.debug("Instrumented %s", entry_point.name)

except Exception: # pylint: disable=broad-except
logger.exception("Instrumenting of %s failed", entry_point.name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this re-raise the Exception?

Copy link
Contributor Author

@owais owais Nov 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this case should halt execution of the program and IIRC, any exceptions raised inside sitecustomize are "eaten up" by python by default and the process still continues to work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant because you only call auto_instrument in line 41 below which is wrapped in another try except block.

So if you re raise it we will get the "Failed to auto initialize opentelemetry" log which will be true in a sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

if (
hasattr(sys, "argv")
and sys.argv[0].split(os.path.sep)[-1] == "celery"
and "worker" in sys.argv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be the following? Not sure if it's more performant or less...

Suggested change
and "worker" in sys.argv
and "worker" in sys.argv[1:]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think it matter either way. [1:] probably clarifies the intent a tiny bit more so will make the change.

Comment on lines +51 to +55
from celery.signals import worker_process_init # pylint:disable=E0401

@worker_process_init.connect(weak=False)
def init_celery(*args, **kwargs):
initialize()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a sweet catch, thanks for knowing to do something like this!

Copy link
Contributor

@NathanielRN NathanielRN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really excited for this change 👍

Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty good, just a minor typo to fix and a question about the change in docs.

Released 2020-10-13

- Fixed boostrap command to correctly install opentelemetry-instrumentation-falcon instead of opentelemetry-instrumentation-flask
- Fixed boostrap command to correctly install opentelemetry-instrumentation-falcon instead of opentelemetry-instrumentation-flask. ([#1138](https://github.com/open-telemetry/opentelemetry-python/pull/1138))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Fixed boostrap command to correctly install opentelemetry-instrumentation-falcon instead of opentelemetry-instrumentation-flask. ([#1138](https://github.com/open-telemetry/opentelemetry-python/pull/1138))
- Fixed bootstrap command to correctly install opentelemetry-instrumentation-falcon instead of opentelemetry-instrumentation-flask. ([#1138](https://github.com/open-telemetry/opentelemetry-python/pull/1138))

::

opentelemetry-bootstrap --action=install|requirements
opentelemetry-instrument -e zipkin,otlp celery -A tasks worker --loglevel=info
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain the reason behind removing the info about opentelemetry-bootstrap?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, this info is probably important to keep here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unintentional. Probably caused by a bad rebase. Fixing.

This commit extends the instrument command so it automatically
configures tracing with a provider, span processor and exporter. Most of
the component used can be customized with env vars or CLI arguments.

Details can be found on opentelemetry-instrumentation's README package.

Fixes open-telemetry#663
Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:required-for-ga To be resolved before GA release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement a mechanism to configure exporters with autoinstrumentation command

5 participants