Skip to content

feat(http_telemetry)!: add client telemetry and use it in fetch - #1035

Draft
Evgeny Fedorov (evgenyfedorov2) wants to merge 17 commits into
microsoft:mainfrom
evgenyfedorov2:u/efedorov/http-telemetry-pr3
Draft

Evgeny Fedorov (evgenyfedorov2) wants to merge 17 commits into
microsoft:mainfrom
evgenyfedorov2:u/efedorov/http-telemetry-pr3

Conversation

@evgenyfedorov2

Copy link
Copy Markdown
Member

Summary

Adds client telemetry to http_telemetry. fetch now emits its request and connection telemetry as observed events through the sink of its deps, in place of tracing logs and OpenTelemetry metrics.

// One layer for each whole request, one for each attempt.
let total = Telemetry::client(sink.clone()).report_total_duration(true);
let attempt = Telemetry::client(sink.clone()).request_header_data_classes([(REQUEST_ID, PUBLIC)]);
let service = (total, attempt, handler).into_service();

// fetch: the deps take the sink, and the standard pipeline emits the events.
let client = HttpClient::builder_tokio(TokioDeps::new(sink)).name("books_client").build();
  • Events: http.client.request.complete (Debug), http.client.request.error (Warn) and http.client.request.abandoned (Warn) for each attempt. With report_total_duration(true), a layer emits http.client.request.total.complete, http.client.request.total.error and http.client.request.total.abandoned for a whole request.
  • Metrics: the histograms http.client.request.duration and http.client.request.total_duration, in seconds, with the bucket boundaries of the OpenTelemetry HTTP metrics conventions.
  • Attributes: http.request.method, url.scheme, server.address, server.port, url.template, network.protocol.name, network.protocol.version, http.response.status_code and error.type. The events also have http.request.method_original, url.path.redacted and exception.message. Each processor redacts url.path.redacted with its own redactor.
  • Headers and bodies: the client layer has the header and body options of the server layer, such as request_header_data_classes and response_body_media_types. If the layer logs a response body, it emits the complete event when the caller drops the body.
  • error_labels_with takes the caller's own function for error.type.
  • TelemetryLayer takes its side as a type parameter: TelemetryLayer<Server> or TelemetryLayer<Client>.

In fetch (breaking):

  • TokioDeps, FakeDeps and CustomDeps require an observed::Sink, so TokioDeps::new, FakeDeps::new, HttpClient::new_tokio and HttpClient::new_fake take one. CustomContext::sink gives it to a custom transport.
  • The standard pipeline emits the attempt events and the total events. StandardRequestPipeline::attempt_telemetry and total_telemetry configure them, and a custom pipeline gets the layer from PipelineContext::client_telemetry(). Each event has fetch.runtime, fetch.transport and http.client.name.
  • The Tokio transport emits http.client.connection.start, http.client.connection.success, http.client.connection.error and http.client.connection.closed, which record http.client.connection.setup.duration and http.client.connection.duration.
  • Removed: the Logging and Metrics handlers, TelemetryAttributes, attempt_logs, attempt_metrics, total_metrics, redaction_engine, HttpClientBuilder::meter_provider, PipelineContext::meter and CustomContext::meter. An application turns the events into metrics through its sink.

seatbelt still logs through tracing. Until it supports observed, fetch records no resilience metrics. A follow-up PR adds that support.

Changes in other crates

  • fetch_hyper (breaking): emits its connection telemetry as observed events. HyperTransportBuilder::new takes the sink, the meter setter is removed, and client_attributes adds fetch.runtime, fetch.transport and http.client.name to the events.
  • observed: the #[histogram] attribute takes boundaries, the bucket boundaries that an exporter passes to the OpenTelemetry histogram. MetricDescription gets with_boundaries and boundaries.
  • http_extensions (breaking): ExtensionsExt::uri_template_label gives no label for a plain path, so a URL given as text records url.template as unknown, not its whole path and query. HttpBody implements Default.
  • templated_uri: PathAndQuery::is_templated tells a templated path from a path given as text.
  • fetch_winhttp_impl: the WinHTTP transport reads its sink from CustomContext::sink.

Overhead

cargo bench -p fetch --features test-util --bench pipelines, one run of each case on main at 96d26fbc and on this branch, alternated case by case, on an Apple M3 Max, macOS 27.2.1, rustc 1.97.1. Each case sends GET https://example.com/some/path?query=value to a fake transport. On main, the requests have no tracing subscriber and use the global meter provider. On this branch, the first two cases use Sink::noop().

Case Setup Time Allocations Bytes
standard_pipeline Default pipeline 2.64 → 2.18 µs 29 → 24 3824 → 3736
custom_standard_pipeline Timeouts and retry, then Logging and Metrics on main, client_telemetry() on this branch 1.92 → 1.52 µs 25 → 22 3764 → 3709
standard_pipeline_observed Default pipeline, a processor that reads every field of every event 2.92 µs 28 3880

`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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant