@pingops/core 0.1.1 → 0.1.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/index.cjs +824 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +306 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +306 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +804 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +17 -5
- package/dist/context-keys.d.ts +0 -42
- package/dist/context-keys.d.ts.map +0 -1
- package/dist/context-keys.js +0 -43
- package/dist/context-keys.js.map +0 -1
- package/dist/filtering/domain-filter.d.ts +0 -9
- package/dist/filtering/domain-filter.d.ts.map +0 -1
- package/dist/filtering/domain-filter.js +0 -136
- package/dist/filtering/domain-filter.js.map +0 -1
- package/dist/filtering/header-filter.d.ts +0 -31
- package/dist/filtering/header-filter.d.ts.map +0 -1
- package/dist/filtering/header-filter.js +0 -187
- package/dist/filtering/header-filter.js.map +0 -1
- package/dist/filtering/span-filter.d.ts +0 -13
- package/dist/filtering/span-filter.d.ts.map +0 -1
- package/dist/filtering/span-filter.js +0 -46
- package/dist/filtering/span-filter.js.map +0 -1
- package/dist/index.d.ts +0 -13
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -13
- package/dist/index.js.map +0 -1
- package/dist/logger.d.ts +0 -21
- package/dist/logger.d.ts.map +0 -1
- package/dist/logger.js +0 -36
- package/dist/logger.js.map +0 -1
- package/dist/types.d.ts +0 -46
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -5
- package/dist/types.js.map +0 -1
- package/dist/utils/context-extractor.d.ts +0 -13
- package/dist/utils/context-extractor.d.ts.map +0 -1
- package/dist/utils/context-extractor.js +0 -44
- package/dist/utils/context-extractor.js.map +0 -1
- package/dist/utils/span-extractor.d.ts +0 -10
- package/dist/utils/span-extractor.d.ts.map +0 -1
- package/dist/utils/span-extractor.js +0 -156
- package/dist/utils/span-extractor.js.map +0 -1
- package/dist/wrap-http.d.ts +0 -55
- package/dist/wrap-http.d.ts.map +0 -1
- package/dist/wrap-http.js +0 -135
- package/dist/wrap-http.js.map +0 -1
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Extracts structured data from spans for PingOps backend
|
|
3
|
-
*/
|
|
4
|
-
import { filterHeaders, extractHeadersFromAttributes, } from "../filtering/header-filter";
|
|
5
|
-
/**
|
|
6
|
-
* Extracts domain from URL
|
|
7
|
-
*/
|
|
8
|
-
function extractDomainFromUrl(url) {
|
|
9
|
-
try {
|
|
10
|
-
const urlObj = new URL(url);
|
|
11
|
-
return urlObj.hostname;
|
|
12
|
-
}
|
|
13
|
-
catch {
|
|
14
|
-
const match = url.match(/^(?:https?:\/\/)?([^/]+)/);
|
|
15
|
-
return match ? match[1] : "";
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Gets domain rule configuration for a given URL
|
|
20
|
-
*/
|
|
21
|
-
function getDomainRule(url, domainAllowList) {
|
|
22
|
-
if (!domainAllowList) {
|
|
23
|
-
return undefined;
|
|
24
|
-
}
|
|
25
|
-
const domain = extractDomainFromUrl(url);
|
|
26
|
-
for (const rule of domainAllowList) {
|
|
27
|
-
if (domain === rule.domain ||
|
|
28
|
-
domain.endsWith(`.${rule.domain}`) ||
|
|
29
|
-
domain === rule.domain.slice(1)) {
|
|
30
|
-
return rule;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return undefined;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Determines if body should be captured based on priority:
|
|
37
|
-
* domain rule > global config > default (false)
|
|
38
|
-
*/
|
|
39
|
-
function shouldCaptureBody(domainRule, globalConfig, bodyType) {
|
|
40
|
-
// Check domain-specific rule first
|
|
41
|
-
if (domainRule) {
|
|
42
|
-
const domainValue = bodyType === "request"
|
|
43
|
-
? domainRule.captureRequestBody
|
|
44
|
-
: domainRule.captureResponseBody;
|
|
45
|
-
if (domainValue !== undefined) {
|
|
46
|
-
return domainValue;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
// Fall back to global config
|
|
50
|
-
if (globalConfig !== undefined) {
|
|
51
|
-
return globalConfig;
|
|
52
|
-
}
|
|
53
|
-
// Default to false
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Extracts structured payload from a span
|
|
58
|
-
*/
|
|
59
|
-
export function extractSpanPayload(span, domainAllowList, globalHeadersAllowList, globalHeadersDenyList, globalCaptureRequestBody, globalCaptureResponseBody) {
|
|
60
|
-
const attributes = span.attributes;
|
|
61
|
-
const url = attributes["http.url"] || attributes["url.full"];
|
|
62
|
-
// Get domain-specific rule if available
|
|
63
|
-
const domainRule = url ? getDomainRule(url, domainAllowList) : undefined;
|
|
64
|
-
// Merge global and domain-specific header rules
|
|
65
|
-
const headersAllowList = domainRule?.headersAllowList ?? globalHeadersAllowList;
|
|
66
|
-
const headersDenyList = domainRule?.headersDenyList ?? globalHeadersDenyList;
|
|
67
|
-
// Determine if bodies should be captured
|
|
68
|
-
const shouldCaptureReqBody = shouldCaptureBody(domainRule, globalCaptureRequestBody, "request");
|
|
69
|
-
const shouldCaptureRespBody = shouldCaptureBody(domainRule, globalCaptureResponseBody, "response");
|
|
70
|
-
// Extract HTTP headers if available
|
|
71
|
-
let requestHeaders = {};
|
|
72
|
-
let responseHeaders = {};
|
|
73
|
-
// First, try to extract flat array format headers (e.g., 'http.request.header.0', 'http.request.header.1')
|
|
74
|
-
const flatRequestHeaders = extractHeadersFromAttributes(attributes, "http.request.header");
|
|
75
|
-
const flatResponseHeaders = extractHeadersFromAttributes(attributes, "http.response.header");
|
|
76
|
-
// Try to get headers from attributes (format may vary by instrumentation)
|
|
77
|
-
const httpRequestHeadersValue = attributes["http.request.header"];
|
|
78
|
-
const httpResponseHeadersValue = attributes["http.response.header"];
|
|
79
|
-
// Type guard: check if value is a record/object with string keys
|
|
80
|
-
const isHeadersRecord = (value) => {
|
|
81
|
-
return (typeof value === "object" &&
|
|
82
|
-
value !== null &&
|
|
83
|
-
!Array.isArray(value) &&
|
|
84
|
-
Object.values(value).every((v) => typeof v === "string" ||
|
|
85
|
-
(Array.isArray(v) && v.every((item) => typeof item === "string")) ||
|
|
86
|
-
v === undefined));
|
|
87
|
-
};
|
|
88
|
-
// Use flat array format if available, otherwise use direct attribute
|
|
89
|
-
if (flatRequestHeaders) {
|
|
90
|
-
requestHeaders = filterHeaders(flatRequestHeaders, headersAllowList, headersDenyList);
|
|
91
|
-
}
|
|
92
|
-
else if (isHeadersRecord(httpRequestHeadersValue)) {
|
|
93
|
-
requestHeaders = filterHeaders(httpRequestHeadersValue, headersAllowList, headersDenyList);
|
|
94
|
-
}
|
|
95
|
-
if (flatResponseHeaders) {
|
|
96
|
-
responseHeaders = filterHeaders(flatResponseHeaders, headersAllowList, headersDenyList);
|
|
97
|
-
}
|
|
98
|
-
else if (isHeadersRecord(httpResponseHeadersValue)) {
|
|
99
|
-
responseHeaders = filterHeaders(httpResponseHeadersValue, headersAllowList, headersDenyList);
|
|
100
|
-
}
|
|
101
|
-
// Build attributes object
|
|
102
|
-
const extractedAttributes = {
|
|
103
|
-
...attributes,
|
|
104
|
-
};
|
|
105
|
-
// Remove flat array format headers (e.g., 'http.request.header.0', 'http.request.header.1', etc.)
|
|
106
|
-
// We'll replace them with the proper key-value format
|
|
107
|
-
for (const key in extractedAttributes) {
|
|
108
|
-
if ((key.startsWith("http.request.header.") &&
|
|
109
|
-
key !== "http.request.header") ||
|
|
110
|
-
(key.startsWith("http.response.header.") &&
|
|
111
|
-
key !== "http.response.header")) {
|
|
112
|
-
// Check if it's a numeric index (flat array format)
|
|
113
|
-
const match = key.match(/^(http\.(?:request|response)\.header)\.(\d+)$/);
|
|
114
|
-
if (match) {
|
|
115
|
-
delete extractedAttributes[key];
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
// Add filtered headers in proper key-value format
|
|
120
|
-
if (Object.keys(requestHeaders).length > 0) {
|
|
121
|
-
extractedAttributes["http.request.header"] = requestHeaders;
|
|
122
|
-
}
|
|
123
|
-
if (Object.keys(responseHeaders).length > 0) {
|
|
124
|
-
extractedAttributes["http.response.header"] = responseHeaders;
|
|
125
|
-
}
|
|
126
|
-
// Remove body attributes if capture is disabled
|
|
127
|
-
if (!shouldCaptureReqBody) {
|
|
128
|
-
delete extractedAttributes["http.request.body"];
|
|
129
|
-
}
|
|
130
|
-
if (!shouldCaptureRespBody) {
|
|
131
|
-
delete extractedAttributes["http.response.body"];
|
|
132
|
-
}
|
|
133
|
-
// Build span payload
|
|
134
|
-
const spanContext = span.spanContext();
|
|
135
|
-
// parentSpanId may not be available in all versions of ReadableSpan
|
|
136
|
-
const parentSpanId = "parentSpanId" in span
|
|
137
|
-
? span.parentSpanId
|
|
138
|
-
: undefined;
|
|
139
|
-
return {
|
|
140
|
-
traceId: spanContext.traceId,
|
|
141
|
-
spanId: spanContext.spanId,
|
|
142
|
-
parentSpanId,
|
|
143
|
-
name: span.name,
|
|
144
|
-
kind: span.kind.toString(),
|
|
145
|
-
startTime: new Date(span.startTime[0] * 1000 + span.startTime[1] / 1000000).toISOString(),
|
|
146
|
-
endTime: new Date(span.endTime[0] * 1000 + span.endTime[1] / 1000000).toISOString(),
|
|
147
|
-
duration: (span.endTime[0] - span.startTime[0]) * 1000 +
|
|
148
|
-
(span.endTime[1] - span.startTime[1]) / 1000000,
|
|
149
|
-
attributes: extractedAttributes,
|
|
150
|
-
status: {
|
|
151
|
-
code: span.status.code.toString(),
|
|
152
|
-
message: span.status.message,
|
|
153
|
-
},
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
//# sourceMappingURL=span-extractor.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"span-extractor.js","sourceRoot":"","sources":["../../src/utils/span-extractor.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,EACL,a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,IAAI,CAAC,QAAQ,EAAE;QAC1B,SAAS,EAAE,IAAI,IAAI,CACjB,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,OAAO,CACvD,CAAC,WAAW,EAAE;QACf,OAAO,EAAE,IAAI,IAAI,CACf,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CACnD,CAAC,WAAW,EAAE;QACf,QAAQ,EACN,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI;YAC5C,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO;QACjD,UAAU,EAAE,mBAAmB;QAC/B,MAAM,EAAE;YACN,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE;YACjC,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,OAAO;SAC7B;KACF,CAAC;AACJ,CAAC"}
|
package/dist/wrap-http.d.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* wrapHttp - Wraps a function to set attributes on HTTP spans created within the wrapped block.
|
|
3
|
-
*
|
|
4
|
-
* This function sets attributes (userId, sessionId, tags, metadata) in the OpenTelemetry
|
|
5
|
-
* context, which are automatically propagated to all spans created within the wrapped function.
|
|
6
|
-
*
|
|
7
|
-
* Instrumentation behavior:
|
|
8
|
-
* - If `initializePingops` was called: All HTTP requests are instrumented by default.
|
|
9
|
-
* `wrapHttp` only adds attributes to spans created within the wrapped block.
|
|
10
|
-
* - If `initializePingops` was NOT called: Only HTTP requests within `wrapHttp` blocks
|
|
11
|
-
* are instrumented. Requests outside `wrapHttp` are not instrumented.
|
|
12
|
-
*/
|
|
13
|
-
import type { WrapHttpAttributes } from "./types";
|
|
14
|
-
/**
|
|
15
|
-
* Options for wrapHttp function
|
|
16
|
-
*/
|
|
17
|
-
export interface WrapHttpOptions {
|
|
18
|
-
attributes?: WrapHttpAttributes;
|
|
19
|
-
/**
|
|
20
|
-
* Callback to check if SDK is initialized.
|
|
21
|
-
* Required to determine if global instrumentation is enabled.
|
|
22
|
-
*/
|
|
23
|
-
checkInitialized: () => boolean;
|
|
24
|
-
/**
|
|
25
|
-
* Callback to check if global instrumentation is enabled.
|
|
26
|
-
* Required to determine instrumentation behavior.
|
|
27
|
-
*/
|
|
28
|
-
isGlobalInstrumentationEnabled: () => boolean;
|
|
29
|
-
/**
|
|
30
|
-
* Optional callback to ensure SDK is initialized (auto-initialization).
|
|
31
|
-
* If not provided, wrapHttp will try to auto-initialize from environment variables.
|
|
32
|
-
*/
|
|
33
|
-
ensureInitialized?: () => Promise<void>;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Wraps a function to set attributes on HTTP spans created within the wrapped block.
|
|
37
|
-
*
|
|
38
|
-
* This function sets attributes (userId, sessionId, tags, metadata) in the OpenTelemetry
|
|
39
|
-
* context, which are automatically propagated to all spans created within the wrapped function.
|
|
40
|
-
*
|
|
41
|
-
* Instrumentation behavior:
|
|
42
|
-
* - If `initializePingops` was called: All HTTP requests are instrumented by default.
|
|
43
|
-
* `wrapHttp` only adds attributes to spans created within the wrapped block.
|
|
44
|
-
* - If `initializePingops` was NOT called: Only HTTP requests within `wrapHttp` blocks
|
|
45
|
-
* are instrumented. Requests outside `wrapHttp` are not instrumented.
|
|
46
|
-
*
|
|
47
|
-
* Note: This is the low-level API. For a simpler API with automatic setup,
|
|
48
|
-
* use `wrapHttp` from `@pingops/sdk` instead.
|
|
49
|
-
*
|
|
50
|
-
* @param options - Options including attributes and required callbacks
|
|
51
|
-
* @param fn - Function to execute within the attribute context
|
|
52
|
-
* @returns The result of the function
|
|
53
|
-
*/
|
|
54
|
-
export declare function wrapHttp<T>(options: WrapHttpOptions, fn: () => T | Promise<T>): T | Promise<T>;
|
|
55
|
-
//# sourceMappingURL=wrap-http.d.ts.map
|
package/dist/wrap-http.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"wrap-http.d.ts","sourceRoot":"","sources":["../src/wrap-http.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAaH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAIlD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,UAAU,CAAC,EAAE,kBAAkB,CAAC;IAChC;;;OAGG;IACH,gBAAgB,EAAE,MAAM,OAAO,CAAC;IAChC;;;OAGG;IACH,8BAA8B,EAAE,MAAM,OAAO,CAAC;IAC9C;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CACzC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EACxB,OAAO,EAAE,eAAe,EACxB,EAAE,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GACvB,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAoDhB"}
|
package/dist/wrap-http.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* wrapHttp - Wraps a function to set attributes on HTTP spans created within the wrapped block.
|
|
3
|
-
*
|
|
4
|
-
* This function sets attributes (userId, sessionId, tags, metadata) in the OpenTelemetry
|
|
5
|
-
* context, which are automatically propagated to all spans created within the wrapped function.
|
|
6
|
-
*
|
|
7
|
-
* Instrumentation behavior:
|
|
8
|
-
* - If `initializePingops` was called: All HTTP requests are instrumented by default.
|
|
9
|
-
* `wrapHttp` only adds attributes to spans created within the wrapped block.
|
|
10
|
-
* - If `initializePingops` was NOT called: Only HTTP requests within `wrapHttp` blocks
|
|
11
|
-
* are instrumented. Requests outside `wrapHttp` are not instrumented.
|
|
12
|
-
*/
|
|
13
|
-
import { context } from "@opentelemetry/api";
|
|
14
|
-
import { createLogger } from "./logger";
|
|
15
|
-
import { PINGOPS_HTTP_ENABLED, PINGOPS_USER_ID, PINGOPS_SESSION_ID, PINGOPS_TAGS, PINGOPS_METADATA, PINGOPS_CAPTURE_REQUEST_BODY, PINGOPS_CAPTURE_RESPONSE_BODY, } from "./context-keys";
|
|
16
|
-
const logger = createLogger("[PingOps wrapHttp]");
|
|
17
|
-
/**
|
|
18
|
-
* Wraps a function to set attributes on HTTP spans created within the wrapped block.
|
|
19
|
-
*
|
|
20
|
-
* This function sets attributes (userId, sessionId, tags, metadata) in the OpenTelemetry
|
|
21
|
-
* context, which are automatically propagated to all spans created within the wrapped function.
|
|
22
|
-
*
|
|
23
|
-
* Instrumentation behavior:
|
|
24
|
-
* - If `initializePingops` was called: All HTTP requests are instrumented by default.
|
|
25
|
-
* `wrapHttp` only adds attributes to spans created within the wrapped block.
|
|
26
|
-
* - If `initializePingops` was NOT called: Only HTTP requests within `wrapHttp` blocks
|
|
27
|
-
* are instrumented. Requests outside `wrapHttp` are not instrumented.
|
|
28
|
-
*
|
|
29
|
-
* Note: This is the low-level API. For a simpler API with automatic setup,
|
|
30
|
-
* use `wrapHttp` from `@pingops/sdk` instead.
|
|
31
|
-
*
|
|
32
|
-
* @param options - Options including attributes and required callbacks
|
|
33
|
-
* @param fn - Function to execute within the attribute context
|
|
34
|
-
* @returns The result of the function
|
|
35
|
-
*/
|
|
36
|
-
export function wrapHttp(options, fn) {
|
|
37
|
-
logger.debug("wrapHttp called", {
|
|
38
|
-
hasAttributes: !!options.attributes,
|
|
39
|
-
hasUserId: !!options.attributes?.userId,
|
|
40
|
-
hasSessionId: !!options.attributes?.sessionId,
|
|
41
|
-
hasTags: !!options.attributes?.tags,
|
|
42
|
-
hasMetadata: !!options.attributes?.metadata,
|
|
43
|
-
});
|
|
44
|
-
// Normalize options - if just attributes provided, it means callbacks are required
|
|
45
|
-
// This is a type error at compile time, but we handle it gracefully
|
|
46
|
-
const normalizedOptions = "checkInitialized" in options && "isGlobalInstrumentationEnabled" in options
|
|
47
|
-
? options
|
|
48
|
-
: (() => {
|
|
49
|
-
throw new Error("wrapHttp requires checkInitialized and isGlobalInstrumentationEnabled callbacks. Use wrapHttp from @pingops/sdk for automatic setup.");
|
|
50
|
-
})();
|
|
51
|
-
const { checkInitialized, ensureInitialized } = normalizedOptions;
|
|
52
|
-
// Ensure SDK is initialized so that span processor can extract attributes
|
|
53
|
-
// If already initialized, execute synchronously
|
|
54
|
-
if (checkInitialized()) {
|
|
55
|
-
logger.debug("SDK already initialized, executing wrapHttp synchronously");
|
|
56
|
-
return executeWrapHttpWithContext(normalizedOptions, fn);
|
|
57
|
-
}
|
|
58
|
-
// If not initialized, we need to initialize first (async)
|
|
59
|
-
if (ensureInitialized) {
|
|
60
|
-
logger.debug("SDK not initialized, using provided ensureInitialized callback");
|
|
61
|
-
return ensureInitialized()
|
|
62
|
-
.then(() => {
|
|
63
|
-
logger.debug("SDK initialized, executing wrapHttp");
|
|
64
|
-
return executeWrapHttpWithContext(normalizedOptions, fn);
|
|
65
|
-
})
|
|
66
|
-
.catch((error) => {
|
|
67
|
-
logger.error("Failed to initialize SDK for wrapHttp", {
|
|
68
|
-
error: error instanceof Error ? error.message : String(error),
|
|
69
|
-
});
|
|
70
|
-
throw error;
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
// No ensureInitialized callback provided, execute without initialization
|
|
74
|
-
logger.debug("SDK not initialized and no ensureInitialized callback provided, executing wrapHttp");
|
|
75
|
-
return executeWrapHttpWithContext(normalizedOptions, fn);
|
|
76
|
-
}
|
|
77
|
-
function executeWrapHttpWithContext(options, fn) {
|
|
78
|
-
const { attributes, isGlobalInstrumentationEnabled } = options;
|
|
79
|
-
const globalInstrumentationEnabled = isGlobalInstrumentationEnabled();
|
|
80
|
-
logger.debug("Executing wrapHttp context", {
|
|
81
|
-
hasAttributes: !!attributes,
|
|
82
|
-
globalInstrumentationEnabled,
|
|
83
|
-
});
|
|
84
|
-
const activeContext = context.active();
|
|
85
|
-
// If global instrumentation is not enabled, enable HTTP instrumentation
|
|
86
|
-
// for this block only. If it's enabled, all requests are already instrumented.
|
|
87
|
-
let contextWithAttributes = activeContext;
|
|
88
|
-
if (!globalInstrumentationEnabled) {
|
|
89
|
-
contextWithAttributes = contextWithAttributes.setValue(PINGOPS_HTTP_ENABLED, true);
|
|
90
|
-
}
|
|
91
|
-
// Set attributes in context if provided
|
|
92
|
-
// These will be propagated to all spans created within the wrapped function
|
|
93
|
-
if (attributes) {
|
|
94
|
-
if (attributes.userId !== undefined) {
|
|
95
|
-
contextWithAttributes = contextWithAttributes.setValue(PINGOPS_USER_ID, attributes.userId);
|
|
96
|
-
}
|
|
97
|
-
if (attributes.sessionId !== undefined) {
|
|
98
|
-
contextWithAttributes = contextWithAttributes.setValue(PINGOPS_SESSION_ID, attributes.sessionId);
|
|
99
|
-
}
|
|
100
|
-
if (attributes.tags !== undefined) {
|
|
101
|
-
contextWithAttributes = contextWithAttributes.setValue(PINGOPS_TAGS, attributes.tags);
|
|
102
|
-
}
|
|
103
|
-
if (attributes.metadata !== undefined) {
|
|
104
|
-
contextWithAttributes = contextWithAttributes.setValue(PINGOPS_METADATA, attributes.metadata);
|
|
105
|
-
}
|
|
106
|
-
if (attributes.captureRequestBody !== undefined) {
|
|
107
|
-
contextWithAttributes = contextWithAttributes.setValue(PINGOPS_CAPTURE_REQUEST_BODY, attributes.captureRequestBody);
|
|
108
|
-
}
|
|
109
|
-
if (attributes.captureResponseBody !== undefined) {
|
|
110
|
-
contextWithAttributes = contextWithAttributes.setValue(PINGOPS_CAPTURE_RESPONSE_BODY, attributes.captureResponseBody);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
// Run user code inside the context with attributes
|
|
114
|
-
return context.with(contextWithAttributes, () => {
|
|
115
|
-
try {
|
|
116
|
-
const result = fn();
|
|
117
|
-
if (result instanceof Promise) {
|
|
118
|
-
return result.catch((err) => {
|
|
119
|
-
logger.error("Error in wrapHttp async execution", {
|
|
120
|
-
error: err instanceof Error ? err.message : String(err),
|
|
121
|
-
});
|
|
122
|
-
throw err;
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
return result;
|
|
126
|
-
}
|
|
127
|
-
catch (err) {
|
|
128
|
-
logger.error("Error in wrapHttp sync execution", {
|
|
129
|
-
error: err instanceof Error ? err.message : String(err),
|
|
130
|
-
});
|
|
131
|
-
throw err;
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
//# sourceMappingURL=wrap-http.js.map
|
package/dist/wrap-http.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"wrap-http.js","sourceRoot":"","sources":["../src/wrap-http.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EACL,oBAAoB,EACpB,eAAe,EACf,kBAAkB,EAClB,YAAY,EACZ,gBAAgB,EAChB,4BAA4B,EAC5B,6BAA6B,GAC9B,MAAM,gBAAgB,CAAC;AAGxB,MAAM,MAAM,GAAG,YAAY,CAAC,oBAAoB,CAAC,CAAC;AAwBlD;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,QAAQ,CACtB,OAAwB,EACxB,EAAwB;IAExB,MAAM,CAAC,KAAK,CAAC,iBAAiB,EAAE;QAC9B,aAAa,EAAE,CAAC,CAAC,OAAO,CAAC,UAAU;QACnC,SAAS,EAAE,CAAC,CAAC,OAAO,CAAC,UAAU,EAAE,MAAM;QACvC,YAAY,EAAE,CAAC,CAAC,OAAO,CAAC,UAAU,EAAE,SAAS;QAC7C,OAAO,EAAE,CAAC,CAAC,OAAO,CAAC,UAAU,EAAE,IAAI;Q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