Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How disable RestTemplate monitoring #304

Closed
chendy560 opened this issue Dec 16, 2017 · 8 comments
Closed

How disable RestTemplate monitoring #304

chendy560 opened this issue Dec 16, 2017 · 8 comments
Labels
question A user question, probably better suited for StackOverflow

Comments

@chendy560
Copy link

There are many such codes in my system:

    public void doSomething(String id) {
        URI uri = UriComponentsBuilder.fromHttpUrl("http://...").queryParam("id", id).build().encode().toUri();
        RequestEntity<Void> request = RequestEntity.get(uri).build();
        restTemplate.exchange(request, SomeThing.class);
        //...
    }

Each different id will bring a different URI, as well as a new tag. It will take up a lot of memory, so how disable it?

@spencergibb
Copy link
Collaborator

Why don't you use the rest template it URI templates?

@chendy560
Copy link
Author

@spencergibb
Sorry, I did not know it before😂
It works, thanks

@dougbacelar
Copy link

@spencergibb Code I am working on cannot be easily refactored. Is there a way to turn off metric gathering for a particular RestTemplate bean?

I am using the code below to customize the RestTemplate

new MetricsRestTemplateCustomizer( meterRegistry, ( urlTemplate, request, response ) ->
  Arrays.asList(
      RestTemplateExchangeTags.method(request),
      RestTemplateExchangeTags.status(response),
      RestTemplateExchangeTags.clientName(request)
  ), 
  "myTestMetric"
)
.customize( restTemplate );

This creates a metric called myTestMetric and it does not record the URI, just like I wanted. However the automatically generated http_client_requests_seconds_* are still being recorded.

Each request would generate a new metric as the url is very complex.

@dougbacelar
Copy link

dougbacelar commented Apr 18, 2018

I fixed my problem creating the following @Bean:

    @Bean
    RestTemplateExchangeTagsProvider restTemplateExchangeTagsProvider() {

        new RestTemplateExchangeTagsProvider() {
            @Override
            Iterable<Tag> getTags( String urlTemplate, HttpRequest request, ClientHttpResponse response ) {

                Tag uriTag

                if ( StringUtils.hasText( urlTemplate ) ) {
                    uriTag = RestTemplateExchangeTags.uri( urlTemplate )
                } else if ( request.URI.path.startsWith( '/faulty-endpoint' ) ) {
                    uriTag = RestTemplateExchangeTags.uri( request.URI.path )
                } else {
                    uriTag = RestTemplateExchangeTags.uri( request )
                }

                [ RestTemplateExchangeTags.method( request ),
                  uriTag,
                  RestTemplateExchangeTags.status( response ),
                  RestTemplateExchangeTags.clientName( request ) ]
            }
        }
    }

If the URI.path starts with /faulty-endpoint, it will tag the URI.path instead of the full URI with all parameters.

The above is simply a slightly modified version of the DefaultRestTemplateExchangeTagsProvider

@izeye izeye added the question A user question, probably better suited for StackOverflow label Jun 6, 2018
@chandreshlko
Copy link

chandreshlko commented Jun 24, 2020

I fixed my problem creating the following @Bean:

    @Bean
    RestTemplateExchangeTagsProvider restTemplateExchangeTagsProvider() {

        new RestTemplateExchangeTagsProvider() {
            @Override
            Iterable<Tag> getTags( String urlTemplate, HttpRequest request, ClientHttpResponse response ) {

                Tag uriTag

                if ( StringUtils.hasText( urlTemplate ) ) {
                    uriTag = RestTemplateExchangeTags.uri( urlTemplate )
                } else if ( request.URI.path.startsWith( '/faulty-endpoint' ) ) {
                    uriTag = RestTemplateExchangeTags.uri( request.URI.path )
                } else {
                    uriTag = RestTemplateExchangeTags.uri( request )
                }

                [ RestTemplateExchangeTags.method( request ),
                  uriTag,
                  RestTemplateExchangeTags.status( response ),
                  RestTemplateExchangeTags.clientName( request ) ]
            }
        }
    }

If the URI.path starts with /faulty-endpoint, it will tag the URI.path instead of the full URI with all parameters.

The above is simply a slightly modified version of the DefaultRestTemplateExchangeTagsProvider

I am calling a third party API for logging the metrics
the URI is like "/api/xxx/123" the last 3 digits are changes
my requirement is to count the trigger for "/api/xxx" for this metrics
I have other api call that i don't want to change I am using spring boot (RestTemplate) and the configuration is global
any suggestion how can i do this.

@shakuzen
Copy link
Member

@chandreshlko I'm not understanding exactly what you're trying to accomplish. It will probably be easier to figure this out through chat. Feel free to join us on Slack at https://slack.micrometer.io.

@faramarzaf
Copy link

faramarzaf commented Jul 9, 2023

@chandreshlko and @shakuzen I have the same problem, I am invoking rest API that has a path variable like this -> /api/{id}
but I don't want to log /{id} part by each request send to 3rd party because of the high request rate which leads to using more space. Is there any solution to exclude the specific path? I mean something like bucket metric for Rest controllers?

@bclozel
Copy link
Contributor

bclozel commented Jul 9, 2023

@faramarzaf can you ask this question on StackOverflow? Please add to your question the relevant information like: which spring boot version you are using, how are you creating and using the rest template instance, show which metrics are collected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A user question, probably better suited for StackOverflow
Projects
None yet
Development

No branches or pull requests

8 participants