2

我是 OpenTelemetry 词的新手。我已经分别为我的服务创建了跨度,但是当我尝试使用上下文传播组合两个不同服务的跨度时,我无法成功。我使用了以下代码:

 // at client side:
  public static void sendContext(String resource) {

TextMapSetter<HttpURLConnection> setter =
    new TextMapSetter<HttpURLConnection>() {
      @Override
      public void set(HttpURLConnection carrier, String key, String value) {
        carrier.setRequestProperty(key, value);
      }
    };
HttpURLConnection transportLayer = null;
String urlString = "http://127.0.0.1:8080" + resource;
try {
  URL url = new URL(urlString);
  transportLayer = (HttpURLConnection) url.openConnection();
} catch (MalformedURLException ex) {
  System.out.println(ex.getMessage());
} catch (IOException e) {
  System.out.println(e.getMessage());
}
GlobalOpenTelemetry.getPropagators()
    .getTextMapPropagator()
    .inject(Context.current(), transportLayer, setter);
}



// at server side:
public static Context getContext(HttpServletRequest request) {

TextMapGetter<HttpServletRequest> getter =
    new TextMapGetter<HttpServletRequest>() {
      @Override
      public String get(HttpServletRequest carrier, String key) {
        Enumeration<String> headerNames = carrier.getHeaderNames();
        if (headerNames != null) {
          while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            System.out.println("headerNames.nextElement(): " + headerName);
            if (headerName.equals(key)) {
              String headerValue = request.getHeader(headerName);
              System.out.println("headerValue): " + headerValue);
              return headerValue;
            }
          }
        }

        return null;
      }

      @Override
      public Iterable<String> keys(HttpServletRequest carrier) {
        Set<String> set = new HashSet<String>();
        Enumeration<String> headerNames = carrier.getHeaderNames();
        if (headerNames != null) {
          while (headerNames.hasMoreElements()) {
            set.add(headerNames.nextElement());
          }
        }
        return set;
      }
    };
Context extractedContext =
    GlobalOpenTelemetry.getPropagators()
        .getTextMapPropagator()
        .extract(Context.current(), request, getter);

在服务器上,我无法获得父跨度。请帮助解决这个问题。

4

0 回答 0