open-telemetry / opentelemetry-php

The OpenTelemetry PHP Library
https://opentelemetry.io/docs/instrumentation/php/
Apache License 2.0
724 stars 182 forks source link

How do I report tracing data asynchronously #1010

Closed runtu666 closed 1 year ago

runtu666 commented 1 year ago

How do I save the tracing data first, such as saving it in a file, or saving it in a message queue or database, and then start another program to report these link tracking data to jeager?Is there a related demo for reference?

runtu666 commented 1 year ago

Because I found that the data reporting of php is performed synchronously, which will slow down our request response. We hope that the data can be reported asynchronously, so as to achieve the purpose of not affecting the business

brettmc commented 1 year ago

No, we don't have any examples of using another data source to enqueue telemetry. We don't have a built-in way to asynchronous send yet, but we do have some future plans (probably using fibers, so not compatible with 7.4).

Have you looked at running the opentelemetry collector as an agent? Having a collector co-located with your application allows for very fast exporting. It also has some other features that can help you here, such as persistant queue and batch sending.

brettmc commented 1 year ago

such as saving it in a file

We do support exporting telemetry to file. There is also an experimental part of the spec that deals with json file output for OTLP: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/file-exporter.md

You could achieve this now using stream transports to file:

$file = fopen('/path/to/traces.jsonl, 'a');
$transport = (new OpenTelemetry\SDK\Common\Export\Stream\StreamTransportFactory())->create($file, ContentTypes::NDJSON);
$exporter = new OpenTelemetry\Contrib\Otlp\SpanExporter($transport);
runtu666 commented 1 year ago

ok,Then how to report the json in the file to the collector? @brettmc

brettmc commented 1 year ago

ok,Then how to report the json in the file to the collector?

I'm not aware of any tools that can do that, so you'd probably have to write something. You could look at how the exporter currently sends JSON signals over http to get you started - it's not complicated.

If you're already using the collector, then I think that the best thing you can do today is to run a collector as an agent (eg a sidecar). It's still synchronous, but so too is sending to a queue or database, yes?

If you are using a modern php runtime (react/amp/etc) you could also look at https://github.com/Nevay/opentelemetry-async-sdk. It's unsupported by us, but it will hopefully be integrated into our SDK in the future (after we go stable).

runtu666 commented 1 year ago

Now that collector is used, does it need to modify the code to switch to agent collection?

brettmc commented 1 year ago

No, the collector can act as an agent or gateway/service with no changes: https://opentelemetry.io/docs/collector/deployment/ (it's more to do with placement and usage; a collector close to application is called an agent, a central collector accepting telemetry from multiple sources is a gateway). For your application code, you only need to change your endpoint (assuming you are using OTLP) and configure the collector to forward to jaeger: application -> collector -> jaeger

runtu666 commented 1 year ago

ok thanks

brettmc commented 1 year ago

Closing as I think this has been answered as best we can at the moment.