1

I'm using google cloud trace inside GKE. Everything appears to work fine if I import the library as an argument when running node in the docker image, however if I import the client library in my code itself, I see no traces generated

As per the documentation, google cloud trace must be the first element imported in the application. To manage this in typescript with import statements, I have moved it to a dedicated file which I them import as the first module in my index.ts file. Here's what that looks like:

index.ts:

// Trace MUST be the first import before any external modules
import './app/services/trace';
import 'source-map-support/register';
import makeApp from './app/app';
// ...
makeApp().then(app => app.listen(port));

app/services/trace.ts:

import * as TraceAgent from '@google-cloud/trace-agent';

if (process.env.NODE_ENV === 'production') {
    TraceAgent.start({
        ignoreUrls: ['/livez', '/healthz', '/metrics'], // ignore internal only endpoints
        ignoreMethods: ['OPTIONS'],
        contextHeaderBehavior: 'ignore', // Ignore any incoming context headers. Stop outsiders forcing requests to be traced.
        samplingRate: 10,
        serviceContext: {
            service: 'my-service-name',
            version: process.env.VERSION || 'v0.0.0'
        }
    });
}

export default TraceAgent;

app/app.ts

import express from 'express';
// ...
export default async function makeApp() {
    const app = express();
    // ...
    return app
}

My typescript target is configured as es2022 with module set to commonjs. And the resulting compiled index.js looks like this

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// Trace MUST be the first import before any external modules
require("./app/services/trace");
require("source-map-support/register");
const app_1 = __importDefault(require("./app/app"));
//...

From what I understand, that means that @google-cloud/trace-agent is indeed being imported first, and while it does import fine and the start method is called, I still see no traces being generated when imported like this in my code.

Wazbat
  • 53
  • 6

0 Answers0