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

Access the correlation context information to send operation ID in response #1154

Open
Apokalypt opened this issue May 29, 2023 · 4 comments

Comments

@Apokalypt
Copy link

Context

My team and I are trying to improve the way we work with logs so that we can be more efficient when we encounter bugs in our APIs. To do this, we'd like to expose the operation ID and the ID of the parent operation in the body of the response we send.

Feature request

In order to do this, we need to be able to retrieve this information. We thought of a simple solution: expose the correlation context in the request object ( request.__context ) so that we can retrieve the operation ID / parent ID and expose it in the API response in the event of an error.

If it's already possible to do this, we'd love to hear about it. If not, do you think it would be feasible and interesting?

@hectorhdzg
Copy link
Member

@Apokalypt telemetry processors should have the request object and also correlation context available for you to consume, more info here.

@Apokalypt
Copy link
Author

@Apokalypt telemetry processors should have the request object and also correlation context available for you to consume, more info here.

I'm not sure to understand how to proceed. We've already used these methods to filter some data, but I can't see how this would allow us to access the correlation ID in our error handler (using express) and return it in the API response.

@hectorhdzg
Copy link
Member

Well depends on when do you need the information, we should also be adding headers with correlation context information, have you tried reading the response headers?

@Apokalypt
Copy link
Author

Well depends on when do you need the information, we should also be adding headers with correlation context information, have you tried reading the response headers?

To be more explicit, here is a small code that show what we would like to do :

const appInsights = require("applicationinsights");
appInsights.setup(process.env.CONNECTION_STRING).start();
/* some customization to auto collect request, console, ... */

const express = require('express');
const app = express();

/* some routes and middleware */

// Handle errors from routes and middleware
app.use( (error, req, res, _next) => {
    const err = ResponseError.fromError(error);

    const response = err.toBaseResponseBody();
    const context = extractContext(req);
    if (context) {
        response.context = {
            id: context.id,
            parent: context.parent
        };
    }

    res.status(err.status).json(response);
});

const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Server listening on port ${port}`) );

function extractContext(req) {
    // Try to extract context generated by app insights to retrieve correlation id and parent correlation id
    // in order to facilitate bug tracking/analysis
}

We try to take a look at response header but, as expected, there is nothing in it. I hope this code could help you to understand what we want to do.
The first thing we thought of was to modify the library code to expose the context information in the request object, but if there's something already implemented, let us know.

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

No branches or pull requests

2 participants