-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
200 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
coverage | ||
dist | ||
node_modules | ||
.eslintcache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
export { fetchGcpProjectId } from "./lib/gcp"; | ||
export { getHttpTraceHeader } from "./lib/http"; | ||
export { getLoggingTraceData, logger } from "./lib/logging"; | ||
export { decorateLogs, logger } from "./lib/logging"; | ||
export { middleware } from "./lib/middleware"; | ||
export { createTraceparent } from "./lib/traceparent"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,17 @@ | ||
import gcpMetaData from "gcp-metadata"; | ||
|
||
let gcpProjectId: string | undefined = undefined; | ||
|
||
export function getGcpProjectId() { | ||
return process.env.GCP_PROJECT || gcpProjectId; | ||
} | ||
|
||
/** | ||
* Fetches the Google Cloud Platform (GCP) project ID from the GCP metadata server. | ||
* | ||
* You only need to call this function if you're not setting the `GCP_PROJECT` environment variable. | ||
* You can alternatively set the `GCP_PROJECT` environment variable, which takes precedence. | ||
*/ | ||
export async function fetchGcpProjectId() { | ||
export async function getGcpProjectId(): Promise<string | undefined> { | ||
if (process.env.GCP_PROJECT) { | ||
return process.env.GCP_PROJECT; | ||
} | ||
|
||
const isAvailable = await gcpMetaData.isAvailable(); | ||
if (!isAvailable) return; | ||
|
||
gcpProjectId = await gcpMetaData.project("project-id"); | ||
} | ||
|
||
/** | ||
* Resets the GCP project ID, for testing. | ||
*/ | ||
export function reset() { | ||
gcpProjectId = undefined; | ||
return await gcpMetaData.project("project-id"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import { getStore } from "./middleware"; | ||
|
||
export function getHttpTraceHeader() { | ||
const { traceparent } = getStore(); | ||
if (traceparent) return { traceparent }; | ||
return {}; | ||
const { traceparent } = getStore() || {}; | ||
return traceparent ? { traceparent } : {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,55 @@ | ||
import type { RequestHandler } from "express"; | ||
import { AsyncLocalStorage } from "node:async_hooks"; | ||
import { createTraceparent } from "./traceparent"; | ||
import { getGcpProjectId } from "./gcp"; | ||
import { getTraceFromTraceparent, createTraceparent } from "./traceparent"; | ||
|
||
type LogFields = { | ||
traceId?: string; | ||
"logging.googleapis.com/trace"?: string; | ||
"logging.googleapis.com/spanId"?: string; | ||
"logging.googleapis.com/trace_sampled"?: boolean; | ||
[key: string]: unknown; | ||
}; | ||
|
||
type Store = { | ||
traceparent?: string; | ||
clientServiceAccount?: string; | ||
[key: string]: unknown; | ||
logFields: LogFields; | ||
}; | ||
|
||
const storage = new AsyncLocalStorage<Store>(); | ||
|
||
export const middleware: RequestHandler = (req, _res, next) => { | ||
const traceparent = req.header("traceparent") || createTraceparent(); | ||
export type Middleware = () => RequestHandler; | ||
|
||
export const middleware: Middleware = () => { | ||
let initialized = false; | ||
let projectId: string | undefined; | ||
|
||
return async (req, _res, next) => { | ||
if (!initialized) { | ||
initialized = true; | ||
projectId = await getGcpProjectId(); | ||
} | ||
|
||
const traceparent = req.header("traceparent") || createTraceparent(); | ||
const trace = getTraceFromTraceparent(traceparent); | ||
const logFields: LogFields = {}; | ||
|
||
if (trace) { | ||
logFields.traceId = trace.traceId; | ||
|
||
if (projectId) { | ||
logFields["logging.googleapis.com/trace"] = `projects/${projectId}/traces/${trace.traceId}`; | ||
logFields["logging.googleapis.com/spanId"] = trace.parentId; | ||
logFields["logging.googleapis.com/trace_sampled"] = trace.isSampled; | ||
} | ||
} | ||
|
||
storage.run({ traceparent }, () => { | ||
next(); | ||
}); | ||
storage.run({ traceparent, logFields }, () => { | ||
next(); | ||
}); | ||
}; | ||
}; | ||
|
||
export function getStore() { | ||
return storage.getStore() || {}; | ||
export function getStore(): Store | undefined { | ||
return storage.getStore(); | ||
} |
Oops, something went wrong.