簡體   English   中英

Opentelemetry- Typescript 項目(Zipkin 出口商)

[英]Opentelemetry- Typescript Project (Zipkin exporter)

我有一個簡單的 hello-world typescript 項目,我正在嘗試在其上設置 opentelemetry。 我想將跟蹤發送到控制台和 Zipkin。

我運行了該應用程序,但在控制台和 Zipkin 上都沒有得到任何跟蹤器。 當我導出 init function(在此函數中設置跟蹤器)並將其導入 app.ts 文件時,我無法准確指出問題所在,但由於某些原因我無法獲取跟蹤器。 運行應用程序時沒有錯誤。

這是一個包含兩個文件的簡單項目:tracer.ts 和 app.ts

示蹤劑.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 文檔更改了您的示例以進行調整:

示蹤劑.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);
};

以及跟蹤器的調用方式:
應用程序.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 中看到痕跡。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM