Skip to content

Commit

Permalink
make serviceName optional
Browse files Browse the repository at this point in the history
  • Loading branch information
dawkaka committed Oct 29, 2024
1 parent 67ce69b commit 87766c0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ Intialize apitoolkit into your project by providing `serviceName` like so:

```js
import express from 'express';
import { APIToolkit } from 'apitoolkit-express';
import { expressMiddleware, expressErrorHandler } from 'apitoolkit-express';

const apitoolkitClient = APIToolkit.NewClient({
serviceName: '<YOUR_INSTRUMENTATION_SERVICE_NAME>'
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(expressMiddleware());

app.get('/hello/:name', (req, res) => {
res.json({ message: `Hello ${req.params.name}!` });
});

app.use(expressErrorHandler);

app.listen(3000, () => {
console.log('Server started on port 3000');
});
```

Expand Down
4 changes: 2 additions & 2 deletions lib/apitoolkit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextFunction, Request, Response } from 'express';
import { ReportError } from 'apitoolkit-js';
export { ReportError } from 'apitoolkit-js';
type Config = {
serviceName: string;
serviceName?: string;
debug?: boolean;
redactHeaders?: string[];
redactRequestBody?: string[];
Expand All @@ -12,7 +12,7 @@ type Config = {
tags?: string[];
serviceVersion?: string;
};
export declare function expressMiddleware(config: Config): (req: Request, res: Response, next: NextFunction) => void;
export declare function expressMiddleware(config?: Config): (req: Request, res: Response, next: NextFunction) => void;
export declare function expressErrorHandler(err: Error, _req: Request, _res: Response, next: NextFunction): void;
declare const APIToolkit: {
expressMiddleware: typeof expressMiddleware;
Expand Down
6 changes: 5 additions & 1 deletion lib/apitoolkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ const apitoolkit_js_1 = require("apitoolkit-js");
var apitoolkit_js_2 = require("apitoolkit-js");
Object.defineProperty(exports, "ReportError", { enumerable: true, get: function () { return apitoolkit_js_2.ReportError; } });
function expressMiddleware(config) {
if (!config) {
config = {};
}
return function expressMidd(req, res, next) {
apitoolkit_js_1.asyncLocalStorage.run(new Map(), () => {
const store = apitoolkit_js_1.asyncLocalStorage.getStore();
const msg_id = (0, uuid_1.v4)();
const span = api_1.trace.getTracer(config.serviceName).startSpan('apitoolkit-http-span');
const span = api_1.trace.getTracer(config.serviceName || '').startSpan('apitoolkit-http-span');
if (store) {
store.set('apitoolkit-span', span);
store.set('apitoolkit-msg-id', msg_id);
Expand All @@ -28,6 +31,7 @@ function expressMiddleware(config) {
res.send = val => {
if (config.captureResponseBody) {
respBody = val;
console.log('APIToolkit: respBodyv >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>', respBody);
}
return oldSend.apply(res, [val]);
};
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

9 changes: 6 additions & 3 deletions src/apitoolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { asyncLocalStorage, ReportError } from 'apitoolkit-js';
export { ReportError } from 'apitoolkit-js';

type Config = {
serviceName: string;
serviceName?: string;
debug?: boolean;
redactHeaders?: string[];
redactRequestBody?: string[];
Expand All @@ -17,12 +17,15 @@ type Config = {
serviceVersion?: string;
};

export function expressMiddleware(config: Config) {
export function expressMiddleware(config?: Config) {
if (!config) {
config = {};
}
return function expressMidd(req: Request, res: Response, next: NextFunction) {
asyncLocalStorage.run(new Map(), () => {
const store = asyncLocalStorage.getStore();
const msg_id = uuidv4();
const span = trace.getTracer(config.serviceName).startSpan('apitoolkit-http-span');
const span = trace.getTracer(config.serviceName || '').startSpan('apitoolkit-http-span');

if (store) {
store.set('apitoolkit-span', span);
Expand Down

0 comments on commit 87766c0

Please sign in to comment.