Skip to content

Commit

Permalink
feat: Expose context method for writing custom Grafana metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
morgsmccauley committed Jul 13, 2023
1 parent 5b71804 commit 6bca022
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
11 changes: 10 additions & 1 deletion indexer-js-queue-handler/indexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,16 @@ export default class Indexer {
},
log: async (log) => {
return await this.writeLog(functionName, block_height, log);
}
},
putMetric: (metricName, value) => {
const [accountId, name] = functionName.split('/');
return this.deps.metrics.putCustomMetric(
accountId,
name,
`CUSTOM_${metricName}`,
value
);
},
};
}

Expand Down
42 changes: 42 additions & 0 deletions indexer-js-queue-handler/indexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,48 @@ mutation _1 { set(functionName: "buildnear.testnet/test", key: "foo2", data: "in
]);
});

test('allows writing of custom metrics', async () => {
const mockFetch = jest.fn(() => ({
status: 200,
json: async () => ({
errors: null,
}),
}));
const block_height = 456;
const mockS3 = {
getObject: jest.fn(() => ({
promise: () => Promise.resolve({
Body: {
toString: () => JSON.stringify({
chunks: [],
header: {
height: block_height
}
})
}
})
})),
};
const metrics = {
putBlockHeight: () => {},
putCustomMetric: jest.fn(),
};
const indexer = new Indexer('mainnet', { fetch: mockFetch, s3: mockS3, awsXray: mockAwsXray, metrics });

const functions = {};
functions['buildnear.testnet/test'] = {code:`
context.putMetric('TEST_METRIC', 1)
`};
await indexer.runFunctions(block_height, functions, false, { imperative: true });

expect(metrics.putCustomMetric).toHaveBeenCalledWith(
'buildnear.testnet',
'test',
'CUSTOM_TEST_METRIC',
1
);
});

// The unhandled promise causes problems with test reporting.
// Note unhandled promise rejections fail to proceed to the next function on AWS Lambda
test.skip('Indexer.runFunctions() continues despite promise rejection, unable to log rejection', async () => {
Expand Down
8 changes: 6 additions & 2 deletions indexer-js-queue-handler/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ export default class Metrics {
}

putBlockHeight(accountId, functionName, height) {
return this.putCustomMetric(accountId, functionName, "INDEXER_FUNCTION_LATEST_BLOCK_HEIGHT", height);
}

putCustomMetric(accountId, functionName, metricName, value) {
return this.cloudwatch
.putMetricData({
MetricData: [
{
MetricName: "INDEXER_FUNCTION_LATEST_BLOCK_HEIGHT",
MetricName: metricName,
Dimensions: [
{
Name: "ACCOUNT_ID",
Expand All @@ -28,7 +32,7 @@ export default class Metrics {
},
],
Unit: "None",
Value: height,
Value: value,
},
],
Namespace: this.namespace,
Expand Down

0 comments on commit 6bca022

Please sign in to comment.