Scrivio Interview library
CodingIntermediate6 min read · updated 12 November 2026

You want every observation for HTTP server requests to add the tenant id to spans but not to metrics, and to rename a tag. How would you do this with ObservationConvention or ObservationFilter?

We write a class that extends DefaultServerRequestObservationConvention, override getHighCardinalityKeyValues to add the tenant id, which then reaches spans but not metrics, and override getLowCardinalityKeyValues to rename a metric tag. Registered as a bean, Spring Boot uses it for every server request observation. For changes that apply to all observations, an ObservationFilter can modify the context just before each observation is stopped.

Why this question is asked

Multi-tenant services often need to answer the question which tenant was this slow request for, and traces are the natural place for that answer. At the same time, adding the tenant id as a metric tag with thousands of tenants would multiply the http.server.requests series and the bill. Interviewers ask this to see whether we know the extension points Spring provides for its own instrumentation, and whether we understand the difference between low and high cardinality data. In this answer, we will recap how Spring creates the server request observation, write a custom convention that adds the tenant id and renames a tag, see how Boot picks it up, compare it with an ObservationFilter, and check the result.

1A Quick Recap of the Server Request Observation

For every incoming HTTP request, Spring MVC's ServerHttpObservationFilter creates an observation named http.server.requests. The observation carries a ServerRequestObservationContext, which holds the request (the carrier) and the response. An ObservationConvention is the object that decides the observation's name, its contextual name (the span name, such as GET /orders/{id}) and its key values.

The default convention is DefaultServerRequestObservationConvention. It adds low cardinality key values method, uri, status, outcome and exception, and a high cardinality key value http.url. As we saw with the Observation API, the meter handler uses only the low cardinality values as tags, while the tracing handler adds both kinds to the span. So the convention is exactly the place to decide what goes where.

2Writing the Custom Convention

We extend the default rather than implementing the interface from scratch, so we keep all the standard key values and only add or change what we need. The tenant id here comes from a request header; in a real service it might come from the authenticated principal.

Java
import io.micrometer.common.KeyValue;
import io.micrometer.common.KeyValues;
import org.springframework.http.server.observation.DefaultServerRequestObservationConvention;
import org.springframework.http.server.observation.ServerRequestObservationContext;
import org.springframework.stereotype.Component;
@Component
public class TenantServerRequestConvention extends DefaultServerRequestObservationConvention {
@Override
public KeyValues getHighCardinalityKeyValues(ServerRequestObservationContext context) {
String tenant = context.getCarrier().getHeader("X-Tenant-Id");
return super.getHighCardinalityKeyValues(context)
.and("tenant.id", tenant != null ? tenant : "unknown"); // spans only
}
@Override
public KeyValues getLowCardinalityKeyValues(ServerRequestObservationContext context) {
// Rename the "outcome" tag to "result" to match our dashboards
return KeyValues.of(super.getLowCardinalityKeyValues(context).stream()
.map(kv -> kv.getKey().equals("outcome") ? KeyValue.of("result", kv.getValue()) : kv)
.toList());
}
}

Notice the two methods have different jobs. The high cardinality method adds the tenant id on top of the defaults, so it lands on spans only. The low cardinality method keeps every default tag but swaps the key of one, so the metric tag outcome becomes result. We return a value for the tenant even when the header is missing, because a key value without a value is not allowed.

3How Spring Boot Picks It Up

Spring Boot's web MVC observation auto-configuration looks for a bean of type ServerRequestObservationConvention and, if it finds one, passes it to the observation filter instead of the default. Annotating the class with @Component, or declaring it with @Bean, is all the registration needed. For WebFlux the idea is the same, but the classes live in the reactive observation package, org.springframework.http.server.reactive.observation, so import the right one.

Also notice that a convention is per instrumentation. This one changes server requests only; client requests from RestClient or WebClient have their own ClientRequestObservationConvention, and Kafka or data observations have theirs.

Step throughWhere a server request observation is shaped
  1. Request arrivesServerHttpObservationFilter starts http.server.requests
  2. ConventionOur TenantServerRequestConvention supplies name and key values
  3. ObservationFiltersRun just before stop, may edit the context
  4. Meter handlerLow cardinality values only: method, uri, status, result
  5. Tracing handlerLow and high cardinality: adds tenant.id, http.url

5 steps in order. Play them, or step through them one at a time.

The convention decides per instrumentation; filters apply to every observation.

4When to Use an ObservationFilter Instead

An ObservationFilter is a function that receives the context of every observation just before it is stopped and may change it. That makes it the tool for rules that apply everywhere, not just to server requests: adding the deployment region to all observations, or removing a key value that some library adds.

Java
@Bean
ObservationFilter regionFilter(@Value("${app.region}") String region) {
return context -> context.addLowCardinalityKeyValue(KeyValue.of("region", region));
}
@Bean
ObservationFilter tenantOnServerRequests() {
return context -> {
if (context instanceof ServerRequestObservationContext req) {
String tenant = req.getCarrier().getHeader("X-Tenant-Id");
context.addHighCardinalityKeyValue(KeyValue.of("tenant.id", tenant != null ? tenant : "unknown"));
}
return context; // a filter must return the context
};
}

The second filter shows that a filter could also solve our tenant problem by checking the context type. It works, but the convention is clearer for a change that belongs to one instrumentation, and it lets us rename tags cleanly rather than adding and removing values. Spring Boot registers ObservationFilter beans with the registry automatically. For a constant value on everything, Boot also offers management.observations.key-values.region=eu-west-1 without any code.

5Checking the Result and Keeping Tenants Out of Metrics

After a request with X-Tenant-Id: acme, the metric and the span look different, which is what we wanted. We can confirm it with the Prometheus endpoint and the tracing UI, or in a test with ObservationRegistry assertions from micrometer-observation-test.

Properties
# GET /actuator/prometheus
http_server_requests_seconds_count{method="GET",uri="/orders/{id}",status="200",result="SUCCESS",...} 1
# no tenant label here
# Span in the tracing backend
# name: http get /orders/{id}
# tags: method=GET uri=/orders/{id} status=200 result=SUCCESS tenant.id=acme http.url=/orders/7

Keeping the tenant id out of metrics is a deliberate choice. With 3,000 tenants, a tenant tag on http.server.requests would multiply its series by up to 3,000, and with a histogram enabled that means millions of series. If the business does need per-tenant latency for a handful of large tenants, a safer pattern is a low cardinality tier tag such as tenant.tier=enterprise, or a separate metric limited to an allow list. One more caution: renaming tags breaks existing dashboards and alerts that use the old name, so roll it out together with updated queries.

6Follow-up questions

What is the difference between overriding the convention and using an ObservationPredicate?

A convention shapes an observation: name and key values. A predicate decides whether an observation is created at all, for example skipping requests to /actuator. They are often used together, the predicate to drop noise and the convention to shape what remains.

Could we implement ServerRequestObservationConvention directly?

Yes, but then we must produce every key value ourselves and we lose the defaults, including the uri template that keeps metrics bounded. Extending the default and calling super is safer and shorter, and it keeps working when Spring adds new standard key values.

Why not add the tenant id to the MDC instead?

The MDC puts it into log lines, which is useful too, but it does not reach the span. We can do both: the convention for traces and a servlet filter or interceptor for the MDC. With Micrometer Tracing, baggage can also carry the tenant across services and into the MDC if configured with management.tracing.baggage settings.

7Conclusion

In this answer, we customised Spring's HTTP server observation. We extended DefaultServerRequestObservationConvention, added the tenant id in getHighCardinalityKeyValues so it reaches spans but not metrics, and renamed the outcome tag in getLowCardinalityKeyValues. Registering the class as a bean is enough for Spring Boot to use it. We compared this with ObservationFilter, which edits the context of every observation before it is stopped, and we saw why the tenant id stays out of metrics when tenants are many.

What to say in the interviewAbout 80 seconds aloud

Open with

For something specific to HTTP server requests, I'd write a custom convention. I'd extend DefaultServerRequestObservationConvention so I keep the standard key values like method, uri, status and outcome.

Then, if they want more

Then I'd override getHighCardinalityKeyValues, call super, and add tenant.id read from the request header or the principal. High cardinality values go to spans but the meter handler ignores them, so the tenant shows up in traces without creating a series per tenant. To rename a tag, I'd override getLowCardinalityKeyValues and map the outcome key to result, for example.

I register the class as a bean, and Spring Boot's MVC observation auto-configuration uses it instead of the default; for WebFlux it's the reactive variant of the same class.

If the change should apply to every observation, like adding a region, I'd use an ObservationFilter bean instead, which gets each context just before stop and can add or change key values, or for a constant just the management.observations.key-values property.

I'd keep the tenant out of metrics on purpose: with thousands of tenants and histograms it would multiply the series massively. And because renaming a tag breaks dashboards, I'd update the queries in the same change.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "You want every observation for HTTP server requests to add the tenant id to spans but...": https://scrivio-interview-library.netlify.app/spring-boot/observability/687b5a89d8/