Facebook
From x, 2 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 153
  1. import { Controller, Get } from '@nestjs/common';
  2. import { AppService } from './app.service';
  3. import {
  4.   trace,
  5.   Span,
  6.   propagation,
  7.   context,
  8.   createContextKey,
  9. } from '@opentelemetry/api';
  10.  
  11. @Controller()
  12. export class AppController {
  13.   constructor(private readonly appService: AppService) {}
  14.  
  15.   @Get()
  16.   async getHello() {
  17.     const newTracer = trace.getTracer('user-serive');
  18.  
  19.     const ctx = context.active();
  20.     const ctxKey = createContextKey('First ctx key');
  21.     ctx.setValue(ctxKey, 'ctx1');
  22.     const output = {};
  23.     propagation.inject(ctx, output);
  24.     console.log(output, ctx);
  25.  
  26.     const span1 = newTracer.startSpan('start1', {}, ctx);
  27.     const spanCtx = span1.spanContext();
  28.     await this.appService.getHello(spanCtx);
  29.     span1.end();
  30.  
  31.     // let activeContext = propagation.extract(context.active(), {});
  32.     // activeContext.getValue();
  33.     const span2 = newTracer.startSpan('start2');
  34.     this.appService.getHello2();
  35.     span2.end();
  36.   }
  37. }