-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(telemetry): move amount collection in ILP connector
- Loading branch information
1 parent
88ae1d4
commit b3ebcf3
Showing
9 changed files
with
156 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/backend/src/payment-method/ilp/connector/core/middleware/telemetry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { collectTelemetryAmount } from '../../../../../telemetry/transaction-amount' | ||
import { ILPContext, ILPMiddleware } from '../rafiki' | ||
|
||
export function createTelemetryMiddleware(): ILPMiddleware { | ||
return async ( | ||
{ request, services, accounts, response }: ILPContext, | ||
next: () => Promise<void> | ||
): Promise<void> => { | ||
await next() | ||
if ( | ||
services.telemetry && | ||
Number(request.prepare.amount) && | ||
response.fulfill | ||
) { | ||
const { code, scale } = accounts.outgoing.asset | ||
collectTelemetryAmount(services.telemetry, services.logger, { | ||
amount: BigInt(request.prepare.amount), | ||
asset: { code: code, scale: scale } | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
packages/backend/src/payment-method/ilp/connector/core/test/middleware/telemetry.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import assert from 'assert' | ||
import { IlpResponse, OutgoingAccount, ZeroCopyIlpPrepare } from '../..' | ||
import { IncomingAccountFactory, RafikiServicesFactory } from '../../factories' | ||
import { createTelemetryMiddleware } from '../../middleware/telemetry' | ||
import { createILPContext } from '../../utils' | ||
|
||
import { IlpFulfill } from 'ilp-packet' | ||
import * as telemetry from '../../../../../../telemetry/transaction-amount' | ||
|
||
const incomingAccount = IncomingAccountFactory.build({ id: 'alice' }) | ||
|
||
assert.ok(incomingAccount.id) | ||
const services = RafikiServicesFactory.build({}) | ||
|
||
const ctx = createILPContext({ | ||
services, | ||
request: { | ||
prepare: { | ||
amount: 100n | ||
} as unknown as ZeroCopyIlpPrepare, | ||
rawPrepare: Buffer.from('') | ||
}, | ||
accounts: { | ||
incoming: incomingAccount, | ||
outgoing: { asset: { code: 'USD', scale: 2 } } as OutgoingAccount | ||
}, | ||
state: { | ||
unfulfillable: false, | ||
incomingAccount: { | ||
quote: 'exists' | ||
} | ||
}, | ||
response: { | ||
fulfill: 'exists' as unknown as IlpFulfill | ||
} as IlpResponse | ||
}) | ||
|
||
jest.mock('../../../../../../telemetry/transaction-amount') | ||
const middleware = createTelemetryMiddleware() | ||
const next = jest.fn().mockImplementation(() => Promise.resolve()) | ||
|
||
beforeEach(async () => { | ||
incomingAccount.balance = 100n | ||
incomingAccount.asset.scale = 2 | ||
incomingAccount.asset.code = 'USD' | ||
}) | ||
|
||
describe('Telemetry Middleware', function () { | ||
it('does not gather telemetry if telemetry is not enabled (service is undefined)', async () => { | ||
const collectAmountSpy = jest | ||
.spyOn(telemetry, 'collectTelemetryAmount') | ||
.mockImplementation(() => Promise.resolve()) | ||
|
||
await middleware( | ||
{ ...ctx, services: { ...ctx.services, telemetry: undefined } }, | ||
next | ||
) | ||
expect(collectAmountSpy).not.toHaveBeenCalled() | ||
expect(next).toHaveBeenCalled() | ||
}) | ||
|
||
it('does not gather telemetry if response.fulfill undefined', async () => { | ||
const collectAmountSpy = jest.spyOn(telemetry, 'collectTelemetryAmount') | ||
|
||
await middleware( | ||
{ ...ctx, response: { fulfill: undefined } as IlpResponse }, | ||
next | ||
) | ||
|
||
expect(collectAmountSpy).not.toHaveBeenCalled() | ||
expect(next).toHaveBeenCalled() | ||
}) | ||
|
||
it('does not gather telemetry if amount is invalid', async () => { | ||
const collectAmountSpy = jest.spyOn(telemetry, 'collectTelemetryAmount') | ||
|
||
await middleware( | ||
{ | ||
...ctx, | ||
request: { | ||
...ctx.request, | ||
prepare: { amount: '0' } as ZeroCopyIlpPrepare | ||
} | ||
}, | ||
next | ||
) | ||
|
||
expect(collectAmountSpy).not.toHaveBeenCalled() | ||
expect(next).toHaveBeenCalled() | ||
}) | ||
|
||
it('gathers telemetry without blocking middleware chain', async () => { | ||
let nextCalled = false | ||
const next = jest.fn().mockImplementation(() => { | ||
nextCalled = true | ||
return Promise.resolve() | ||
}) | ||
|
||
const collectAmountSpy = jest | ||
.spyOn(telemetry, 'collectTelemetryAmount') | ||
.mockImplementation(() => { | ||
expect(nextCalled).toBe(true) | ||
return Promise.resolve() | ||
}) | ||
|
||
await middleware(ctx, next) | ||
|
||
expect(collectAmountSpy).toHaveBeenCalled() | ||
expect(next).toHaveBeenCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters