Skip to content

Commit

Permalink
chore(backend): fix incrementCounterWithTransactionAmount function
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurapov committed Dec 2, 2024
1 parent 5de6208 commit eccf2dd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
59 changes: 51 additions & 8 deletions packages/backend/src/telemetry/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { Counter, Histogram } from '@opentelemetry/api'
import { privacy } from './privacy'
import { mockRatesApi } from '../tests/rates'
import { ConvertResults } from '../rates/util'

jest.mock('@opentelemetry/api', () => ({
...jest.requireActual('@opentelemetry/api'),
Expand Down Expand Up @@ -355,14 +356,17 @@ describe('Telemetry Service', () => {
expect(internalConvertSpy).not.toHaveBeenCalled()
})

it('should apply privacy', async () => {
it('should apply privacy by default', async () => {
const convertedAmount = 500n

jest
//"any" to access private ts class member variable
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.spyOn(telemetryService as any, 'convertAmount')
.mockImplementation(() => Promise.resolve(convertedAmount))
.mockResolvedValueOnce({
scaledExchangeRate: 1,
amount: convertedAmount
} as ConvertResults)
const applyPrivacySpy = jest
.spyOn(privacy, 'applyPrivacy')
.mockReturnValue(123)
Expand All @@ -378,14 +382,49 @@ describe('Telemetry Service', () => {
value: 100n,
assetCode: 'USD',
assetScale: 2
}
},
undefined
)

expect(applyPrivacySpy).toHaveBeenCalledWith(Number(convertedAmount))
expect(incrementCounterSpy).toHaveBeenCalledWith(counterName, 123, {})
})

it('should not apply privacy if preservePrivacy is false', async () => {
const convertedAmount = 500n

jest
//"any" to access private ts class member variable
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.spyOn(telemetryService as any, 'convertAmount')
.mockResolvedValueOnce({
scaledExchangeRate: 1,
amount: convertedAmount
} as ConvertResults)

const applyPrivacySpy = jest.spyOn(privacy, 'applyPrivacy')
const incrementCounterSpy = jest.spyOn(
telemetryService,
'incrementCounter'
)

const counterName = 'test_counter'
await telemetryService.incrementCounterWithTransactionAmount(
counterName,
{
value: 100n,
assetCode: 'USD',
assetScale: 2
},
undefined,
false
)

expect(applyPrivacySpy).not.toHaveBeenCalled()
expect(incrementCounterSpy).toHaveBeenCalledWith(
counterName,
123,
expect.any(Object)
Number(convertedAmount),
{}
)
})

Expand Down Expand Up @@ -421,7 +460,10 @@ describe('Telemetry Service', () => {
//"any" to access private ts class member variable
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.spyOn(telemetryService as any, 'convertAmount')
.mockImplementation(() => Promise.resolve(10000n))
.mockResolvedValueOnce({
scaledExchangeRate: 1,
amount: 100n
} as ConvertResults)
const incrementCounterSpy = jest.spyOn(
telemetryService,
'incrementCounter'
Expand All @@ -437,14 +479,15 @@ describe('Telemetry Service', () => {
value: 100n,
assetCode: 'USD',
assetScale: 2
}
},
{ attribute: 'metric attribute' }
)

expect(convertSpy).toHaveBeenCalled()
expect(incrementCounterSpy).toHaveBeenCalledWith(
counterName,
obfuscatedAmount,
expect.any(Object)
{ attribute: 'metric attribute' }
)
})
})
Expand Down
11 changes: 5 additions & 6 deletions packages/backend/src/telemetry/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,13 @@ export class TelemetryServiceImpl implements TelemetryService {
return
}

const obfuscatedAmount = preservePrivacy
? privacy.applyPrivacy(Number(converted))
: Number(converted)
this.incrementCounter(name, obfuscatedAmount, attributes)
const finalAmount = preservePrivacy
? privacy.applyPrivacy(Number(converted.amount))
: Number(converted.amount)
this.incrementCounter(name, finalAmount, attributes)
} catch (e) {
this.deps.logger.error(e, `Unable to collect telemetry`)
this.deps.logger.error(e, 'Unable to collect telemetry')
}
return Promise.resolve()
}

public recordHistogram(
Expand Down

0 comments on commit eccf2dd

Please sign in to comment.