@mwcp/otel
TypeScript icon, indicating that this package has built-in type declarations

26.5.1 • Public • Published

@mwcp/otel

Open Telemetry Component for Midway.js

GitHub tag Version License ci codecov Conventional Commits lerna

Note

ESM build only, requires @midwayjs >= 3.12 and set "type": "module" in packages.json

Install

npm i @mwcp/otel

Configuration

Update project src/configuration.ts

import { Configuration } from '@midwayjs/decorator'
import * as koa from '@midwayjs/koa'
import * as otel from '@mwcp/otel'

@Configuration({
  imports: [
    koa,
    otel,
  ],
  importConfigs: [join(__dirname, 'config')],
})
export class ContainerConfiguration implements ILifeCycle {
}

Usage

To try out the OTLPTraceExporter quickly, you can run Jaeger in a docker container:

docker run -d --name jaeger \
  -e COLLECTOR_OTLP_ENABLED=true \
  -p 4317:4317 \
  -p 4318:4318 \
  -p 5778:5778 \
  -p 6831:6831/udp \
  -p 6832:6832/udp \
  -p 16686:16686 \
  jaegertracing/all-in-one:latest

Start project:

export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
npm start

Jaeger Web UI address:

http://localhost:16686/

Trace Decorator

import { Trace } from '@mwcp/otel'

@Controller('/')
export class FooController {

  @Inject() readonly svc: FooService

  /* span name will be `{class name}/{method name}` => "FooController/hello" */
  @Trace()
  async hello(): Promise<string> {
    return 'hello'
  }

  /* span name will be "hello" */
  @Trace('hello')
  async world(): Promise<string> {
    return 'world'
  }

  @Trace({
    spanName: 'hello'
  })
  async world2(): Promise<string> {
    return 'world'
  }
}

TraceInit Decorator

// src/configuration.ts
import { TraceInit } from '@mwcp/otel'

export class AutoConfiguration implements ILifeCycle {
  @TraceInit({ namespace: 'Foo' })
  async onReady(container: IMidwayContainer): Promise<void> {
    // some code
  }
}

Decorator Generics

Auto parameter type of keyGenerator from generics

@Controller('/')
export class FooController {

  @Inject() readonly svc: FooService

  hello(): string {
    // spanName should be 'foo-124-abc'
    const msg = this.svc.concat(123, 'abc')
    return msg
  }
}

@Provide()
export class FooService {
  @Trace<FooService['concat']>({
    spanName: ([v1, v2]) => `foo-${v1 + 1}-${v2}`,
  })
  concat(v1: number, v2: string): string {
    return `${v1.toString()}-${v2}`
  }

  @Trace<FooService['concat2']>({
    spanName: (args) => `foo-${args[0] + 1}-${args[1]}`,
  })
  concat2(v1: number, v2: string): string {
    return `${v1.toString()}-${v2}`
  }
}

License

MIT

Languages


Package Sidebar

Install

npm i @mwcp/otel

Weekly Downloads

729

Version

26.5.1

License

MIT

Unpacked Size

367 kB

Total Files

181

Last publish

Collaborators

  • waiting