All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.instrumentation.spring.web.v3_1.RestTemplateInterceptor Maven / Gradle / Ivy

There is a newer version: 2.8.0-alpha
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.instrumentation.spring.web.v3_1;

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import java.io.IOException;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

final class RestTemplateInterceptor implements ClientHttpRequestInterceptor {

  private final Instrumenter instrumenter;

  RestTemplateInterceptor(Instrumenter instrumenter) {
    this.instrumenter = instrumenter;
  }

  @Override
  public ClientHttpResponse intercept(
      HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
    Context parentContext = Context.current();
    if (!instrumenter.shouldStart(parentContext, request)) {
      return execution.execute(request, body);
    }

    Context context = instrumenter.start(parentContext, request);
    try (Scope ignored = context.makeCurrent()) {
      ClientHttpResponse response = execution.execute(request, body);
      instrumenter.end(context, request, response, null);
      return response;
    } catch (Throwable t) {
      instrumenter.end(context, request, null, t);
      throw t;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy