@drawbridge/drawbridge-telemetry 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/express.cjs CHANGED
@@ -29,27 +29,17 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  // express.js
30
30
  var express_exports = {};
31
31
  __export(express_exports, {
32
+ applyRequestContext: () => applyRequestContext,
32
33
  expressContextMiddleware: () => expressContextMiddleware
33
34
  });
34
35
  module.exports = __toCommonJS(express_exports);
35
36
  var Sentry = __toESM(require("@sentry/core"), 1);
36
- var expressContextMiddleware = () => (req, res, next) => {
37
+ var applyRequestContext = (req) => {
37
38
  const scope = Sentry.getCurrentScope();
38
39
  if (req == null ? void 0 : req.id) {
39
40
  scope.setTag("traceId", req.id);
40
41
  }
41
42
  ;
42
- if (req == null ? void 0 : req.user) {
43
- scope.setUser({
44
- id: req.user.id || req.user._id,
45
- email: req.user.email
46
- });
47
- }
48
- ;
49
- if (req == null ? void 0 : req.organization) {
50
- scope.setTag("organization", req.organization.id || req.organization._id);
51
- }
52
- ;
53
43
  if (req == null ? void 0 : req.method) {
54
44
  scope.setTag("method", req.method);
55
45
  }
@@ -58,9 +48,25 @@ var expressContextMiddleware = () => (req, res, next) => {
58
48
  scope.setTag("route", req.path);
59
49
  }
60
50
  ;
51
+ const user = (req == null ? void 0 : req.authenticated) || (req == null ? void 0 : req.user);
52
+ if (user) {
53
+ scope.setUser({
54
+ id: user.id || user._id,
55
+ email: user.email
56
+ });
57
+ }
58
+ ;
59
+ if (req == null ? void 0 : req.organization) {
60
+ scope.setTag("organization", req.organization.id || req.organization._id);
61
+ }
62
+ ;
63
+ };
64
+ var expressContextMiddleware = () => (req, res, next) => {
65
+ applyRequestContext(req);
61
66
  next();
62
67
  };
63
68
  // Annotate the CommonJS export names for ESM import in node:
64
69
  0 && (module.exports = {
70
+ applyRequestContext,
65
71
  expressContextMiddleware
66
72
  });
@@ -1,16 +1,14 @@
1
1
  import * as Sentry from '@sentry/core';
2
2
 
3
- // Express middleware that tags the active Sentry scope with the current
4
- // user/organization/route/method/traceId. Mount AFTER auth resolution (so
5
- // req.user and req.organization are populated) and AFTER any req.id
6
- // middleware (so req.id exists to use as the traceId).
3
+ // Apply all available context tags from a request to the active Sentry
4
+ // scope. Idempotent safe to call multiple times as the request enriches
5
+ // (e.g. once at request entry for traceId/method/path, again from auth
6
+ // middleware once req.authenticated and req.organization are populated).
7
7
  //
8
- // Every Sentry event captured during this request — and any downstream
9
- // BullMQ jobs that propagate the traceId via job.data.__traceId — is
10
- // filterable by these tags. Without this, errors arrive in Sentry as
11
- // anonymous stack traces.
8
+ // Reads from req.authenticated first (drawbridge-api convention) and
9
+ // falls back to req.user for compatibility with other services.
12
10
 
13
- const expressContextMiddleware = () => ( req, res, next ) => {
11
+ const applyRequestContext = ( req ) => {
14
12
 
15
13
  const scope = Sentry.getCurrentScope();
16
14
 
@@ -19,11 +17,27 @@ const expressContextMiddleware = () => ( req, res, next ) => {
19
17
  scope.setTag( 'traceId', req.id );
20
18
 
21
19
  }
22
- if( req?.user ){
20
+ if( req?.method ){
21
+
22
+ scope.setTag( 'method', req.method );
23
+
24
+ }
25
+ // req.route is set by Express only AFTER route matching, which hasn't
26
+ // happened yet at request-entry middleware time. Use req.path as a
27
+ // best-effort route tag — aggregation by route pattern is left to a
28
+ // future res.on('finish') refinement.
29
+ if( req?.path ){
30
+
31
+ scope.setTag( 'route', req.path );
32
+
33
+ }
34
+ const user = req?.authenticated || req?.user;
35
+
36
+ if( user ){
23
37
 
24
38
  scope.setUser({
25
- id : req.user.id || req.user._id,
26
- email : req.user.email
39
+ id : user.id || user._id,
40
+ email : user.email
27
41
  });
28
42
 
29
43
  }
@@ -32,22 +46,21 @@ const expressContextMiddleware = () => ( req, res, next ) => {
32
46
  scope.setTag( 'organization', req.organization.id || req.organization._id );
33
47
 
34
48
  }
35
- if( req?.method ){
49
+ };
36
50
 
37
- scope.setTag( 'method', req.method );
51
+ // Express middleware factory — runs `applyRequestContext` at request entry
52
+ // and calls next. Tags user/organization opportunistically (only if auth
53
+ // has already populated them); for per-route auth patterns, call
54
+ // `applyRequestContext( req )` again from inside auth middleware after
55
+ // req.authenticated is set so user/org tags apply to the rest of the
56
+ // request's async context.
38
57
 
39
- }
40
- // req.route is set by Express only AFTER route matching, which hasn't
41
- // happened yet at middleware time. Use req.path as a best-effort route
42
- // tag — it captures the URL hit; aggregation by route pattern is left
43
- // to a future res.on('finish') refinement.
44
- if( req?.path ){
58
+ const expressContextMiddleware = () => ( req, res, next ) => {
45
59
 
46
- scope.setTag( 'route', req.path );
60
+ applyRequestContext( req );
47
61
 
48
- }
49
62
  next();
50
63
 
51
64
  };
52
65
 
53
- export { expressContextMiddleware };
66
+ export { applyRequestContext, expressContextMiddleware };
package/dist/express.d.ts CHANGED
@@ -1,16 +1,14 @@
1
1
  import * as Sentry from '@sentry/core';
2
2
 
3
- // Express middleware that tags the active Sentry scope with the current
4
- // user/organization/route/method/traceId. Mount AFTER auth resolution (so
5
- // req.user and req.organization are populated) and AFTER any req.id
6
- // middleware (so req.id exists to use as the traceId).
3
+ // Apply all available context tags from a request to the active Sentry
4
+ // scope. Idempotent safe to call multiple times as the request enriches
5
+ // (e.g. once at request entry for traceId/method/path, again from auth
6
+ // middleware once req.authenticated and req.organization are populated).
7
7
  //
8
- // Every Sentry event captured during this request — and any downstream
9
- // BullMQ jobs that propagate the traceId via job.data.__traceId — is
10
- // filterable by these tags. Without this, errors arrive in Sentry as
11
- // anonymous stack traces.
8
+ // Reads from req.authenticated first (drawbridge-api convention) and
9
+ // falls back to req.user for compatibility with other services.
12
10
 
13
- const expressContextMiddleware = () => ( req, res, next ) => {
11
+ const applyRequestContext = ( req ) => {
14
12
 
15
13
  const scope = Sentry.getCurrentScope();
16
14
 
@@ -19,11 +17,27 @@ const expressContextMiddleware = () => ( req, res, next ) => {
19
17
  scope.setTag( 'traceId', req.id );
20
18
 
21
19
  }
22
- if( req?.user ){
20
+ if( req?.method ){
21
+
22
+ scope.setTag( 'method', req.method );
23
+
24
+ }
25
+ // req.route is set by Express only AFTER route matching, which hasn't
26
+ // happened yet at request-entry middleware time. Use req.path as a
27
+ // best-effort route tag — aggregation by route pattern is left to a
28
+ // future res.on('finish') refinement.
29
+ if( req?.path ){
30
+
31
+ scope.setTag( 'route', req.path );
32
+
33
+ }
34
+ const user = req?.authenticated || req?.user;
35
+
36
+ if( user ){
23
37
 
24
38
  scope.setUser({
25
- id : req.user.id || req.user._id,
26
- email : req.user.email
39
+ id : user.id || user._id,
40
+ email : user.email
27
41
  });
28
42
 
29
43
  }
@@ -32,22 +46,21 @@ const expressContextMiddleware = () => ( req, res, next ) => {
32
46
  scope.setTag( 'organization', req.organization.id || req.organization._id );
33
47
 
34
48
  }
35
- if( req?.method ){
49
+ };
36
50
 
37
- scope.setTag( 'method', req.method );
51
+ // Express middleware factory — runs `applyRequestContext` at request entry
52
+ // and calls next. Tags user/organization opportunistically (only if auth
53
+ // has already populated them); for per-route auth patterns, call
54
+ // `applyRequestContext( req )` again from inside auth middleware after
55
+ // req.authenticated is set so user/org tags apply to the rest of the
56
+ // request's async context.
38
57
 
39
- }
40
- // req.route is set by Express only AFTER route matching, which hasn't
41
- // happened yet at middleware time. Use req.path as a best-effort route
42
- // tag — it captures the URL hit; aggregation by route pattern is left
43
- // to a future res.on('finish') refinement.
44
- if( req?.path ){
58
+ const expressContextMiddleware = () => ( req, res, next ) => {
45
59
 
46
- scope.setTag( 'route', req.path );
60
+ applyRequestContext( req );
47
61
 
48
- }
49
62
  next();
50
63
 
51
64
  };
52
65
 
53
- export { expressContextMiddleware };
66
+ export { applyRequestContext, expressContextMiddleware };
package/dist/express.js CHANGED
@@ -1,22 +1,11 @@
1
1
  // express.js
2
2
  import * as Sentry from "@sentry/core";
3
- var expressContextMiddleware = () => (req, res, next) => {
3
+ var applyRequestContext = (req) => {
4
4
  const scope = Sentry.getCurrentScope();
5
5
  if (req == null ? void 0 : req.id) {
6
6
  scope.setTag("traceId", req.id);
7
7
  }
8
8
  ;
9
- if (req == null ? void 0 : req.user) {
10
- scope.setUser({
11
- id: req.user.id || req.user._id,
12
- email: req.user.email
13
- });
14
- }
15
- ;
16
- if (req == null ? void 0 : req.organization) {
17
- scope.setTag("organization", req.organization.id || req.organization._id);
18
- }
19
- ;
20
9
  if (req == null ? void 0 : req.method) {
21
10
  scope.setTag("method", req.method);
22
11
  }
@@ -25,8 +14,24 @@ var expressContextMiddleware = () => (req, res, next) => {
25
14
  scope.setTag("route", req.path);
26
15
  }
27
16
  ;
17
+ const user = (req == null ? void 0 : req.authenticated) || (req == null ? void 0 : req.user);
18
+ if (user) {
19
+ scope.setUser({
20
+ id: user.id || user._id,
21
+ email: user.email
22
+ });
23
+ }
24
+ ;
25
+ if (req == null ? void 0 : req.organization) {
26
+ scope.setTag("organization", req.organization.id || req.organization._id);
27
+ }
28
+ ;
29
+ };
30
+ var expressContextMiddleware = () => (req, res, next) => {
31
+ applyRequestContext(req);
28
32
  next();
29
33
  };
30
34
  export {
35
+ applyRequestContext,
31
36
  expressContextMiddleware
32
37
  };
package/package.json CHANGED
@@ -52,5 +52,5 @@
52
52
  "build": "tsup && npm publish"
53
53
  },
54
54
  "types": "dist/index.d.ts",
55
- "version": "0.0.1"
55
+ "version": "0.0.2"
56
56
  }