node-sdk-configuration.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
  2. import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc';
  3. import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
  4. import { Resource, type IResource } from '@opentelemetry/resources';
  5. import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
  6. import type { NodeSDKConfiguration } from '@opentelemetry/sdk-node';
  7. import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION, SEMRESATTRS_SERVICE_INSTANCE_ID } from '@opentelemetry/semantic-conventions';
  8. import { getGrowiVersion } from '~/utils/growi-version';
  9. type Configuration = Partial<NodeSDKConfiguration> & {
  10. resource: IResource;
  11. };
  12. let resource: Resource;
  13. let configuration: Configuration;
  14. export const generateNodeSDKConfiguration = (serviceInstanceId?: string): Configuration => {
  15. if (configuration == null) {
  16. const version = getGrowiVersion();
  17. resource = new Resource({
  18. [ATTR_SERVICE_NAME]: 'growi',
  19. [ATTR_SERVICE_VERSION]: version,
  20. });
  21. configuration = {
  22. resource,
  23. traceExporter: new OTLPTraceExporter(),
  24. metricReader: new PeriodicExportingMetricReader({
  25. exporter: new OTLPMetricExporter(),
  26. }),
  27. instrumentations: [getNodeAutoInstrumentations({
  28. '@opentelemetry/instrumentation-bunyan': {
  29. enabled: false,
  30. },
  31. // disable fs instrumentation since this generates very large amount of traces
  32. // see: https://opentelemetry.io/docs/languages/js/libraries/#registration
  33. '@opentelemetry/instrumentation-fs': {
  34. enabled: false,
  35. },
  36. })],
  37. };
  38. }
  39. if (serviceInstanceId != null) {
  40. configuration.resource = resource.merge(new Resource({
  41. [SEMRESATTRS_SERVICE_INSTANCE_ID]: serviceInstanceId,
  42. }));
  43. }
  44. return configuration;
  45. };
  46. // public async shutdownInstrumentation(): Promise<void> {
  47. // await this.sdkInstance.shutdown();
  48. // // メモ: 以下の restart コードは動かない
  49. // // span/metrics ともに何も出なくなる
  50. // // そもそも、restart するような使い方が出来なさそう?
  51. // // see: https://github.com/open-telemetry/opentelemetry-specification/issues/27/
  52. // // const sdk = new NodeSDK({...});
  53. // // sdk.start();
  54. // // await sdk.shutdown().catch(console.error);
  55. // // const newSdk = new NodeSDK({...});
  56. // // newSdk.start();
  57. // }