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

41.0.0 • 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.16 and set "type": "module" in packages.json

Support Schemes

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'
  }
}

Pass scope to avoid the confusion of call chain relationship when async methods are called concurrently

import { Trace } from '@mwcp/otel'

@Controller('/')
export class FooController {
  @Trace()
  async hello(): Promise<string> {
    await Promise.all([
      this._simple1(),
      this._simple2(),
    ])
    return 'OK'
  }

  @Trace({ scope: 'hello1' })
  async _hello1(): Promise<string> {
    return 'world'
  }

  @Trace({ scope: 'hello2' })
  async _hello2(): Promise<string> {
    return 'world'
  }

  @Trace({ scope: 'hello1' })
  async _hello1a(): Promise<string> {
    return 'world'
  }

  @Trace({ scope: 'hello2' })
  async _hello2a(): Promise<string> {
    return 'world'
  }
}

Trace Info

Use this inner before() after() point to the decorated instance

export class FooService {
  foo = 1

  @Trace<Foo['home']>({
    before([options], decoratorContext) {
      assert(this instanceof FooService) // <--- this point to FooService 
      assert(this === decoratorContext.instance)
      assert(this.foo === 1)

      return void 0
    },
    after([options], res, decoratorContext) {
      assert(this instanceof FooService)
      assert(this === decoratorContext.instance)
      assert(this.foo === 1)

      return void 0
    },
  })
  async home(this: FooService, options: InputOptions): Promise<string> { // <--- pass this type explicitly
    const ret = await options.input
    return ret
  }
}

TraceLog Decorator

Add trace attribute to the span through decorator before()/after() method return object, no new span starting

  • add trace tag/log to current active span
  • add trace tag/log to root span

Note return value of decorated method before() and after() should be type:

interface DecoratorTraceData {
  attrs?: Attributes
  events?: Attributes
  rootAttrs?: Attributes
  rootEvents?: Attributes
}
import { TraceLog, DecoratorTraceData } from '@mwcp/otel'

@Controller('/')
export class FooController {

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

  @TraceLog({
    before: async ([input], { instanceName, methodName }) => {
      const attrs: Attributes = {
        args0: input,
      }
      const events: Attributes = {
        ...attrs,
        instanceName,
        methodName,
      }
      const rootAttrs: Attributes = { rootAttrs: 'rootAttrs' }
      const rootEvents: Attributes = { ...rootAttrs }

      return { attrs, events, rootAttrs, rootEvents } as DecoratorTraceData
    },
    after: ([input], res, { instanceName, methodName }) => {
      const attrs: Attributes = {
        args0: input,
        res,
      }
      const events: Attributes = {
        ...attrs,
        instanceName,
        methodName,
      }
      return { events }
    },
  })
  async world(): 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


Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
41.0.092latest

Version History

VersionDownloads (Last 7 Days)Published
41.0.092
40.1.141
40.1.03
40.0.03
39.3.05
39.2.03
39.1.21
39.1.11
39.1.01
39.0.22
39.0.11
39.0.01
38.4.03
38.3.01
38.2.32
38.2.20
38.2.111
38.2.00
38.1.00
38.0.00
37.4.10
37.4.00
37.3.00
37.2.40
37.2.30
37.2.20
37.2.10
37.2.058
37.1.00
37.0.00
36.1.30
36.1.20
36.1.10
36.1.00
36.0.00
35.2.10
35.2.00
35.1.00
35.0.10
35.0.00
34.0.10
34.0.00
33.0.00
32.0.00
31.0.00
30.21.05
30.20.00
30.19.00
30.18.00
30.17.10
30.17.00
30.16.10
30.16.00
30.15.20
30.15.10
30.15.00
30.14.061
30.13.00
30.12.10
30.12.00
30.11.20
30.11.10
30.11.00
30.10.20
30.10.10
30.10.00
30.9.10
30.9.00
30.8.00
30.7.20
30.7.10
30.7.00
30.6.10
30.6.00
30.5.00
30.4.00
30.3.00
30.2.00
30.1.10
30.1.00
30.0.01
29.3.11
29.3.01
29.2.01
29.1.01
29.0.11
29.0.01
28.2.00
28.1.10
28.1.01
28.0.01
27.0.11
27.0.01
26.5.31
26.5.21
26.5.11
26.5.00
26.4.01
26.3.21
26.3.11
26.3.00
26.2.00
26.1.00
26.0.21
26.0.01
25.2.31
25.2.21
25.2.10
25.2.00
25.1.01
25.0.21
25.0.11
25.0.00
24.2.41
24.2.31
24.2.20
24.2.11
24.2.00
24.1.00
24.0.01
23.1.01
23.0.01
22.1.21
22.1.00
22.0.11
22.0.01
21.0.02
20.12.01
20.11.00
20.10.11
20.10.01
20.9.01
20.8.00
20.7.00
20.6.00
20.5.01
20.3.00
20.0.01
19.2.11
19.2.01
19.1.10
19.1.01
19.0.31
19.0.02
15.0.00
10.13.51
10.13.41
10.13.31
10.13.21
10.13.10
10.13.00
10.12.00
10.11.01
10.10.20
10.10.11
10.10.01
10.9.41
10.9.31
10.9.22
10.9.10
10.9.01
10.8.01
10.7.01
10.6.11
10.6.01
10.5.00
10.3.00
10.2.01
10.1.01
10.0.20
10.0.10
10.0.01
9.0.11
9.0.01
8.0.11
8.0.01
7.4.01
7.3.00
7.2.01
7.1.21
7.1.11
7.1.01
7.0.31
7.0.21
7.0.10
7.0.01
6.0.00
5.2.10
5.2.01
5.1.01
5.0.01
4.1.31
4.1.21
4.1.11
4.1.00
4.0.70
4.0.61
4.0.51
4.0.41
4.0.31
4.0.21
4.0.11
4.0.01
3.17.11
3.17.01
3.16.71
3.16.61
3.16.50
3.16.41
3.16.30
3.16.21
3.16.11
3.16.01
3.15.01
3.14.01
3.13.01
3.12.02
3.11.20
3.11.11
3.11.01
3.10.11
3.10.01
3.9.01
3.8.80
3.8.70
3.8.60
3.8.51
3.8.41
3.8.31
3.8.21
3.8.10
3.8.01
3.7.00
3.6.01
3.5.00
3.4.11
3.4.00
3.3.01
3.2.51
3.2.41
3.2.30
3.2.21
3.2.10
3.2.00
3.1.00
3.0.01
2.1.10
2.0.01
1.3.00
1.2.02
1.1.10
1.1.01
1.0.21
1.0.11
1.0.00

Package Sidebar

Install

npm i @mwcp/otel

Weekly Downloads

422

Version

41.0.0

License

MIT

Unpacked Size

544 kB

Total Files

218

Last publish

Collaborators

  • waiting