feat: Opentelemetry tracing for Basic Crawler, Http Crawler and Browser Crawler #3319
+1,149
−411
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This pull request adds Opentelemetry support in the basic crawler, http crawler and browser crawler classes. This functionality is completely opt in through the
enableTelemetryconstructor parameter.Spans are created through this
withSpanfunction of theBasicCrawlerclass.This function checks if tracing is enabled and if it is, wraps its callback in a span context. If it is not, the callback is called directly.
Another new function in
BasicCrawleriswrapLogWithTracingThis function is called in the
BasicCrawlerconstructor and overwrites theinternalfunction of the logger to also emit span events. This should work for the base apify logger as well as any custom loggers so long as they still use theinternalfunction in the same manner. This wrapper can be enabled using the newcollectLogsconstructor parameter.Fixes
issue: #2955
Example Usage
The following snippet requires
crawlee,@opentelemetry/sdk-nodeand,@opentelemetry/apiThis crawler created the following Otel traces
otel-traces.json
To view these easily, use an open telemetry viewer like Jaeger and upload the traces. It is available as a docker image.
As should be obvious from the screenshot above, Opentelemetry can provide a lot more data and context to what an application is doing than can be gathered from logs alone.
Also, once this is merged, we can apply to be listed as adopters of native Opentelemetry alongside Nextjs and SvelteKit on the Opentelemetry website!