instrumentation.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';
  2. import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
  3. import { Instrumentation } from '@opentelemetry/instrumentation';
  4. import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
  5. import { GraphQLInstrumentation } from '@opentelemetry/instrumentation-graphql';
  6. import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
  7. import { IORedisInstrumentation } from '@opentelemetry/instrumentation-ioredis';
  8. import { NestInstrumentation } from '@opentelemetry/instrumentation-nestjs-core';
  9. import { PgInstrumentation } from '@opentelemetry/instrumentation-pg';
  10. import { resourceFromAttributes } from '@opentelemetry/resources';
  11. import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
  12. import { NodeSDK } from '@opentelemetry/sdk-node';
  13. import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-node';
  14. import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';
  15. const traceExporter = new OTLPTraceExporter({
  16. url: 'http://localhost:4318/v1/traces',
  17. });
  18. const metricExporter = new OTLPMetricExporter({
  19. url: 'http://localhost:4318/v1/metrics',
  20. });
  21. const instrumentations: Instrumentation[] = [];
  22. const httpInstrumentation = new HttpInstrumentation();
  23. const expressInstrumentation = new ExpressInstrumentation();
  24. const graphqlInstrumentation = new GraphQLInstrumentation({
  25. mergeItems: true,
  26. });
  27. instrumentations.push(new NestInstrumentation());
  28. instrumentations.push(...[httpInstrumentation, expressInstrumentation, graphqlInstrumentation]);
  29. instrumentations.push(new PgInstrumentation());
  30. instrumentations.push(new IORedisInstrumentation());
  31. const sdk = new NodeSDK({
  32. spanProcessors: [new SimpleSpanProcessor(traceExporter)],
  33. metricReader: new PeriodicExportingMetricReader({
  34. exporter: metricExporter,
  35. }),
  36. resource: resourceFromAttributes({
  37. [ATTR_SERVICE_NAME]: 'vendure-local-dev-server',
  38. [ATTR_SERVICE_VERSION]: '1.0',
  39. }),
  40. instrumentations,
  41. });
  42. sdk.start();