Skip to content

Commit

Permalink
refactor(backend): add NoopTelemetryServiceImpl to make tel optional (#…
Browse files Browse the repository at this point in the history
…2991)

* refactor(backend): add NoopTelemetryServiceImpl to make tel optional

* refactor(backend): rm unecessary telemetry exists check

* fix(localenv): rm telemetry log noise

* feat(backend): switch tel servive impl in dep definition
  • Loading branch information
BlairCurrey authored Oct 1, 2024
1 parent a45d56e commit 98ceece
Show file tree
Hide file tree
Showing 10 changed files with 515 additions and 442 deletions.
5 changes: 5 additions & 0 deletions localenv/telemetry/otel-collector-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ processors:
batch:

exporters:
logging:
loglevel: info
debug:
verbosity: detailed
prometheus:
Expand All @@ -18,6 +20,9 @@ exporters:
insecure: true

service:
telemetry:
logs:
level: warn
pipelines:
metrics:
receivers: [otlp]
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ const WALLET_ADDRESS_PATH = '/:walletAddressPath+'

export interface AppServices {
logger: Promise<Logger>
telemetry?: Promise<TelemetryService>
internalRatesService?: Promise<RatesService>
telemetry: Promise<TelemetryService>
internalRatesService: Promise<RatesService>
knex: Promise<Knex>
axios: Promise<AxiosInstance>
config: Promise<IAppConfig>
Expand Down
83 changes: 36 additions & 47 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ import {
} from './payment-method/ilp/ilp_plugin'
import { createHttpTokenService } from './payment-method/ilp/peer-http-token/service'
import { createPeerService } from './payment-method/ilp/peer/service'
import {
createIlpPaymentService,
ServiceDependencies as IlpPaymentServiceDependencies
} from './payment-method/ilp/service'
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 { TelemetryService, createTelemetryService } from './telemetry/service'
import {
createTelemetryService,
createNoopTelemetryService
} from './telemetry/service'
import { createWebhookService } from './webhook/service'

BigInt.prototype.toJSON = function () {
Expand Down Expand Up @@ -135,29 +135,32 @@ export function initIocContainer(
})
})

if (config.enableTelemetry) {
container.singleton('internalRatesService', async (deps) => {
return createRatesService({
logger: await deps.use('logger'),
exchangeRatesUrl: config.telemetryExchangeRatesUrl,
exchangeRatesLifetime: config.telemetryExchangeRatesLifetime
})
container.singleton('internalRatesService', async (deps) => {
return createRatesService({
logger: await deps.use('logger'),
exchangeRatesUrl: config.telemetryExchangeRatesUrl,
exchangeRatesLifetime: config.telemetryExchangeRatesLifetime
})
})

container.singleton('telemetry', async (deps) => {
const config = await deps.use('config')
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,
baseAssetCode: 'USD',
baseScale: 4
})
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,
baseAssetCode: 'USD',
baseScale: 4
})
}
})

container.singleton('openApi', async () => {
const resourceServerSpec = await getResourceServerOpenAPI()
Expand Down Expand Up @@ -356,10 +359,6 @@ export function initIocContainer(

container.singleton('connectorApp', async (deps) => {
const config = await deps.use('config')
let telemetry: TelemetryService | undefined
if (config.enableTelemetry) {
telemetry = await deps.use('telemetry')
}
return await createConnectorService({
logger: await deps.use('logger'),
redis: await deps.use('redis'),
Expand All @@ -370,7 +369,7 @@ export function initIocContainer(
ratesService: await deps.use('ratesService'),
streamServer: await deps.use('streamServer'),
ilpAddress: config.ilpAddress,
telemetry
telemetry: await deps.use('telemetry')
})
})

Expand Down Expand Up @@ -425,19 +424,14 @@ export function initIocContainer(
})

container.singleton('ilpPaymentService', async (deps) => {
const serviceDependencies: IlpPaymentServiceDependencies = {
return await createIlpPaymentService({
logger: await deps.use('logger'),
knex: await deps.use('knex'),
config: await deps.use('config'),
makeIlpPlugin: await deps.use('makeIlpPlugin'),
ratesService: await deps.use('ratesService')
}

if (config.enableTelemetry) {
serviceDependencies.telemetry = await deps.use('telemetry')
}

return createIlpPaymentService(serviceDependencies)
ratesService: await deps.use('ratesService'),
telemetry: await deps.use('telemetry')
})
})

container.singleton('paymentMethodHandlerService', async (deps) => {
Expand Down Expand Up @@ -469,7 +463,6 @@ export function initIocContainer(
})

container.singleton('outgoingPaymentService', async (deps) => {
const config = await deps.use('config')
return await createOutgoingPaymentService({
logger: await deps.use('logger'),
knex: await deps.use('knex'),
Expand All @@ -481,9 +474,7 @@ export function initIocContainer(
peerService: await deps.use('peerService'),
walletAddressService: await deps.use('walletAddressService'),
quoteService: await deps.use('quoteService'),
telemetry: config.enableTelemetry
? await deps.use('telemetry')
: undefined
telemetry: await deps.use('telemetry')
})
})

Expand Down Expand Up @@ -548,10 +539,8 @@ export const gracefulShutdown = async (
await redis.quit()
redis.disconnect()

if (config.enableTelemetry) {
const telemetry = await container.use('telemetry')
telemetry?.shutdown()
}
const telemetry = await container.use('telemetry')
telemetry.shutdown()
}

export const start = async (
Expand Down
38 changes: 18 additions & 20 deletions packages/backend/src/open_payments/payment/outgoing/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,24 @@ export async function handleSending(
})
const payEndTime = Date.now()

if (deps.telemetry) {
const payDuration = payEndTime - payStartTime
await Promise.all([
deps.telemetry.incrementCounter('transactions_total', 1, {
description: 'Count of funded transactions'
}),
deps.telemetry.recordHistogram('ilp_pay_time_ms', payDuration, {
description: 'Time to complete an ILP payment'
}),
deps.telemetry.incrementCounterWithTransactionAmountDifference(
'transaction_fee_amounts',
payment.sentAmount,
payment.receiveAmount,
{
description: 'Amount sent through the network as fees',
valueType: ValueType.DOUBLE
}
)
])
}
const payDuration = payEndTime - payStartTime
await Promise.all([
deps.telemetry.incrementCounter('transactions_total', 1, {
description: 'Count of funded transactions'
}),
deps.telemetry.recordHistogram('ilp_pay_time_ms', payDuration, {
description: 'Time to complete an ILP payment'
}),
deps.telemetry.incrementCounterWithTransactionAmountDifference(
'transaction_fee_amounts',
payment.sentAmount,
payment.receiveAmount,
{
description: 'Amount sent through the network as fees',
valueType: ValueType.DOUBLE
}
)
])

await handleCompleted(deps, payment)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface ServiceDependencies extends BaseService {
paymentMethodHandlerService: PaymentMethodHandlerService
walletAddressService: WalletAddressService
quoteService: QuoteService
telemetry?: TelemetryService
telemetry: TelemetryService
}

export async function createOutgoingPaymentService(
Expand Down
40 changes: 18 additions & 22 deletions packages/backend/src/payment-method/ilp/connector/core/rafiki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface TransferOptions {
export interface RafikiServices {
//router: Router
accounting: AccountingService
telemetry?: TelemetryService
telemetry: TelemetryService
walletAddresses: WalletAddressService
logger: Logger
incomingPayments: IncomingPaymentService
Expand Down Expand Up @@ -143,7 +143,7 @@ export class Rafiki<T = any> {
get walletAddresses(): WalletAddressService {
return config.walletAddresses
},
get telemetry(): TelemetryService | undefined {
get telemetry(): TelemetryService {
return config.telemetry
},
logger
Expand All @@ -162,9 +162,7 @@ export class Rafiki<T = any> {
const response = new IlpResponse()
const telemetry = this.publicServer.context.services.telemetry

if (telemetry) {
incrementPreparePacketCount(unfulfillable, prepare.amount, telemetry)
}
incrementPreparePacketCount(unfulfillable, prepare.amount, telemetry)

await this.routes(
{
Expand Down Expand Up @@ -194,23 +192,21 @@ export class Rafiki<T = any> {
)
if (!response.rawReply) throw new Error('error generating reply')

if (telemetry) {
const { code, scale } = sourceAccount.asset
incrementFulfillOrRejectPacketCount(
unfulfillable,
prepare.amount,
response,
telemetry
)
await incrementAmount(
unfulfillable,
prepare.amount,
response,
code,
scale,
telemetry
)
}
const { code, scale } = sourceAccount.asset
incrementFulfillOrRejectPacketCount(
unfulfillable,
prepare.amount,
response,
telemetry
)
await incrementAmount(
unfulfillable,
prepare.amount,
response,
code,
scale,
telemetry
)
return response.rawReply
}

Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/payment-method/ilp/connector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface ServiceDependencies extends BaseService {
peerService: PeerService
streamServer: StreamServer
ilpAddress: string
telemetry?: TelemetryService
telemetry: TelemetryService
}

export async function createConnectorService({
Expand Down
20 changes: 9 additions & 11 deletions packages/backend/src/payment-method/ilp/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface ServiceDependencies extends BaseService {
config: IAppConfig
ratesService: RatesService
makeIlpPlugin: (options: IlpPluginOptions) => IlpPlugin
telemetry?: TelemetryService
telemetry: TelemetryService
}

export async function createIlpPaymentService(
Expand Down Expand Up @@ -94,16 +94,14 @@ async function getQuote(
}
const payEndTime = Date.now()

if (deps.telemetry) {
const rateProbeDuraiton = payEndTime - rateProbeStartTime
deps.telemetry.recordHistogram(
'ilp_rate_probe_time_ms',
rateProbeDuraiton,
{
description: 'Time to get an ILP quote'
}
)
}
const rateProbeDuraiton = payEndTime - rateProbeStartTime
deps.telemetry.recordHistogram(
'ilp_rate_probe_time_ms',
rateProbeDuraiton,
{
description: 'Time to get an ILP quote'
}
)
// Pay.startQuote should return PaymentError.InvalidSourceAmount or
// PaymentError.InvalidDestinationAmount for non-positive amounts.
// Outgoing payments' sendAmount or receiveAmount should never be
Expand Down
Loading

0 comments on commit 98ceece

Please sign in to comment.