index.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const { WebTracerProvider } = require('@opentelemetry/sdk-trace-web');
  2. const { Resource } = require('@opentelemetry/resources');
  3. const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
  4. const { getWebAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-web');
  5. const { ConsoleSpanExporter, SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base');
  6. const { registerInstrumentations } = require('@opentelemetry/instrumentation');
  7. const { ZoneContextManager } = require('@opentelemetry/context-zone');
  8. const { context, propagation, trace } = require('@opentelemetry/api');
  9. const { TRACE_PARENT_HEADER, W3CTraceContextPropagator } = require('@opentelemetry/core');
  10. const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
  11. const ds = document.currentScript.dataset;
  12. if (ds.mode == 'dev') {
  13. console.log(ds, ds.appAlias);
  14. }
  15. const serviceName = ds.serviceName || 'unknown-front-service';
  16. const serviceVersion = ds.serviceVersion || '0.0.0';
  17. const commandLine = '-DAPP_NAME=' + (ds.appAlias || 'UNSET'); // 暂时为兼容otel-collector的修改
  18. const otelTraceExporter = ds.exporterOtlpTraceEndpoint;
  19. const resource = Resource.default().merge(
  20. new Resource({
  21. [SemanticResourceAttributes.SERVICE_NAME]: serviceName,
  22. [SemanticResourceAttributes.SERVICE_VERSION]: serviceVersion,
  23. [SemanticResourceAttributes.PROCESS_COMMAND_LINE]: commandLine
  24. })
  25. );
  26. const provider = new WebTracerProvider({
  27. resource: resource,
  28. });
  29. if (ds.mode == 'dev') {
  30. provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
  31. }
  32. provider.addSpanProcessor(new SimpleSpanProcessor(
  33. new OTLPTraceExporter({
  34. url: otelTraceExporter,
  35. })
  36. ));
  37. provider.register({
  38. contextManager: new ZoneContextManager(),
  39. propagator: new W3CTraceContextPropagator(),
  40. });
  41. const instrumentations = getWebAutoInstrumentations({
  42. '@opentelemetry/instrumentation-xml-http-request': {
  43. clearTimingResources: true,
  44. propagateTraceHeaderCorsUrls: [ // 只有能匹配到的对外请求,才会在请求时添加Traceparent请求头
  45. /.+/g,
  46. ],
  47. },
  48. '@opentelemetry/instrumentation-fetch': {
  49. propagateTraceHeaderCorsUrls: [
  50. /.+/g,
  51. ],
  52. },
  53. '@opentelemetry/instrumentation-user-interaction': {
  54. eventNames: ["click", "keypress"],
  55. shouldPreventSpanCreation: (event, element, span) => {
  56. console.log(event, element, span);
  57. return false
  58. }
  59. }
  60. });
  61. registerInstrumentations({
  62. instrumentations,
  63. tracerProvider: provider,
  64. });
  65. var defaultContext;
  66. const metaElement = Array.from(document.getElementsByTagName('meta')).find(
  67. e => e.getAttribute('name') === TRACE_PARENT_HEADER
  68. );
  69. const traceparent = (metaElement && metaElement.content) || '';
  70. if (traceparent == '') {
  71. const tracer = trace.getTracer(
  72. 'front-tracer'
  73. );
  74. tracer.startActiveSpan('Document Load', (span) => {
  75. defaultContext = context.active();
  76. span.end();
  77. })
  78. } else {
  79. defaultContext = propagation.extract(context.active(), { traceparent });
  80. }
  81. export let OneTrace = fn => {
  82. context.with(defaultContext, async () => {
  83. fn()
  84. })
  85. }