microsoft / ApplicationInsights-node.js

Microsoft Application Insights SDK for Node.js
MIT License
325 stars 140 forks source link

Auto correlation not working in apollo-gateway #949

Open fibsifan opened 2 years ago

fibsifan commented 2 years ago

We're trying to use request correlation in apollo gateway. We want to correlate federated graphql requests in order to identify slow subgraphs.

Here's what we did: We set up an apollo-gateway as described in the federation documentation. And before anything else is called, we configure the applicationinsights as described in the node.js documentation for application insights.

The whole thing looks a bit like this (authentication code is left out. we also set context tags for application insights and filter some requests from the tracing, left out for brevity):

const instrumentationKey = process.env.AI_INSTRUMENTATION_KEY;
appInsights
  .setup(instrumentationKey)
  .setAutoDependencyCorrelation(true)
  .setAutoCollectRequests(true)
  .setAutoCollectPerformance(false)
  .setAutoCollectExceptions(true)
  .setAutoCollectDependencies(true)
  .setAutoCollectConsole(false)
  .setUseDiskRetryCaching(true)
  .setDistributedTracingMode(DistributedTracingModes.AI_AND_W3C)
  .start();

const gateway = new ApolloGateway({
  supergraphSdl
});

const server = new ApolloServer({
  gateway
});

server
  .listen({ port: 3000 })
  .then(({ url }) => {
    console.log(`🚀 Gateway ready at ${url}`);
  });

Although we set AutoCollectRequests and AutoCollectDependencies and autoDependencyCorrelation to true and we're using W3C correlation, there is no correlation between the incoming requests to the gateway and incoming requests on the subgraph(s). The subgraphs get correlation headers on their incoming requests, but the traceId differs from the traceId that's reported by the apollo-gateway.

This issue becomes most visible, when looking at the azure application map. No requests between the apollo-gateway and the subgraphs can be observed, instead the subgraphs appear as external dependencies on the map.

When debugging through the applicationinsights code I can observe, that the error exists on the apollo gateway. The traceIds for the incoming and the outgoing request differ in AutoCollectHttpRequests.trackRequest in HttpRequest.ts (for the incoming request) and in AutoCollectHttpDependencies.trackRequest in HttpDependencies.ts (for the outgoing request). Consequently, the headers for the outgoing request (which the subgraph receives) contain an unrelated traceId and correlation is not performed in Azure monitor.

I have tested with DistributedTracingModes.AI and wasn't able to get the correlation to work either, although I didn't analyze the error through debugging in this case. I'm assuming the behaviour is very similar.

Is this a bug in ApplicationInsights-node.js or are we missing something in the configuration of the apollo gateway?

fibsifan commented 2 years ago

seems related to #566