Add query time tracking to IO Events.

This commit is contained in:
Patrick Fic
2021-04-22 10:16:49 -07:00
parent cdf99a16fe
commit 08faff4d8e
11 changed files with 112 additions and 3 deletions

View File

@@ -1,11 +1,18 @@
import { ApolloClient, ApolloLink, InMemoryCache, split } from "@apollo/client";
import {
ApolloClient,
ApolloLink,
InMemoryCache,
operationName,
split,
} from "@apollo/client";
import { setContext } from "@apollo/client/link/context";
import { HttpLink } from "@apollo/client/link/http"; //"apollo-link-http";
import { RetryLink } from "@apollo/client/link/retry";
import { WebSocketLink } from "@apollo/client/link/ws";
import { getMainDefinition } from "@apollo/client/utilities";
//import { split } from "apollo-link";
import apolloLogger from "apollo-link-logger";
import { getMainDefinition } from "@apollo/client/utilities";
import axios from "axios";
import { auth } from "../firebase/firebase.utils";
import errorLink from "../graphql/apollo-error-handling";
@@ -31,6 +38,25 @@ const wsLink = new WebSocketLink({
},
});
const roundTripLink = new ApolloLink((operation, forward) => {
// Called before operation is sent to server
operation.setContext({ start: new Date() });
return forward(operation).map((data) => {
// Called after server responds
const time = new Date() - operation.getContext().start;
console.log(
`Operation ${operation.operationName} took ${time} to complete`
);
TrackExecutionTime(operation.operationName, time);
return data;
});
});
const TrackExecutionTime = async (operationName, time) => {
await axios.post("/ioevent", { operationName, time, dbevent: true });
};
const subscriptionMiddleware = {
applyMiddleware: async (options, next) => {
options.authToken =
@@ -96,7 +122,11 @@ if (process.env.NODE_ENV === "development") {
middlewares.push(apolloLogger);
}
middlewares.push(retryLink.concat(errorLink.concat(authLink.concat(link))));
middlewares.push(
roundTripLink.concat(
retryLink.concat(errorLink.concat(authLink.concat(link)))
)
);
const cache = new InMemoryCache({});