Repository navigation
feat(http_telemetry)!: add client telemetry and use it in fetch - #1035
Draft
Evgeny Fedorov (evgenyfedorov2) wants to merge 17 commits into
Draft
Evgeny Fedorov (evgenyfedorov2) wants to merge 17 commits into
Evgeny Fedorov (evgenyfedorov2) wants to merge 17 commits into
Conversation
`PathAndQuery::is_templated` tells a path that was built from a template apart from a path that was given as text.
`ExtensionsExt::uri_template_label` fell back to `PathAndQuery::template`, which turns a plain path into text. So a URL given as a string got its whole path and query as its template. `fetch` logged that text as `url.path.template` and metered it as `url.template`, unredacted, next to the redacted `url.path.redacted` of the same event. Now only a `UriTemplateLabel` extension or a templated path gives a label. A plain path gives none.
`Telemetry::client` creates a telemetry layer for HTTP clients. Each attempt emits a complete, error or abandoned event and records `http.client.request.duration`. A layer with `report_total_duration(true)` emits the total events instead, and records `http.client.request.total_duration` for the whole request with its retries. `TelemetryLayer` gets the side parameter `R`, `Client` or `Server`, as `CompressionLayer` in `http_compression` has. One drop guard finishes the requests of both sides. A parity test in `fetch` compares the new layer with the `Logging` and `Metrics` handlers of `fetch` on the same requests.
`TokioDeps`, `FakeDeps` and `CustomDeps` get a required `sink` field. Each request pipeline reads it with `PipelineContext::sink`, also when the client builds one pipeline for each thread. A later change sends the request telemetry of `fetch` through it. The constructors take the sink: `TokioDeps::new`, `TokioDeps::with_clock`, `FakeDeps::new`, `FakeDeps::with_clock`, `HttpClient::new_tokio` and `HttpClient::new_fake`. `TokioDeps` and `FakeDeps` lose `Default`, and `FakeDeps` loses `From<&Clock>`, because a `Sink` has no default value. `fetch_winhttp_impl` passes the sink of `WinHttpDeps` to `CustomDeps`. The tests and examples of `fetch` and `fetch_azure` pass `Sink::noop()`.
`MetricDescription` gets `with_boundaries` and `boundaries`, and the `#[histogram]` attribute of `#[event]` gets the option `boundaries = <expr>`, an expression of type `&'static [f64]`. An exporter that creates an OpenTelemetry histogram passes the boundaries to it. Without them, the SDK uses its default buckets, which suit milliseconds. Only `#[histogram]` takes the option. A second `boundaries` in one attribute is an error.
The three duration histograms of the server and the six of the client declare the boundaries from 0.005 to 10 seconds that the OpenTelemetry HTTP metrics conventions give for `http.server.request.duration` and `http.client.request.duration`. The `Debug` output of `Telemetry<T>` shows its inner service when `T` is `Debug`, as the other middleware of the workspace does. The client benchmark case `identity_dimensions` is renamed to `client_attributes`, the term that `fetch` uses for its three fields.
The request telemetry of `fetch` comes from the client layer of `http_telemetry`, as `observed` events through the sink of the client deps. The standard pipeline has an attempt layer just above dispatch and a total layer outside the retries and hedging. Each event has `fetch.runtime`, `fetch.transport` and `http.client.name`. `PipelineContext::client_telemetry` returns the layer of a client, so a custom pipeline can stack it. `StandardRequestPipeline::attempt_telemetry` and `total_telemetry` replace `attempt_logs`, `attempt_metrics` and `total_metrics`. The `Logging` and `Metrics` handlers, `TelemetryAttributes`, and `redaction_engine` on `HttpClientBuilder`, `PipelineContext` and `ClientOptions` are removed. A sink processor redacts the request path with its own redaction engine. The request metrics no longer reach the `fetch` meter, which keeps the connection metrics and the instruments of `CustomContext::meter`. `fetch` no longer depends on `tracing`, `smallvec`, `opentelemetry-semantic-conventions` and `data_privacy`. The `fetch_telemetry` example prints the events of a retried request.
`TelemetryLayer<Client>` gets `request_header_data_classes` and `response_header_data_classes`, with the signatures and rules of the server's methods. The client events log each configured header that is present as `http.request.header.<name>` or `http.response.header.<name>`, redacted by its data class. The layer reads the request headers when the request reaches it, and the response headers when the handler returns a response, so only the complete events have response headers. A layer without configured headers adds no allocation. The header fields and the interner of field names move from the server module to the crate root, and both sides use them. In the crate docs, the header rows of both sides link to `TelemetryLayer`, because a link to a method that both sides have reaches only the client's. The header tests run both sides, the client benchmark gets the header cases of the server benchmark, and the attempt layer of the `http_telemetry_basic` example logs one request header and one response header.
The default body is empty and has the default `HttpBodyOptions`. A caller can take the body out of a request with `std::mem::take` and put a new body back, without moving the whole request.
`TelemetryLayer<Client>` gets `request_body_media_types`, `response_body_media_types` and `body_size_limit`, with the rules of the server's methods. Every client event logs the request body as `http.request.body.content`, and the complete events log the response body as `http.response.body.content`. A logged response body that streams gets its complete event when the caller drops the body, and the event logs the bytes that the caller read. The duration still ends when the handler returns. Both sides copy a body in memory straight into the text of its event. A streamed body goes into room for the whole size limit, taken from one `bytesbuf` `GlobalPool` that all layers share. The media types and the body buffer move from the server module to the crate root, and both sides use them. The body tests run both sides, the client benchmark gets the body cases of the server benchmark, and the attempt layer of the `http_telemetry_basic` example logs JSON bodies.
`fetch_hyper` emits its connection telemetry as `observed` events through a sink, in place of its `tracing` logs and OpenTelemetry histograms. `HyperTransportBuilder::new` takes the sink, and the `meter` setter is removed. The events are `http.client.connection.start`, `http.client.connection.success`, `http.client.connection.error` and `http.client.connection.closed`. The success and the error event record `http.client.connection.setup.duration`, and the closed event records `http.client.connection.duration`. The histograms keep their names, units and bucket boundaries. The Tokio transport of `fetch` passes the sink of `TokioDeps`, so the connection metrics no longer reach the `fetch` meter. A test of `fetch` checks the start and the success event of a Tokio client. `fetch_hyper` no longer depends on `opentelemetry`, `opentelemetry-semantic-conventions` and `tracing`, and the workspace no longer lists `opentelemetry-semantic-conventions`.
The telemetry page of `fetch` lists the four connection events of the Tokio transport and their fields, and keeps the two metrics. The docs of `HttpClientBuilder::meter_provider` say that the provider records the resilience metrics and the instruments of `CustomContext::meter`. The `fetch_connection_scaling` example prints the request events and the connection events, through a printer that it shares with `fetch_telemetry`. The printer holds stdout while it prints an event, so the lines of events from different threads do not mix.
The four connection events get `fetch.runtime`, `fetch.transport` and `http.client.name`. All four log them, and the success, error and closed events record them as metric dimensions, so the connection metrics carry them again, as they did when they were recorded on the `fetch` meter. `HyperTransportBuilder::client_attributes` sets the three values and turns them into `observed::Text` once. Without that call, the events have no such fields. The Tokio transport of `fetch` passes the runtime, the transport and the name of its client. The connection test of `fetch` checks the three values on the start and the success event, and the telemetry page of `fetch` says that each connection event has them.
The client benchmark gets a request whose redacted path two processors print, and a PURGE request whose original method two processors read. The server benchmark gets a response to HEAD, a route added at run time, a route parameter without a data class, a request with three lines of one logged header, and a response body that fails with a refused connection.
…ocessor panics The crate docs add that an excluded request is also left out of both metrics, that a custom pipeline of `fetch` emits request events only if it stacks `PipelineContext::client_telemetry()`, and that after a processor panic the middleware emits no later event of the request.
The table of connection events gives the message of each event. The section on client attributes says that the metrics of the request and connection events have them as attributes.
`fetch` has no OpenTelemetry meter. An application records metrics through its sink, so `HttpClientBuilder::meter_provider`, `PipelineContext::meter` and `CustomContext::meter` are removed, with the `opentelemetry` dependency and the `metrics` feature of `seatbelt`. Until `seatbelt` supports `observed`, `fetch` records no resilience metrics. `CustomContext::sink` gives a custom transport the sink of the deps of its client. The Tokio transport and the WinHTTP transport read it. The crate-private `ClientAttributes` is removed. `PipelineContext` reads the runtime and transport names from the transport, and the client name is part of the client options.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds client telemetry to
http_telemetry.fetchnow emits its request and connection telemetry asobservedevents through the sink of its deps, in place oftracinglogs and OpenTelemetry metrics.http.client.request.complete(Debug),http.client.request.error(Warn) andhttp.client.request.abandoned(Warn) for each attempt. Withreport_total_duration(true), a layer emitshttp.client.request.total.complete,http.client.request.total.errorandhttp.client.request.total.abandonedfor a whole request.http.client.request.durationandhttp.client.request.total_duration, in seconds, with the bucket boundaries of the OpenTelemetry HTTP metrics conventions.http.request.method,url.scheme,server.address,server.port,url.template,network.protocol.name,network.protocol.version,http.response.status_codeanderror.type. The events also havehttp.request.method_original,url.path.redactedandexception.message. Each processor redactsurl.path.redactedwith its own redactor.request_header_data_classesandresponse_body_media_types. If the layer logs a response body, it emits the complete event when the caller drops the body.error_labels_withtakes the caller's own function forerror.type.TelemetryLayertakes its side as a type parameter:TelemetryLayer<Server>orTelemetryLayer<Client>.In
fetch(breaking):TokioDeps,FakeDepsandCustomDepsrequire anobserved::Sink, soTokioDeps::new,FakeDeps::new,HttpClient::new_tokioandHttpClient::new_faketake one.CustomContext::sinkgives it to a custom transport.StandardRequestPipeline::attempt_telemetryandtotal_telemetryconfigure them, and a custom pipeline gets the layer fromPipelineContext::client_telemetry(). Each event hasfetch.runtime,fetch.transportandhttp.client.name.http.client.connection.start,http.client.connection.success,http.client.connection.errorandhttp.client.connection.closed, which recordhttp.client.connection.setup.durationandhttp.client.connection.duration.LoggingandMetricshandlers,TelemetryAttributes,attempt_logs,attempt_metrics,total_metrics,redaction_engine,HttpClientBuilder::meter_provider,PipelineContext::meterandCustomContext::meter. An application turns the events into metrics through its sink.seatbeltstill logs throughtracing. Until it supportsobserved,fetchrecords no resilience metrics. A follow-up PR adds that support.Changes in other crates
fetch_hyper(breaking): emits its connection telemetry asobservedevents.HyperTransportBuilder::newtakes the sink, themetersetter is removed, andclient_attributesaddsfetch.runtime,fetch.transportandhttp.client.nameto the events.observed: the#[histogram]attribute takesboundaries, the bucket boundaries that an exporter passes to the OpenTelemetry histogram.MetricDescriptiongetswith_boundariesandboundaries.http_extensions(breaking):ExtensionsExt::uri_template_labelgives no label for a plain path, so a URL given as text recordsurl.templateasunknown, not its whole path and query.HttpBodyimplementsDefault.templated_uri:PathAndQuery::is_templatedtells a templated path from a path given as text.fetch_winhttp_impl: the WinHTTP transport reads its sink fromCustomContext::sink.Overhead
cargo bench -p fetch --features test-util --bench pipelines, one run of each case onmainat96d26fbcand on this branch, alternated case by case, on an Apple M3 Max, macOS 27.2.1, rustc 1.97.1. Each case sendsGET https://example.com/some/path?query=valueto a fake transport. Onmain, the requests have notracingsubscriber and use the global meter provider. On this branch, the first two cases useSink::noop().standard_pipelinecustom_standard_pipelineLoggingandMetricsonmain,client_telemetry()on this branchstandard_pipeline_observed