Skip to content

Commit

Permalink
feat(backend): switch tel servive impl in dep definition
Browse files Browse the repository at this point in the history
  • Loading branch information
BlairCurrey committed Sep 24, 2024
1 parent a003c3e commit dfd8b2b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 36 deletions.
11 changes: 9 additions & 2 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ import { createIlpPaymentService } from './payment-method/ilp/service'
import { createSPSPRoutes } from './payment-method/ilp/spsp/routes'
import { createStreamCredentialsService } from './payment-method/ilp/stream-credentials/service'
import { createRatesService } from './rates/service'
import { createTelemetryService } from './telemetry/service'
import {
createTelemetryService,
createNoopTelemetryService
} from './telemetry/service'
import { createWebhookService } from './webhook/service'

BigInt.prototype.toJSON = function () {
Expand Down Expand Up @@ -142,14 +145,18 @@ export function initIocContainer(

container.singleton('telemetry', async (deps) => {
const config = await deps.use('config')

if (!config.enableTelemetry) {
return createNoopTelemetryService()
}

return createTelemetryService({
logger: await deps.use('logger'),
aseRatesService: await deps.use('ratesService'),
internalRatesService: await deps.use('internalRatesService')!,
instanceName: config.instanceName,
collectorUrls: config.openTelemetryCollectors,
exportIntervalMillis: config.openTelemetryExportInterval,
enableTelemetry: config.enableTelemetry,
baseAssetCode: 'USD',
baseScale: 4
})
Expand Down
41 changes: 21 additions & 20 deletions packages/backend/src/telemetry/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import { ConvertError, RatesService } from '../rates/service'
import { TestContainer, createTestApp } from '../tests/app'
import { mockCounter, mockHistogram } from '../tests/telemetry'
import {
createTelemetryService,
NoopTelemetryServiceImpl,
TelemetryService,
TelemetryServiceImpl,
TelemetryServiceDependencies
TelemetryServiceImpl
} from './service'
import { Counter, Histogram } from '@opentelemetry/api'
import { privacy } from './privacy'
Expand Down Expand Up @@ -66,9 +64,9 @@ describe('Telemetry Service', () => {
})

appContainer = await createTestApp(deps)
telemetryService = await deps.use('telemetry')!
aseRatesService = await deps.use('ratesService')!
internalRatesService = await deps.use('internalRatesService')!
telemetryService = await deps.use('telemetry')
aseRatesService = await deps.use('ratesService')
internalRatesService = await deps.use('internalRatesService')

mockRatesApi(exchangeRatesUrl, (base) => {
apiRequestCount++
Expand All @@ -80,6 +78,10 @@ describe('Telemetry Service', () => {
await appContainer.shutdown()
})

test('telemetryService instance should be real implementation', () => {
expect(telemetryService instanceof TelemetryServiceImpl).toBe(true)
})

it('should create a counter with source attribute for a new metric', () => {
const name = 'test_counter'
const amount = 1
Expand Down Expand Up @@ -441,29 +443,28 @@ describe('Telemetry Service', () => {
})
})
describe('Telemetry Disabled', () => {
let deps: TelemetryServiceDependencies
let deps: IocContract<AppServices>
let appContainer: TestContainer
let telemetryService: TelemetryService

beforeEach(() => {
deps = {
beforeAll(async (): Promise<void> => {
deps = initIocContainer({
...Config,
enableTelemetry: false
} as TelemetryServiceDependencies
})
appContainer = await createTestApp(deps)
telemetryService = await deps.use('telemetry')!
})

test('should return NoopTelemetryServiceImpl when enableTelemetry is false', () => {
const telemetryService = createTelemetryService(deps)

expect(telemetryService).toBeInstanceOf(NoopTelemetryServiceImpl)
afterAll(async (): Promise<void> => {
await appContainer.shutdown()
})

test('should return TelemetryServiceImpl when enableTelemetry is true', () => {
deps.enableTelemetry = true
const telemetryService = createTelemetryService(deps)

expect(telemetryService).toBeInstanceOf(TelemetryServiceImpl)
test('telemetryService instance should be no-op implementation', () => {
expect(telemetryService instanceof NoopTelemetryServiceImpl).toBe(true)
})

test('NoopTelemetryServiceImpl should not get meter ', () => {
const telemetryService = createTelemetryService(deps)
telemetryService.recordHistogram('testhistogram', 1)
telemetryService.incrementCounter('testcounter', 1)

Expand Down
19 changes: 5 additions & 14 deletions packages/backend/src/telemetry/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ export interface TelemetryServiceDependencies extends BaseService {
internalRatesService: RatesService
baseAssetCode: string
baseScale: number
enableTelemetry: boolean
}

const METER_NAME = 'Rafiki'

export function createTelemetryService(
deps: TelemetryServiceDependencies
): TelemetryService {
if (!deps.enableTelemetry) {
return new NoopTelemetryServiceImpl(deps)
}
return new TelemetryServiceImpl(deps)
}

export function createNoopTelemetryService(): TelemetryService {
return new NoopTelemetryServiceImpl()
}

export class TelemetryServiceImpl implements TelemetryService {
private instanceName: string
private meterProvider?: MeterProvider
Expand Down Expand Up @@ -223,16 +223,7 @@ export class TelemetryServiceImpl implements TelemetryService {

/* eslint-disable @typescript-eslint/no-unused-vars */
export class NoopTelemetryServiceImpl implements TelemetryService {
private instanceName: string
private meterProvider?: MeterProvider
private internalRatesService: RatesService
private aseRatesService: RatesService

constructor(private deps: TelemetryServiceDependencies) {
this.instanceName = deps.instanceName
this.internalRatesService = deps.internalRatesService
this.aseRatesService = deps.aseRatesService
}
constructor() {}

public async shutdown(): Promise<void> {
// do nothing
Expand Down

0 comments on commit dfd8b2b

Please sign in to comment.