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

chore(backend): fix incrementCounterWithTransactionAmount function #3146

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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))
Copy link
Contributor

Choose a reason for hiding this comment

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

ah man... Number takes anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely feels like it should take in some proper types instead...

: 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
Loading