graphql-hive / gateway

GraphQL gateway that can act as a Federation Gateway or a Proxy Gateway for any GraphQL service.
https://the-guild.dev/graphql/hive/docs/gateway
MIT License
13 stars 2 forks source link

OpenTelemetry plugin is logging stringified object #169

Open klippx opened 7 hours ago

klippx commented 7 hours ago

Sample log:

{
  "level": "error",
  "logger": "OpenTelemetry",
  "message": "{\"message\":\"Request timed out\",\"originalLine\":\"59\",\"originalColumn\":\"25\",\"line\":\"87\",\"column\":\"24\",\"sourceURL\":\"/app/node_modules/@opentelemetry/otlp-exporter-base/build/src/platform/node/http-transport-utils.js\",\"stack\":\"Error: Request timed out\\n    at <anonymous> (/app/node_modules/@opentelemetry/otlp-exporter-base/build/src/platform/node/http-transport-utils.js:87:24)\\n    at emit (node:events:70:22)\\n    at emitCloseNT (node:http:718:33)\\n    at <anonymous> (node:http:1012:70)\\n    at <anonymous> (native:19:28)\\n    at processTicksAndRejections (native:7:39)\",\"name\":\"Error\"}",
  "timestamp": "2024-11-22T14:15:18.780Z"
}

My setup:

const log = winston.createLogger({ ... })

// Need to patch `child` since graphql-mesh uses a non-standard `.child(str: string)` to create log children
const logging = new Proxy(log, {
  get(target, prop, receiver) {
    if (prop === 'child') {
      return (firstArg: string | object, ...args: any[]) => {
        if (typeof firstArg === 'string') {
          return target.child({ logger: firstArg, ...args });
        } else {
          return Reflect.apply(Reflect.get(target, prop, receiver), target, [
            firstArg,
            ...args,
          ]);
        }
      };
    }
    return Reflect.get(target, prop, receiver);
  },
});

export const gatewayConfig = defineConfig({
  logging,
  plugins: (ctx) => [
    useOpenTelemetry({
      ...ctx,
      inheritContext: true, // Optional, whether to inherit the context from the incoming request
      propagateContext: true, // Optional, whether to propagate the context to the outgoing requests
      exporters: [
        createOtelExporter({
          url: `http://${config.OTEL_COLLECTOR_HOST}:${otelPort}`,
        }),
      ],
    }),
  ],
});

and starting with bun hive-gateway supergraph

I can "fix" it by applying a patch commit to the @graphql-mesh/plugin-opentelemetry:

Screenshot 2024-11-22 at 3 13 54 PM

But these patch commits are very tedious to maintain.