Opentelemetry- Typescript 项目(Zipkin 导出器)

Opentelemetry- Typescript Project (Zipkin exporter)

我有一个简单的 hello-world typescript 项目,我正在尝试在其上设置 opentelemetry。我想将跟踪发送到控制台和 Zipkin。

我 运行 应用程序,但我在控制台和 Zipkin 上都没有得到任何跟踪器。当我导出 init 函数(在此函数中设置跟踪器)并将其导入 app.ts 文件时,我无法准确指出问题所在,但由于某些原因我无法获取跟踪器。我运行应用程序没有错误。

这是一个包含两个文件的简单项目:tracer.ts 和 app.ts

tracer.ts

import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { SimpleSpanProcessor, BatchSpanProcessor, ConsoleSpanExporter, } from '@opentelemetry/sdk-trace-base'; 
import { Resource } from '@opentelemetry/resources'; 
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; 
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin'; 
import { registerInstrumentations } from '@opentelemetry/instrumentation'; 
import { ExpressInstrumentation, ExpressRequestHookInformation } from 'opentelemetry-instrumentation-express'; 
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
    
const init = function (serviceName: string) {
    
//zipkin**************
    
//Specify zipkin url. defualt url is http://localhost:9411/api/v2/spans
    
const zipkinUrl = 'http://localhost'; 
const zipkinPort = '9411'; 
const zipkinPath = '/api/v2/spans'; 
const zipkinURL = ${zipkinUrl}:${zipkinPort}${zipkinPath};
    
const options = { 
    headers: { 
        'my-header': 'header-value', 
    }, 
    url: zipkinURL, 
    //serviceName: 'your-application-name',
    
    // optional interceptor
    getExportRequestHeaders: () => {
        return {
            'my-header': 'header-value',
        }
    }
    
} 
const traceExporter_zipkin = new ZipkinExporter(options);
    
////////*************End zipkin config */
    
const provider = new NodeTracerProvider({
    resource: new Resource({
        [SemanticResourceAttributes.SERVICE_NAME]: serviceName
    }),
});
    
//export to console
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
    
//export to zipkin
provider.addSpanProcessor(new SimpleSpanProcessor(traceExporter_zipkin));
    
    
provider.register();
registerInstrumentations({
    instrumentations: [
        // new ExpressInstrumentation({
        //     requestHook: (span, reqInfo) => {
        //         span.setAttribute('request-headers',JSON.stringify(reqInfo.req.headers))
        //     }
        // }),
        new HttpInstrumentation(),
        new ExpressInstrumentation()
    ]
});
const tracer = provider.getTracer(serviceName);
    return { tracer };
}
    
export default init;

//end of tracer.ts*********

****app.ts*********

import express from 'express'
import init from './tracer'; 

const { tracer } = init('app-services');
    
const app = express();
    
app.get('/', (req, res) =>{ res.send('Hello'); });
    
app.listen(3200, () => console.log('Server running'));

//end of app.ts*********

我已经根据 OpenTelemetry 文档更改了您的示例以进行调整:

tracer.ts

const opentelemetry = require('@opentelemetry/api');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { SimpleSpanProcessor, ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-base');
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin');

const { ExpressInstrumentation, ExpressRequestHookInformation } = require('@opentelemetry/instrumentation-express');
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');

module.exports = (serviceName) => {

  //Specify zipkin url. defualt url is http://localhost:9411/api/v2/spans
  const zipkinUrl = 'http://localhost';
  const zipkinPort = '9411';
  const zipkinPath = '/api/v2/spans';
  const zipkinURL = `${zipkinUrl}:${zipkinPort}${zipkinPath}`;

  const options = {
    headers: {
      'my-header': 'header-value',
    },
    url: zipkinURL,
    //serviceName: 'your-application-name',
    
   
    // optional interceptor
    getExportRequestHeaders: () => {
      return {
        'my-header': 'header-value',
      }
    }
  }
  const traceExporter_zipkin = new ZipkinExporter(options);

  const provider = new NodeTracerProvider({
    resource: new Resource({
      [SemanticResourceAttributes.SERVICE_NAME]: serviceName,
    }),
  });

  provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
  provider.addSpanProcessor(new SimpleSpanProcessor(traceExporter_zipkin));

  provider.register();

  registerInstrumentations({
    instrumentations: [
      // new ExpressInstrumentation({
      //     requestHook: (span, reqInfo) => {
      //         span.setAttribute('request-headers',JSON.stringify(reqInfo.req.headers))
      //     }
      // }),
      new ExpressInstrumentation(),
      new HttpInstrumentation()
    ],
  });

  return opentelemetry.trace.getTracer(serviceName);
};

以及跟踪器的调用方式:
app.ts

require('./tracer.ts')('app-services');

const express = require('express');

const app = express();

app.get('/', (req, res) =>{
    res.send('Hello');
});

app.listen(3200, () => console.log('Server running'));

我已经在本地测试过,我可以在控制台和 Zipkin 中看到痕迹。