Skip to content

Commit

Permalink
refactor(observability): ensure logger can be configured with options…
Browse files Browse the repository at this point in the history
… and env

refs #430
  • Loading branch information
ygrishajev committed Nov 13, 2024
1 parent c9efd6c commit bb84492
Show file tree
Hide file tree
Showing 31 changed files with 197 additions and 190 deletions.
2 changes: 1 addition & 1 deletion apps/api/mvm.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"@akashnetwork/database": "1.0.0",
"@akashnetwork/env-loader": "1.0.1",
"@akashnetwork/http-sdk": "1.0.8",
"@akashnetwork/logging": "1.0.1"
"@akashnetwork/logging": "2.0.0"
}
}
2 changes: 1 addition & 1 deletion apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function startScheduler() {
scheduler.start();
}

const appLogger = new LoggerService({ context: "APP" });
const appLogger = LoggerService.forContext("APP");

/**
* Initialize database
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type CheckoutSessionsOutput = Table["$inferSelect"];

@singleton()
export class CheckoutSessionRepository extends BaseRepository<Table, CheckoutSessionsInput, CheckoutSessionsOutput> {
private readonly logger = new LoggerService({ context: CheckoutSessionRepository.name });
private readonly logger = LoggerService.forContext(CheckoutSessionRepository.name);

constructor(
@InjectPg() protected readonly pg: ApiPgDatabase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class ManagedUserWalletService {

private readonly HD_PATH = "m/44'/118'/0'/0";

private readonly logger = new LoggerService({ context: ManagedUserWalletService.name });
private readonly logger = LoggerService.forContext(ManagedUserWalletService.name);

constructor(
@InjectBillingConfig() private readonly config: BillingConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class MasterSigningClientService {
{ cache: false, batchScheduleFn: callback => setTimeout(callback, this.config.MASTER_WALLET_BATCHING_INTERVAL_MS) }
);

private readonly logger = new LoggerService({ context: this.loggerContext });
private readonly logger = LoggerService.forContext(this.loggerContext);

constructor(
private readonly config: BillingConfig,
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/billing/services/refill/refill.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SentryEventService } from "@src/core/services/sentry-event/sentry-event

@singleton()
export class RefillService {
private readonly logger = new LoggerService({ context: RefillService.name });
private readonly logger = LoggerService.forContext(RefillService.name);

constructor(
@InjectBillingConfig() private readonly config: BillingConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { WithTransaction } from "@src/core";

@singleton()
export class StripeWebhookService {
private readonly logger = new LoggerService({ context: StripeWebhookService.name });
private readonly logger = LoggerService.forContext(StripeWebhookService.name);

constructor(
private readonly stripe: StripeService,
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/caching/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { differenceInSeconds } from "date-fns";

import MemoryCacheEngine from "./memoryCacheEngine";

const logger = new LoggerService({ context: "Caching" });
const logger = LoggerService.forContext("Caching");

export const cacheEngine = new MemoryCacheEngine();
const pendingRequests: { [key: string]: Promise<unknown> } = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import "@test/mocks/logger-service.mock";

import { BlockHttpService as BlockHttpServiceCommon } from "@akashnetwork/http-sdk";
import { faker } from "@faker-js/faker";

import { BlockHttpService } from "./block-http.service";

jest.mock("@akashnetwork/logging");

describe(BlockHttpService.name, () => {
let service: BlockHttpService;
let blockHttpService: BlockHttpServiceCommon;
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ program
});
});

const logger = new LoggerService({ context: "CLI" });
const logger = LoggerService.forContext("CLI");

async function executeCliHandler(name: string, handler: () => Promise<void>) {
await context.with(trace.setSpan(context.active(), tracer.startSpan(name)), async () => {
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/core/providers/postgres.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { config } from "@src/core/config";
import { PostgresLoggerService } from "@src/core/services/postgres-logger/postgres-logger.service";
import * as userSchemas from "@src/user/model-schemas";

const logger = new LoggerService({ context: "POSTGRES" });
const logger = LoggerService.forContext("POSTGRES");
const migrationClient = postgres(config.POSTGRES_DB_URI, { max: 1, onnotice: logger.info.bind(logger) });
const appClient = postgres(config.POSTGRES_DB_URI, { max: config.POSTGRES_MAX_CONNECTIONS, onnotice: logger.info.bind(logger) });

Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/core/services/error/error.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import "@test/mocks/logger-service.mock";

import { LoggerService } from "@akashnetwork/logging";
import { faker } from "@faker-js/faker";

import { Sentry } from "@src/core/providers/sentry.provider";
import { SentryEventService } from "@src/core/services/sentry-event/sentry-event.service";
import { ErrorService } from "./error.service";

jest.mock("@akashnetwork/logging");

describe(ErrorService.name, () => {
const sentryEventService = new SentryEventService();
let sentry: Sentry;
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/core/services/error/error.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SentryEventService } from "@src/core/services/sentry-event/sentry-event

@singleton()
export class ErrorService {
private readonly logger = new LoggerService();
private readonly logger = LoggerService.forContext(ErrorService.name);

constructor(
@InjectSentry() private readonly sentry: Sentry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { SentryEventService } from "@src/core/services/sentry-event/sentry-event

@singleton()
export class HonoErrorHandlerService {
private readonly logger = new LoggerService({ context: "ErrorHandler" });
private readonly logger = LoggerService.forContext("ErrorHandler");

constructor(
@InjectSentry() private readonly sentry: Sentry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { HonoInterceptor } from "@src/core/types/hono-interceptor.type";

@singleton()
export class HttpLoggerService implements HonoInterceptor {
private readonly logger = new LoggerService({ context: "HTTP" });
private readonly logger = LoggerService.forContext("HTTP");

intercept() {
return async (c: Context, next: Next) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class PostgresLoggerService implements LogWriter {

constructor(options?: PostgresLoggerServiceOptions) {
const orm = options?.orm || "drizzle";
this.logger = new LoggerService({ context: "POSTGRES", orm, database: options?.database });
this.logger = new LoggerService({ base: { context: "POSTGRES", orm, database: options?.database } });
this.isDrizzle = orm === "drizzle";
this.useFormat = options?.useFormat || false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { averageBlockTime } from "@src/utils/constants";

@singleton()
export class StaleManagedDeploymentsCleanerService {
private readonly logger = new LoggerService({ context: StaleManagedDeploymentsCleanerService.name });
private readonly logger = LoggerService.forContext(StaleManagedDeploymentsCleanerService.name);

private readonly MAX_LIVE_BLOCKS = Math.floor((10 * secondsInMinute) / averageBlockTime);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "@test/mocks/logger-service.mock";

import { AllowanceHttpService, BalanceHttpService, Denom } from "@akashnetwork/http-sdk";
import { faker } from "@faker-js/faker";
import { MsgExec } from "cosmjs-types/cosmos/authz/v1beta1/tx";
Expand All @@ -23,8 +25,6 @@ import { DrainingDeploymentSeeder } from "@test/seeders/draining-deployment.seed
import { FeesAuthorizationSeeder } from "@test/seeders/fees-authorization.seeder";
import { stub } from "@test/services/stub";

jest.mock("@akashnetwork/logging");

describe(TopUpCustodialDeploymentsService.name, () => {
const CURRENT_BLOCK_HEIGHT = 7481457;
const UAKT_TOP_UP_MASTER_WALLET_ADDRESS = AkashAddressSeeder.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class TopUpCustodialDeploymentsService implements DeploymentsRefiller {

private readonly MIN_FEES_AVAILABLE = 5000;

private readonly logger = new LoggerService({ context: TopUpCustodialDeploymentsService.name });
private readonly logger = LoggerService.forContext(TopUpCustodialDeploymentsService.name);

constructor(
private readonly topUpToolsService: TopUpToolsService,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "@test/mocks/logger-service.mock";

import { faker } from "@faker-js/faker";

import { BillingConfig } from "@src/billing/providers";
Expand All @@ -17,8 +19,6 @@ import { DrainingDeploymentSeeder } from "@test/seeders/draining-deployment.seed
import { UserWalletSeeder } from "@test/seeders/user-wallet.seeder";
import { stub } from "@test/services/stub";

jest.mock("@akashnetwork/logging");

describe(TopUpManagedDeploymentsService.name, () => {
const CURRENT_BLOCK_HEIGHT = 7481457;
const MANAGED_MASTER_WALLET_ADDRESS = AkashAddressSeeder.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { DeploymentsRefiller, TopUpDeploymentsOptions } from "@src/deployment/ty
export class TopUpManagedDeploymentsService implements DeploymentsRefiller {
private readonly CONCURRENCY = 10;

private readonly logger = new LoggerService({ context: TopUpManagedDeploymentsService.name });
private readonly logger = LoggerService.forContext(TopUpManagedDeploymentsService.name);

constructor(
private readonly userWalletRepository: UserWalletRepository,
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "./open-telemetry";
import "reflect-metadata";
import "@akashnetwork/env-loader";
import "./open-telemetry";

async function bootstrap() {
/* eslint-disable @typescript-eslint/no-var-requires */
Expand Down
7 changes: 7 additions & 0 deletions apps/api/src/open-telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { LoggerService } from "@akashnetwork/logging";
import { context, trace } from "@opentelemetry/api";
import { registerInstrumentations } from "@opentelemetry/instrumentation";
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
Expand All @@ -7,3 +9,8 @@ new NodeTracerProvider().register();
registerInstrumentations({
instrumentations: [new HttpInstrumentation()]
});

LoggerService.mixin = () => {
const currentSpan = trace.getSpan(context.active());
return { ...currentSpan?.spanContext() };
};
11 changes: 5 additions & 6 deletions apps/api/src/routes/v1/dashboardData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import { getTransactions } from "@src/services/db/transactionsService";
import { getChainStats } from "@src/services/external/apiNodeService";
import { createLoggingExecutor } from "@src/utils/logging";


const logger = new LoggerService({ context: "Dashboard" });
const runOrLog = createLoggingExecutor(logger)
const logger = LoggerService.forContext("Dashboard");
const runOrLog = createLoggingExecutor(logger);

const route = createRoute({
method: "get",
Expand Down Expand Up @@ -167,8 +166,8 @@ export default new OpenAPIHono().openapi(route, async c => {
const chainStats = {
...chainStatsQuery,
height: latestBlocks && latestBlocks.length > 0 ? latestBlocks[0].height : undefined,
transactionCount: latestBlocks && latestBlocks.length > 0 ? latestBlocks[0].totalTransactionCount : undefined,
}
transactionCount: latestBlocks && latestBlocks.length > 0 ? latestBlocks[0].totalTransactionCount : undefined
};

return c.json({
chainStats,
Expand All @@ -179,4 +178,4 @@ export default new OpenAPIHono().openapi(route, async c => {
latestBlocks,
latestTransactions
});
});
});
2 changes: 1 addition & 1 deletion apps/api/src/services/db/userDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import pick from "lodash/pick";
import { Transaction } from "sequelize";
import { container } from "tsyringe";

const logger = new LoggerService({ context: "UserDataService" });
const logger = LoggerService.forContext("UserDataService");

function randomIntFromInterval(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1) + min);
Expand Down
38 changes: 12 additions & 26 deletions apps/api/src/services/external/apiNodeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,35 @@ import { getDeploymentRelatedMessages } from "../db/deploymentService";
import { getProviderList } from "../db/providerStatusService";

export async function getChainStats() {
const logger = new LoggerService({ context: "ApiNode" })
const runOrLog = createLoggingExecutor(logger)
const logger = LoggerService.forContext("ApiNode");
const runOrLog = createLoggingExecutor(logger);

const result = await cacheResponse(
60 * 5, // 5 minutes
cacheKeys.getChainStats,
async () => {
const bondedTokensAsPromised = await runOrLog(async () => {
const bondedTokensQuery = await axios.get<CosmosStakingPoolResponse>(
`${apiNodeUrl}/cosmos/staking/v1beta1/pool`
);
const bondedTokensQuery = await axios.get<CosmosStakingPoolResponse>(`${apiNodeUrl}/cosmos/staking/v1beta1/pool`);
return parseInt(bondedTokensQuery.data.pool.bonded_tokens);
});

const totalSupplyAsPromised = await runOrLog(async () => {
const supplyQuery = await axios.get<CosmosBankSupplyResponse>(
`${apiNodeUrl}/cosmos/bank/v1beta1/supply?pagination.limit=1000`
);
return parseInt(
supplyQuery.data.supply.find((x) => x.denom === "uakt")?.amount || "0"
);
const supplyQuery = await axios.get<CosmosBankSupplyResponse>(`${apiNodeUrl}/cosmos/bank/v1beta1/supply?pagination.limit=1000`);
return parseInt(supplyQuery.data.supply.find(x => x.denom === "uakt")?.amount || "0");
});

const communityPoolAsPromised = await runOrLog(async () => {
const communityPoolQuery = await axios.get<CosmosDistributionCommunityPoolResponse>(
`${apiNodeUrl}/cosmos/distribution/v1beta1/community_pool`
);
return parseFloat(
communityPoolQuery.data.pool.find((x) => x.denom === "uakt")?.amount || "0"
);
const communityPoolQuery = await axios.get<CosmosDistributionCommunityPoolResponse>(`${apiNodeUrl}/cosmos/distribution/v1beta1/community_pool`);
return parseFloat(communityPoolQuery.data.pool.find(x => x.denom === "uakt")?.amount || "0");
});

const inflationAsPromised = await runOrLog(async () => {
const inflationQuery = await axios.get<CosmosMintInflationResponse>(
`${apiNodeUrl}/cosmos/mint/v1beta1/inflation`
);
const inflationQuery = await axios.get<CosmosMintInflationResponse>(`${apiNodeUrl}/cosmos/mint/v1beta1/inflation`);
return parseFloat(inflationQuery.data.inflation || "0");
});

const communityTaxAsPromised = await runOrLog(async () => {
const distributionQuery = await axios.get<CosmosDistributionParamsResponse>(
`${apiNodeUrl}/cosmos/distribution/v1beta1/params`
);
const distributionQuery = await axios.get<CosmosDistributionParamsResponse>(`${apiNodeUrl}/cosmos/distribution/v1beta1/params`);
return parseFloat(distributionQuery.data.params.community_tax || "0");
});

Expand All @@ -93,23 +79,23 @@ export async function getChainStats() {
inflation,
communityTax,
bondedTokens,
totalSupply,
totalSupply
};
},
true
);

let stakingAPR: number | undefined;
if (result.bondedTokens && result.bondedTokens > 0 && result.inflation && result.communityTax && result.totalSupply) {
stakingAPR = result.inflation * (1 - result.communityTax) * result.totalSupply / result.bondedTokens
stakingAPR = (result.inflation * (1 - result.communityTax) * result.totalSupply) / result.bondedTokens;
}

return {
bondedTokens: result.bondedTokens,
totalSupply: result.totalSupply,
communityPool: result.communityPool,
inflation: result.inflation,
stakingAPR,
stakingAPR
};
}

Expand Down
3 changes: 1 addition & 2 deletions apps/api/src/utils/coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { LoggerService } from "@akashnetwork/logging";
import { asset_lists } from "@chain-registry/assets";
import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin";


const logger = new LoggerService({ context: "CoinUtil" });
const logger = LoggerService.forContext("CoinUtil");

export function coinToAsset(coin: Coin) {
if (coin.denom === "uakt") {
Expand Down
17 changes: 17 additions & 0 deletions apps/api/test/mocks/logger-service.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
jest.mock("@akashnetwork/logging", () => {
class LoggerService {
static forContext() {
return new LoggerService();
}

error() {}
debug() {}
info() {}
}

jest.spyOn(LoggerService.prototype, "error");
jest.spyOn(LoggerService.prototype, "debug");
jest.spyOn(LoggerService.prototype, "info");

return { LoggerService };
});
2 changes: 1 addition & 1 deletion packages/logging/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@akashnetwork/logging",
"version": "1.0.1",
"version": "2.0.0",
"description": "Package containing logging tools",
"main": "src/index.ts",
"scripts": {
Expand Down
Loading

0 comments on commit bb84492

Please sign in to comment.