@graphql-mesh/plugin-statsd
Version:
81 lines (80 loc) • 3.15 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = useMeshStatsd;
const hot_shots_1 = require("hot-shots");
const statsd_1 = require("@envelop/statsd");
const utils_1 = require("@graphql-mesh/utils");
const disposablestack_1 = require("@whatwg-node/disposablestack");
const metricNames = {
delegationCount: 'delegations.count',
delegationErrorCount: 'delegations.error.count',
delegationLatency: 'delegations.latency',
fetchCount: 'fetch.count',
fetchErrorCount: 'fetch.error.count',
fetchLatency: 'fetch.latency',
};
function useMeshStatsd(pluginOptions) {
const client = pluginOptions.client && 'close' in pluginOptions.client
? pluginOptions.client
: new hot_shots_1.StatsD(pluginOptions.client);
const prefix = pluginOptions.prefix || 'graphql';
return {
onPluginInit({ addPlugin }) {
addPlugin((0, statsd_1.useStatsD)({
...pluginOptions,
client,
}));
},
onFetch({ url, options, info }) {
const tags = {
url,
method: options.method,
sourceName: info?.sourceName,
fieldName: info?.fieldName,
requestHeaders: JSON.stringify(options.headers),
};
const start = Date.now();
return ({ response }) => {
tags.statusCode = response.status.toString();
tags.statusText = response.statusText;
const responseHeadersObj = (0, utils_1.getHeadersObj)(response.headers);
tags.responseHeaders = JSON.stringify(responseHeadersObj);
client.increment(`${prefix}.${metricNames.fetchCount}`, tags);
const end = Date.now();
if (!response.ok) {
client.increment(`${prefix}.${metricNames.fetchErrorCount}`, tags);
}
client.histogram(`${prefix}.${metricNames.fetchLatency}`, end - start, tags);
};
},
onDelegate({ sourceName, fieldName, args, key }) {
const tags = {
sourceName,
fieldName,
args: JSON.stringify(args),
key,
};
const start = Date.now();
return ({ result }) => {
if (result instanceof Error) {
client.increment(`${prefix}.${metricNames.delegationErrorCount}`, tags);
}
client.increment(`${prefix}.${metricNames.delegationCount}`, tags);
const end = Date.now();
client.histogram(`${prefix}.${metricNames.delegationLatency}`, end - start, tags);
};
},
[disposablestack_1.DisposableSymbols.asyncDispose]() {
return new Promise((resolve, reject) => {
client.close(err => {
if (err) {
reject(err);
}
else {
resolve();
}
});
});
},
};
}
;