Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DPLT-1044 Expose context method for writing custom Grafana metrics #126

Merged
merged 2 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: (name, value) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are graphql, set, and log present in buildFunctionalContextForFunction as well as buildImperativeContextForFunction whereas the new putMetric is only present in the latter?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. We don't use the functional mode anymore. The place we would potentially resurrect it is to parallelize historical processing. That would be for a scenario where we want to execute against blocks in parallel, accumulating mutations to be run, and then running those mutations sequentially. For that sort of disjointed processing metrics might end up weird.
Still, we should probably add it, or we could remove "functional" mode until it's needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't add it because functional is essentially unused now.

Like you said @gabehamilton, metrics could end up weird in functional because of the deferred execution, unless there is someway to pass the timestamp directly 🤔. Probably not worth spending too much time on this though since it's not really used. Happy to remove it in a follow up PR if everyone agrees.

const [accountId, fnName] = functionName.split('/');
return this.deps.metrics.putCustomMetric(
accountId,
fnName,
`CUSTOM_${name}`,
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",
morgsmccauley marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -28,7 +32,7 @@ export default class Metrics {
},
],
Unit: "None",
morgsmccauley marked this conversation as resolved.
Show resolved Hide resolved
Value: height,
Value: value,
},
],
Namespace: this.namespace,
Expand Down