Skip to content

Conversation

Tussle0410
Copy link
Contributor

I've modified the DefaultClientRequestObservationConvention.class to reduce redundant calls to getCarrier() and getResponse() in ClientRequestObservationContext.

Previously, getCarrier() was called 8 times and getResponse() was called 3 times throughout the code.

I've modified the implementation to retrieve the ClientHttpRequest and ClientHttpResponse objects just once per method and then use these references for subsequent operations, making the code more efficient and easier to maintain.

	protected KeyValue clientName(ClientRequestObservationContext context) {
		if (context.getCarrier() != null && context.getCarrier().getURI().getHost() != null) {
			return KeyValue.of(LowCardinalityKeyNames.CLIENT_NAME, context.getCarrier().getURI().getHost());
		}
		return CLIENT_NAME_NONE;
	}

I've modified the existing code as shown below.

	protected KeyValue clientName(ClientRequestObservationContext context) {
		ClientHttpRequest request = getClientHttpRequest(context);
		if (request != null && request.getURI().getHost() != null) {
			return KeyValue.of(LowCardinalityKeyNames.CLIENT_NAME, request.getURI().getHost());
		}
		return CLIENT_NAME_NONE;
	}

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 11, 2025
@bclozel
Copy link
Member

bclozel commented Jun 5, 2025

Thanks for the proposal, but I don't think it's worth introducing methods for single liners like this. Cheers!

@bclozel bclozel closed this Jun 5, 2025
@bclozel bclozel added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants