|
| 1 | +ifdef::env-github[] |
| 2 | +NOTE: For the best reading experience, |
| 3 | +please view this documentation at https://www.elastic.co/guide/en/apm/agent/java[elastic.co] |
| 4 | +endif::[] |
| 5 | + |
| 6 | +[[opentracing-bridge]] |
| 7 | +== Elastic APM OpenTracing bridge |
| 8 | + |
| 9 | +The Elastic APM OpenTracing bridge allows to create Elastic APM `Transactions` and `Spans`, |
| 10 | +using the OpenTracing API. |
| 11 | +In other words, |
| 12 | +it translates the calls to the OpenTracing API to Elastic APM and thus allows for reusing existing instrumentation. |
| 13 | + |
| 14 | +The first span of a service will be converted to an Elastic APM |
| 15 | +{apm-overview-ref-v}/transactions.html[`Transaction`], |
| 16 | +subsequent spans are mapped to Elastic APM |
| 17 | +{apm-overview-ref-v}/transaction-spans.html[`Span`]. |
| 18 | + |
| 19 | +[float] |
| 20 | +[[opentracing-getting-started]] |
| 21 | +=== Getting started |
| 22 | +The first step in getting started with the OpenTracing API bridge is to install the `opentracing` library: |
| 23 | + |
| 24 | +[source,bash] |
| 25 | +---- |
| 26 | +pip install elastic-apm[opentracing] |
| 27 | +---- |
| 28 | + |
| 29 | +Or if you already have installed `elastic-apm` |
| 30 | + |
| 31 | + |
| 32 | +[source,bash] |
| 33 | +---- |
| 34 | +pip install opentracing>=2.0.0 |
| 35 | +---- |
| 36 | + |
| 37 | + |
| 38 | +[float] |
| 39 | +[[opentracing-init-tracer]] |
| 40 | +=== Initialize tracer |
| 41 | + |
| 42 | +[source,python] |
| 43 | +---- |
| 44 | +from elasticapm.contrib.opentracing import Tracer |
| 45 | +
|
| 46 | +tracer = Tracer(); |
| 47 | +---- |
| 48 | + |
| 49 | +`Tracer` accepts the following optional arguments: |
| 50 | + |
| 51 | + * `client_instance`: an already instantiated Elastic APM client |
| 52 | + * `config`: a configuration dictionary, which will be used to instantiate a new Elastic APM client, |
| 53 | + e.g. `{"SERVER_URL": "https://example.org"}. See <<configuration, configuration>> for more information. |
| 54 | + * `scope_manager`: a scope manager instance. Defaults to `ThreadLocalScopeManager` (see |
| 55 | + |
| 56 | + |
| 57 | +[float] |
| 58 | +[[opentracing-elastic-apm-tags]] |
| 59 | +=== Elastic APM specific tags |
| 60 | + |
| 61 | +Elastic APM defines some tags which are not included in the OpenTracing API but are relevant in the context of Elastic APM. |
| 62 | + |
| 63 | +- `type` - sets the type of the transaction, |
| 64 | + for example `request`, `ext` or `db` |
| 65 | +- `user.id` - sets the user id, |
| 66 | + appears in the "User" tab in the transaction details in the Elastic APM UI |
| 67 | +- `user.email` - sets the user email, |
| 68 | + appears in the "User" tab in the transaction details in the Elastic APM UI |
| 69 | +- `user.username` - sets the user name, |
| 70 | + appears in the "User" tab in the transaction details in the Elastic APM UI |
| 71 | +- `result` - sets the result of the transaction. Overrides the default value of `success`. |
| 72 | + |
| 73 | +NOTE: these tags need to be set on the first span of an operation (e.g. an incoming request of your webapp). |
| 74 | + |
| 75 | +[float] |
| 76 | +[[opentracing-caveats]] |
| 77 | +=== Caveats |
| 78 | +Not all features of the OpenTracing API are supported. |
| 79 | + |
| 80 | +[float] |
| 81 | +[[opentracing-scope-managers]] |
| 82 | +==== Scope Managers |
| 83 | +Currently, only the `ThreadLocalScopeManager` is supported. |
| 84 | +Using other scope managers will lead to unpredictable and possibly app-breaking behaviour. |
| 85 | + |
| 86 | +[float] |
| 87 | +[[opentracing-instrumentation]] |
| 88 | +==== Instrumentation |
| 89 | + |
| 90 | +It is recommended to not use the built-in instrumentations of Elastic APM together with third-party OpenTracing instrumentations |
| 91 | +like https://pypi.org/project/opentracing_instrumentation/[opentracing_instrumentation] in the same service. |
| 92 | +If you would like to use such instrumentations, we recommend to disable the built-in instrumentation using the <<config-instrument,`instrument`>> config option. |
| 93 | + |
| 94 | +[float] |
| 95 | +[[opentracing-propagation]] |
| 96 | +==== Context propagation |
| 97 | +This bridge only supports the formats `Format.Builtin.TEXT_MAP` and `Format.Builtin.HTTP_HEADERS`. |
| 98 | +`Format.Builtin.BINARY` is currently not supported. |
| 99 | + |
| 100 | +[float] |
| 101 | +[[opentracing-references]] |
| 102 | +==== Span References |
| 103 | +Currently, this bridge only supports `child_of` references. |
| 104 | +Other references, |
| 105 | +like `follows_from` are not supported yet. |
| 106 | + |
| 107 | +[float] |
| 108 | +[[opentracing-baggage]] |
| 109 | +==== Baggage |
| 110 | +The `Span.set_baggage_item(key, value)` method is not supported. |
| 111 | +Baggage items are silently dropped. |
| 112 | + |
| 113 | +[float] |
| 114 | +[[opentracing-logs]] |
| 115 | +==== Logs |
| 116 | +Only exception logging is supported. |
| 117 | +Logging an Exception on the OpenTracing span will create an Elastic APM |
| 118 | +{apm-overview-ref-v}/errors.html[`Error`]. |
| 119 | +Example: |
| 120 | + |
| 121 | +[source,python] |
| 122 | +---- |
| 123 | +with tracer.start_active_span("transaction") as tx_scope: |
| 124 | + try: |
| 125 | + raise ValueError("oops") |
| 126 | + except ValueError: |
| 127 | + exc_type, exc_val, exc_tb = sys.exc_info()[:3] |
| 128 | + tx_scope.span.log_kv({ |
| 129 | + "python.exception.type": exc_type, |
| 130 | + "python.exception.val": exc_val, |
| 131 | + "python.exception.tb": exc_tb |
| 132 | + }) |
| 133 | +---- |
| 134 | + |
0 commit comments