0

我有一个 web 应用程序 APPA,它调用一个 webservice master,它调用另一个 webservice slave。为了关联跟踪,我使用了 opentelemetry。当master调用slave时,我可以在 Jaeger 中看到跟踪,但是当 Web 应用程序调用master时,我看到只有 Web 应用程序被调用,没有跟踪与 Web 服务相关,我可以分别看到来自 2 个相关 Web 服务的跟踪:

在此处输入图像描述

但是当我无法使用 Web 应用程序时,我只能看到来自 Web 应用程序的痕迹,而不是所有 3 个组件: 在此处输入图像描述

这是我在 Web 应用程序中所做的事情:

import { context, getSpan, setSpan } from '@opentelemetry/api';
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing';
import { CollectorTraceExporter } from '@opentelemetry/exporter-collector';
import { WebTracerProvider } from '@opentelemetry/web';
import { FetchInstrumentation } from '@opentelemetry/instrumentation-fetch';
import { ZoneContextManager } from '@opentelemetry/context-zone';
import { B3Propagator } from '@opentelemetry/propagator-b3';
import { registerInstrumentations } from '@opentelemetry/instrumentation';

const provider = new WebTracerProvider();

registerInstrumentations({
  instrumentations: [
    new FetchInstrumentation({
      ignoreUrls: [/localhost:8090\/sockjs-node/],
      propagateTraceHeaderCorsUrls: [
        'https://cors-test.appspot.com/test',
        'https://httpbin.org/get',
      ],
      clearTimingResources: true
    }),
  ],
  tracerProvider: provider,
});

provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
provider.addSpanProcessor(new SimpleSpanProcessor(new CollectorTraceExporter({
  serviceName: 'nodejs-opentelemetry-web-fetch',
  url: 'http://localhost:55681/v1/traces'
})));
provider.register({
  contextManager: new ZoneContextManager(),
  propagator: new B3Propagator(),
});

const webTracerWithZone = provider.getTracer('example-tracer-web');

const getData = (url) => fetch(url, {
  method: 'GET',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
});

// example of keeping track of context between async operations
const prepareClickEvent = () => {
  const url = 'http://localhost:3000';

  const element = document.getElementById('button1');

  const onClick = () => {
    const singleSpan = webTracerWithZone.startSpan(`files-series-info`);
    context.with(setSpan(context.active(), singleSpan), () => {
      getData(url).then((_data) => {
        getSpan(context.active()).addEvent('fetching-single-span-completed');
        singleSpan.end();
      });
    });
    for (let i = 0, j = 5; i < j; i += 1) {
      const span = webTracerWithZone.startSpan(`files-series-info-${i}`);
      context.with(setSpan(context.active(), span), () => {
        getData(url).then((_data) => {
          getSpan(context.active()).addEvent(`fetching-span-${i}-completed`);
          span.end();
        });
      });
    }
  };
  element.addEventListener('click', onClick);
};

window.addEventListener('load', prepareClickEvent);

我在核对所有 3 个组件时做错了什么?

4

0 回答 0