-
Notifications
You must be signed in to change notification settings - Fork 2k
/
contextualizedStats.ts
231 lines (209 loc) 路 7.56 KB
/
contextualizedStats.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { DurationHistogram } from './durationHistogram';
import {
IStatsContext,
Trace,
TypeStat,
QueryLatencyStats,
IPathErrorStats,
} from 'apollo-engine-reporting-protobuf';
export class ContextualizedStats {
statsContext: IStatsContext;
queryLatencyStats: QueryLatencyStats;
perTypeStat: { [k: string]: TypeStat };
constructor(statsContext: IStatsContext) {
this.statsContext = statsContext;
this.queryLatencyStats = new QueryLatencyStats({
latencyCount: new DurationHistogram(),
requestCount: 0,
cacheHits: 0,
persistedQueryHits: 0,
persistedQueryMisses: 0,
cacheLatencyCount: new DurationHistogram(),
rootErrorStats: Object.create(null),
requestsWithErrorsCount: 0,
publicCacheTtlCount: new DurationHistogram(),
privateCacheTtlCount: new DurationHistogram(),
registeredOperationCount: 0,
forbiddenOperationCount: 0,
});
this.perTypeStat = Object.create(null);
}
public addTrace(trace: Trace) {
const queryLatencyStats = this.queryLatencyStats;
queryLatencyStats.requestCount++;
if (trace.fullQueryCacheHit) {
((queryLatencyStats.cacheLatencyCount as unknown) as DurationHistogram).incrementDuration(
trace.durationNs,
);
queryLatencyStats.cacheHits++;
} else {
((queryLatencyStats.latencyCount as unknown) as DurationHistogram).incrementDuration(
trace.durationNs,
);
}
if (!trace.fullQueryCacheHit && trace.cachePolicy && trace.cachePolicy.maxAgeNs) {
if (trace.cachePolicy.scope == Trace.CachePolicy.Scope.PRIVATE) {
((queryLatencyStats.privateCacheTtlCount as unknown) as DurationHistogram).incrementDuration(
trace.cachePolicy.maxAgeNs
);
} else if (trace.cachePolicy.scope == Trace.CachePolicy.Scope.PUBLIC) {
((queryLatencyStats.publicCacheTtlCount as unknown) as DurationHistogram).incrementDuration(
trace.cachePolicy.maxAgeNs
);
}
}
if (trace.persistedQueryHit) {
queryLatencyStats.persistedQueryHits++;
} else if (trace.persistedQueryRegister) {
queryLatencyStats.persistedQueryMisses++;
}
if (trace.forbiddenOperation) {
queryLatencyStats.forbiddenOperationCount++;
} else if (trace.registeredOperation) {
queryLatencyStats.registeredOperationCount++;
}
let hasError = false;
const typeStats = this.perTypeStat;
const rootPathErrorStats = queryLatencyStats.rootErrorStats as IPathErrorStats;
function traceNodeStats(node: Trace.INode, path: ReadonlyArray<string>) {
// Generate error stats and error path information
if (node.error && node.error.length > 0) {
hasError = true;
let currPathErrorStats: IPathErrorStats = rootPathErrorStats;
for (const subPath of path.values()) {
let children = currPathErrorStats.children;
if (!children) {
children = Object.create(null);
currPathErrorStats.children = children;
}
// Children cannot be null or undefined be null or undefined
let nextPathErrorStats = (children as {
[k: string]: IPathErrorStats;
})[subPath];
if (!nextPathErrorStats) {
nextPathErrorStats = Object.create(null);
(
children as { [k: string]: IPathErrorStats }
)[subPath] = nextPathErrorStats;
}
// nextPathErrorStats cannot be null or undefined
currPathErrorStats = nextPathErrorStats as IPathErrorStats;
}
currPathErrorStats.requestsWithErrorsCount =
(currPathErrorStats.requestsWithErrorsCount || 0) + 1;
currPathErrorStats.errorsCount =
(currPathErrorStats.errorsCount || 0) + node.error.length;
}
if (
node.parentType &&
node.originalFieldName &&
node.type &&
node.endTime &&
node.startTime
) {
let typeStat = typeStats[node.parentType];
if (!typeStat) {
typeStat = new TypeStat();
typeStats[node.parentType] = typeStat;
}
let fieldStat = typeStat.perFieldStat[node.originalFieldName];
const duration = node.endTime - node.startTime;
if (!fieldStat) {
const durationHistogram = new DurationHistogram();
durationHistogram.incrementDuration(duration);
fieldStat = {
returnType: node.type,
errorsCount: (node.error && node.error.length) || 0,
count: 1,
requestsWithErrorsCount:
node.error && node.error.length > 0 ? 1 : 0,
latencyCount: durationHistogram,
};
typeStat.perFieldStat[node.originalFieldName] = fieldStat;
} else {
// We only create the object in the above line so we can know they aren't null
(fieldStat.errorsCount as number) =
(node.error && node.error.length) || 0;
(fieldStat.count as number)++;
// Note: this is actually counting the number of resolver calls for this
// field that had at least one error, not the number of overall GraphQL
// queries that had at least one error for this field. That doesn't seem
// to match the name, but it does match the Go engineproxy implementation.
// (Well, actually the Go engineproxy implementation is even buggier because
// it counts errors multiple times if multiple resolvers have the same path.)
(fieldStat.requestsWithErrorsCount as number) +=
node.error && node.error.length > 0 ? 1 : 0;
((fieldStat.latencyCount as unknown) as DurationHistogram).incrementDuration(
duration,
);
}
}
}
iterateOverTraceForStats(trace, traceNodeStats);
if (hasError) {
queryLatencyStats.requestsWithErrorsCount++;
}
}
}
/**
* Iterates over the entire trace and add the error to the errorPathStats object if there are errors
* Also returns true if there are any errors found so we can increment errorsCount
* @param trace Trace wer are iterating over
* @param f function to be run on every node of the trace
*/
function iterateOverTraceForStats(
trace: Trace,
f: (node: Trace.INode, path: ReadonlyArray<string>) => void,
): void {
if (trace.root) {
iterateOverTraceNode(trace.root, [], f);
}
if (trace.queryPlan) {
iterateOverQueryPlan(trace.queryPlan, f);
}
}
function iterateOverQueryPlan(
node: Trace.IQueryPlanNode | null | undefined,
f: (node: Trace.INode, path: ReadonlyArray<string>) => void,
): void {
if (!node) return;
if (
node.fetch &&
node.fetch.trace &&
node.fetch.trace.root &&
node.fetch.serviceName
) {
iterateOverTraceNode(
node.fetch.trace.root,
[`service:${node.fetch.serviceName}`],
f,
);
} else if (node.flatten) {
iterateOverQueryPlan(node.flatten.node, f);
} else if (node.parallel && node.parallel.nodes) {
node.parallel.nodes.map(node => {
iterateOverQueryPlan(node, f);
});
} else if (node.sequence && node.sequence.nodes) {
node.sequence.nodes.map(node => {
iterateOverQueryPlan(node, f);
});
}
}
function iterateOverTraceNode(
node: Trace.INode,
path: ReadonlyArray<string>,
f: (node: Trace.INode, path: ReadonlyArray<string>) => void,
) {
if (node.child) {
for (const child of node.child) {
let childPath = path;
if (child.responseName) {
// concat creates a new shallow copy of the array
childPath = path.concat(child.responseName);
}
iterateOverTraceNode(child, childPath, f);
}
}
f(node, path);
}