Skip to content

Commit

Permalink
feat: add pino as proper logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasperhino committed Dec 10, 2024
1 parent 5bd7f6f commit 26a4faa
Show file tree
Hide file tree
Showing 17 changed files with 292 additions and 54 deletions.
1 change: 1 addition & 0 deletions analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dotenv": "^16.4.7",
"glob": "^11.0.0",
"octokit": "^4.0.2",
"pino": "^9.5.0",
"typescript": "^5.7.2"
},
"devDependencies": {
Expand Down
93 changes: 93 additions & 0 deletions analytics/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions analytics/src/github-storage.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import "dotenv/config";

import { readFileSync } from "fs";
import { storeMetricsToRepo } from "./github.js";
import logger from "./logger.js";

export async function storeInGitHub(file: string, benchmark = false) {
if (process.env.DEBUG) {
console.log("DEBUG mode enabled, skipping GitHub storage.");
logger.debug("DEBUG mode enabled, skipping GitHub storage.");
return;
}

if (!process.env.GITHUB_REPOSITORY) {
console.log("GITHUB_REPOSITORY environment variable is not set");
logger.warn("GITHUB_REPOSITORY environment variable is not set");
return;
}

Expand Down
21 changes: 11 additions & 10 deletions analytics/src/github.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import "dotenv/config";
import { Octokit } from "octokit"
import logger from "./logger.js";


async function obtainOctokit(): Promise<Octokit> {
let octokit: Octokit;
if (process.env.GITHUB_ACTIONS) {
console.log("Running in GitHub Actions, using @octokit/auth-action");
logger.debug("Running in GitHub Actions, using @octokit/auth-action");
const { createActionAuth } = await import("@octokit/auth-action");
octokit = new Octokit({ authStrategy: createActionAuth });
} else {
Expand All @@ -20,7 +21,7 @@ async function obtainOctokit(): Promise<Octokit> {
}

async function createTag(tag: string, message: string, object_sha: string, owner: string, repo: string) {
console.log(`creating tag ${tag} - "${message}"`);
logger.debug(`creating tag ${tag} - "${message}"`);

const octokit = await obtainOctokit();

Expand All @@ -36,11 +37,11 @@ async function createTag(tag: string, message: string, object_sha: string, owner
}
);

console.log(response);
logger.debug(response);
}

async function createRef(ref: string, sha: string, owner: string, repo: string) {
console.log(`creating ref ${ref} for metrics tree ${sha}`);
logger.debug(`creating ref ${ref} for metrics tree ${sha}`);

const octokit = await obtainOctokit();

Expand All @@ -54,11 +55,11 @@ async function createRef(ref: string, sha: string, owner: string, repo: string)
}
);

console.log(response);
logger.debug(response);
}

async function createBlob(content: string, owner: string, repo: string) {
console.log(`creating blob with content: ${content.substring(0, 10)} ...`);
logger.debug(`creating blob with content: ${content.substring(0, 10)} ...`);

const octokit = await obtainOctokit();

Expand All @@ -72,11 +73,11 @@ async function createBlob(content: string, owner: string, repo: string) {
}
);

console.log(response);
logger.debug(response);
}

async function createTree(metrics: string, owner: string, repo: string): Promise<string> {
console.log(`creating tree at ${owner}/${repo}`);
logger.debug(`creating tree at ${owner}/${repo}`);

const octokit = await obtainOctokit();

Expand All @@ -96,13 +97,13 @@ async function createTree(metrics: string, owner: string, repo: string): Promise
}
);

console.log(response);
logger.debug(response);
return response.data.sha;
}

export async function storeMetricsToRepo(metrics: string, commit_sha: string, owner: string, repo: string) {
if (process.env.DEBUG) {
console.log("DEBUG mode enabled, skipping GitHub API calls");
logger.debug("DEBUG mode enabled, skipping GitHub API calls");
return;
}

Expand Down
11 changes: 6 additions & 5 deletions analytics/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import "dotenv/config";

import { storeInGitHub } from "./github-storage.js";
import logger from "./logger.js";

if (!process.env.REPOSITORY_PATH) {
throw new Error("REPOSITORY_PATH environment variable is not set");
}
let metrics_path = "/metrics/metrics.json";
if (!process.env.METRICS_PATH) {
console.warn("Using default METRICS_PATH /metrics/metrics.json")
logger.warn("Using default METRICS_PATH /metrics/metrics.json")
} else {
metrics_path = process.env.METRICS_PATH;
}
console.log(`REPOSITORY_PATH="${process.env.REPOSITORY_PATH}"`);
console.log(`METRICS_PATH="${process.env.METRICS_PATH}"`)
logger.info(`REPOSITORY_PATH="${process.env.REPOSITORY_PATH}"`);
logger.info(`METRICS_PATH="${process.env.METRICS_PATH}"`)

const benchmark = Boolean(process.env.BENCHMARK);
console.log(`Perform benchmarking of operations`);
if (benchmark) logger.info(`Perform benchmarking of operations`);

storeInGitHub(metrics_path, benchmark).catch((e) => {
console.error(e);
logger.error(e);
process.exit(1);
});
13 changes: 13 additions & 0 deletions analytics/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { pino } from 'pino';

const logger = pino({
level: process.env.LOG_LEVEL || 'info',
transport: {
target: 'pino-pretty',
options: {
colorize: process.env.NODE_ENV !== 'production'
}
}
});

export default logger;
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"buffer": "^6.0.3",
"isomorphic-fetch": "^3.0.0",
"octokit": "^4.0.2",
"pino": "^9.5.0",
"pretty-bytes": "^6.1.1",
"react": "^19.0.0",
"react-dom": "^19.0.0",
Expand Down
Loading

0 comments on commit 26a4faa

Please sign in to comment.