Skip to content

Commit

Permalink
Fix/bundling (#11)
Browse files Browse the repository at this point in the history
* Build with tsup instead

* Add block comments to public interface

* Mark express types as optional peerDependency
  • Loading branch information
wisko authored Nov 12, 2024
1 parent 942fa40 commit b2c0250
Show file tree
Hide file tree
Showing 9 changed files with 1,307 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .c8rc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"branches": "100",
"functions": "100",
"lines": "100",
"exclude": ["coverage", "dist", "test", "index.ts", "eslint.config.js"]
"exclude": ["coverage", "dist", "test", "index.ts", "eslint.config.js", "tsup.config.ts"]
}
6 changes: 6 additions & 0 deletions lib/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { getStore } from "./middleware";

/**
* Retrieves an object from the request context store with headers that can be used
* for requests to downstream services.
*
* Make sure you are using the `middleware` when using this.
*/
export function getHttpTraceHeader(): Record<string, string> {
const { traceparent } = getStore() || {};
return traceparent ? { traceparent } : {};
Expand Down
3 changes: 3 additions & 0 deletions lib/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export type LoggerOptions = Omit<PinoOptions, "level" | "formatters"> & {

export type DestinationStream = PinoDestinationStream;

/**
* Creates a pino logger that is pre-configured and ready to be used with minimal setup.
*/
export function logger(options: LoggerOptions = {}, stream?: DestinationStream | undefined): Logger {
const env = process.env.NODE_ENV /* c8 ignore next */ || "development";
const shouldPrettyPrint = ["development", "test", "dev"].includes(env) && !stream;
Expand Down
6 changes: 6 additions & 0 deletions lib/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export type Middleware = () => RequestHandler;

const storage = new AsyncLocalStorage<Store>();

/**
* Express middleware to be used to automatically decorate all logs with trace information.
*
* Only logs that occur inside the request context will be decorated, and applications running
* in GCP will get the appropriate log fields to show up correctly in the GCP Trace Explorer.
*/
export const middleware: Middleware = () => {
let initialized = false;
let projectId: string | undefined;
Expand Down
3 changes: 3 additions & 0 deletions lib/traceparent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export function getTraceFromTraceparent(traceHeader: string) {
};
}

/**
* Generate a unique traceparent header value.
*/
export function createTraceparent(isSampled: boolean = false) {
const version = "00";
const traceId = crypto.randomBytes(16).toString("hex");
Expand Down
Loading

0 comments on commit b2c0250

Please sign in to comment.