Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting TypeError: Converting circular structure to JSON on express app #51

Open
ahsannaseem opened this issue Jul 30, 2020 · 1 comment

Comments

@ahsannaseem
Copy link

Hi, so we are trying to use prometheus-api-metrics following as mentioned in official doc. But getting circular structure to json error.

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Socket'
    |     property 'parser' -> object with constructor 'HTTPParser'
    --- property 'socket' closes the circle
    at JSON.stringify (<anonymous>)
    at /Users/ahsannaseem/coding/project-repo/node_modules/prometheus-api-metrics/src/metrics-middleware.js:14:61

code in application.ts

require("dotenv-extended").load();
process.env.NODE_ENV !== "development" ? require("newrelic") : null;
import * as loggerConfig from "./config/logger-config";
loggerConfig.initialize();
import * as bodyParser from "body-parser";
import { registerConfigsAndLoad, container, createDatabasePools } from "./config/inversify.config";
import { loadAndSetScheduleForPeriodicData } from "./services/periodic-loaded-config";
import { Server } from "net";
//declare prometheus-api-metrics
import * as apiMetrics from "prometheus-api-metrics";
let isGlobalsInitilizingStarted: boolean = false;
let isGlobalsInitilized: boolean = false;

export async function initGlobals() {
    if (isGlobalsInitilizingStarted)
        throw new Error("Initilizing is already started.");
    isGlobalsInitilizingStarted = true;
    console.log("Initilized global variables.");
    await require("./config/container-config").configure();
    await registerConfigsAndLoad();
    createDatabasePools();
    // some init can be done in parallel
    await Promise.all([
        require("./config/redis-config").configure(),
        loadAndSetScheduleForPeriodicData()
    ]);

    isGlobalsInitilized = true;
}

export async function startSqsEventListener() {
    if (!isGlobalsInitilized) {
        throw new Error("Globals are noit initilized. Run initGlobals() first.");
    }
    await require("./config/sqs-config").listen();
}

export async function startWebServer(): Promise<Server> {
    if (!isGlobalsInitilized) {
        throw new Error("Globals are noit initilized. Run initGlobals() first.");
    }

    // start http server
    const app = require("express")();
    app.use(bodyParser.json());
// consume api-metrics
    app.use(apiMetrics);
    const routeConfig = require("./config/route-config");
    routeConfig.configure(app);
    const port = process.env.PORT || 3000;
    return app.listen(port, function () {
        console.log("service listening on port", port);
    });
}

@kobik
Copy link
Collaborator

kobik commented Oct 18, 2020

Hi @ahsannaseem, when do you get this error? on incoming request?

have you called init first?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants