open-telemetry / opentelemetry-js

OpenTelemetry JavaScript Client
https://opentelemetry.io
Apache License 2.0
2.71k stars 789 forks source link

ESM+Typescript not able to auto-instrument some things? #3288

Closed djMax closed 2 years ago

djMax commented 2 years ago

What happened?

Steps to Reproduce

Clone https://github.com/gas-buddy/gasbuddy yarn yarn build:all yarn token-serv

Expected Result

Pino logs should include traces and spans.

Actual Result

Pino logs do not include traces and spans.

Additional Details

Included below

OpenTelemetry Setup Code

import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import * as opentelemetry from '@opentelemetry/sdk-node';

import type { DelayLoadServiceStartOptions } from '../types.js';

// For troubleshooting, set the log level to DiagLogLevel.DEBUG
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);

const sdk = new opentelemetry.NodeSDK({
  traceExporter: new opentelemetry.tracing.ConsoleSpanExporter(),
  instrumentations: [getNodeAutoInstrumentations()],
});

export default async function startWithTelemetry(options: DelayLoadServiceStartOptions) {
  await sdk.start();

  // We can't import things (express, pino, etc) before
  // telemetry has gotten its hooks in. So that's why
  // DelayLoadServiceStartOptions exists.
  const module = await import(options.service);
  const service = module.default;

  const { startApp, listen } = await import('../app.js');
  const app = await startApp({ ...options, service });
  app.locals.logger.info('OpenTelemetry enabled');

  await listen(app, async () => {
    await sdk.shutdown();
    app.locals.logger.info('OpenTelemetry shut down');
  });
}

package.json

{
  "name": "@gasbuddyrepo/service",
  "type": "module",
  "version": "1.0.0",
  "description": "An exploration of a modern GasBuddy service with Typescript support",
  "exports": "./build/index.js",
  "types": "./build/index.d.ts",
  "scripts": {
    "test": "jest",
    "lint": "eslint src",
    "build": "tsc -p tsconfig.json && yarn dlx chmodx build/bin/*",
    "watch": "tsc -p tsconfig.json -w --preserveWatchOutput",
    "clean": "npx rimraf ./build"
  },
  "author": "GasBuddy Developers <developers@gasbuddy.com>",
  "license": "UNLICENSED",
  "repository": {
    "type": "git",
    "url": "git@github.com:gas-buddy/gasbuddy.git"
  },
  "keywords": [
    "typescript",
    "gasbuddy"
  ],
  "engines": {
    "node": ">16"
  },
  "dependencies": {
    "@gasbuddyrepo/service": "^1.0.0",
    "@opentelemetry/api": "^1.2.0",
    "@opentelemetry/auto-instrumentations-node": "^0.32.1",
    "@opentelemetry/exporter-otlp-grpc": "^0.26.0",
    "@opentelemetry/sdk-node": "^0.33.0",
    "confit": "^3.0.0",
    "express": "^4.18.1",
    "express-openapi-validator": "^4.13.8",
    "pino": "^8.6.1",
    "read-pkg-up": "^9.1.0",
    "shortstop-dns": "^1.1.0",
    "shortstop-handlers": "^1.1.1",
    "shortstop-yaml": "^1.0.0"
  },
  "devDependencies": {
    "@types/confit": "^2.0.3",
    "@types/express": "^4.17.14",
    "@typescript-eslint/eslint-plugin": "^5.3.1",
    "@typescript-eslint/parser": "^5.3.1",
    "eslint": "^8.2.0",
    "eslint-config-airbnb-base": "^15.0.0",
    "eslint-config-airbnb-typescript": "^17.0.0",
    "eslint-plugin-import": "^2.25.3",
    "eslint-plugin-prettier": "^4.0.0",
    "prettier": "^2.5.1",
    "typescript": "^4.4.4"
  },
  "bin": {
    "start-service": "./build/bin/start-service.js"
  }
}

Relevant log output

@opentelemetry/api: Registered a global for diag v1.2.0.
Loading instrumentation for @opentelemetry/instrumentation-amqplib
Loading instrumentation for @opentelemetry/instrumentation-aws-lambda
No modules instrumentation has been defined, nothing will be patched
Loading instrumentation for @opentelemetry/instrumentation-aws-sdk
Loading instrumentation for @opentelemetry/instrumentation-bunyan
Loading instrumentation for @opentelemetry/instrumentation-cassandra-driver
Loading instrumentation for @opentelemetry/instrumentation-connect
Loading instrumentation for @opentelemetry/instrumentation-dns
Loading instrumentation for @opentelemetry/instrumentation-express
Loading instrumentation for @opentelemetry/instrumentation-fastify
Loading instrumentation for @opentelemetry/instrumentation-generic-pool
Loading instrumentation for @opentelemetry/instrumentation-graphql
Loading instrumentation for @opentelemetry/instrumentation-grpc
Loading instrumentation for @opentelemetry/instrumentation-hapi
Loading instrumentation for @opentelemetry/instrumentation-http
Loading instrumentation for @opentelemetry/instrumentation-ioredis
Loading instrumentation for @opentelemetry/instrumentation-knex
Loading instrumentation for @opentelemetry/instrumentation-koa
Loading instrumentation for @opentelemetry/instrumentation-lru-memoizer
Loading instrumentation for @opentelemetry/instrumentation-memcached
Loading instrumentation for @opentelemetry/instrumentation-mongodb
Loading instrumentation for @opentelemetry/instrumentation-mysql2
Loading instrumentation for @opentelemetry/instrumentation-mysql
Loading instrumentation for @opentelemetry/instrumentation-nestjs-core
Loading instrumentation for @opentelemetry/instrumentation-net
Loading instrumentation for @opentelemetry/instrumentation-pg
Loading instrumentation for @opentelemetry/instrumentation-pino
Loading instrumentation for @opentelemetry/instrumentation-redis
Loading instrumentation for @opentelemetry/instrumentation-redis-4
Loading instrumentation for @opentelemetry/instrumentation-restify
Loading instrumentation for @opentelemetry/instrumentation-winston
EnvDetector found resource. Resource { attributes: {} }
ProcessDetector found resource. Resource {
  attributes: {
    'process.pid': 52654,
    'process.executable.name': '/Users/memetral/.nvm/versions/node/v16.17.1/bin/node',
    'process.command': '/Users/memetral/dev/gb/gasbuddy/node_modules/.bin/start-service',
    'process.command_line': '/Users/memetral/.nvm/versions/node/v16.17.1/bin/node /Users/memetral/dev/gb/gasbuddy/node_modules/.bin/start-service',
    'process.runtime.version': '16.17.1',
    'process.runtime.name': 'nodejs',
    'process.runtime.description': 'Node.js'
  }
}
@opentelemetry/api: Registered a global for trace v1.2.0.
@opentelemetry/api: Registered a global for context v1.2.0.
@opentelemetry/api: Registered a global for propagation v1.2.0.
Applying patch for dns
patch lookup function
patch lookup function
Applying patch for net module
Patching pg.Client.prototype.query
@opentelemetry/instrumentation-http Applying patch for http@16.17.1
Applying patch for express@4.18.1
@opentelemetry/instrumentation-http Applying patch for https@16.17.1
{"level":30,"time":1664511439593,"pid":52654,"hostname":"MaxM1Max.local","msg":"OpenTelemetry enabled"}
{"level":30,"time":1664511439595,"pid":52654,"hostname":"MaxM1Max.local","port":8001,"service":"token-serv","msg":"express listening"}
Flarna commented 2 years ago

regarding ESM see #1946. In short ESM modules are not yet supported so any problems regarding ESM are not bugs.

I responded already on https://github.com/open-telemetry/opentelemetry-js/discussions/3285 which seems to cover the same topic.

Closing this one as duplicate.

djMax commented 2 years ago

That issue leads to a few stale PRs and some mention that perhaps there's a way to get the same benefit with esModuleInterop? Will resume convo on #3285 .