MCPcopy Create free account
hub / github.com/hashintel/hash / statsPlugin

Function statsPlugin

apps/hash-api/src/graphql/create-apollo-server.ts:35–122  ·  view source on GitHub ↗
({
  statsd,
}: {
  statsd?: StatsD;
})

Source from the content-addressed store, hash-verified

33import type { Server } from "node:http";
34
35const statsPlugin = ({
36 statsd,
37}: {
38 statsd?: StatsD;
39}): ApolloServerPlugin<GraphQLContext> => ({
40 requestDidStart: async () => {
41 const startTimestamp = performance.now();
42
43 return {
44 didResolveOperation: async (didResolveOperationCtx) => {
45 const operationName = didResolveOperationCtx.operationName;
46 if (operationName) {
47 statsd?.increment(operationName, ["graphql"]);
48 }
49 },
50
51 executionDidStart: async ({ request }) => {
52 const scope = Sentry.getCurrentScope();
53
54 scope.setContext("graphql", {
55 query: request.query,
56 variables: request.variables,
57 });
58 },
59
60 didEncounterErrors: async (ctx) => {
61 for (const err of ctx.errors) {
62 // Don't send ForbiddenErrors to Sentry – we can add more here as needed
63 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- this may be undefined
64 if (err.extensions?.code === "FORBIDDEN") {
65 continue;
66 }
67
68 if (err.path) {
69 Sentry.getCurrentScope().addBreadcrumb({
70 category: "query-path",
71 message: err.path.join(" > "),
72 });
73 }
74
75 Sentry.captureException(err);
76 }
77 },
78
79 willSendResponse: async (ctx) => {
80 if (ctx.operationName === "IntrospectionQuery") {
81 // Ignore introspection queries from graphiql
82 return;
83 }
84 const elapsed = performance.now() - startTimestamp;
85
86 // take the first part of the UA to help identify browser vs server requests
87 const userAgent = ctx.request.http?.headers
88 .get("user-agent")
89 ?.split(" ")[0];
90
91 const msg = {
92 operation: ctx.operationName,

Callers 1

createApolloServerFunction · 0.85

Calls 7

nowMethod · 0.80
splitMethod · 0.80
infoMethod · 0.80
getMethod · 0.65
incrementMethod · 0.45
joinMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected