@brizz/sdk 0.1.4 → 0.1.6

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.d.cts CHANGED
@@ -88,6 +88,7 @@ interface IBrizzInitializeOptions {
88
88
  apiKey?: string;
89
89
  headers?: Record<string, string>;
90
90
  disableBatch?: boolean;
91
+ environment?: string;
91
92
  masking?: boolean | IMaskingConfig;
92
93
  logLevel?: LogLevel | string;
93
94
  instrumentModules?: IInstrumentModules;
@@ -116,6 +117,12 @@ declare function emitEvent(name: string, attributes?: Record<string, string | nu
116
117
  declare function getSpanExporter(): SpanExporter;
117
118
  declare function getSpanProcessor(): SpanProcessor;
118
119
 
120
+ declare function callWithProperties<A extends unknown[], F extends (...args: A) => ReturnType<F>>(properties: {
121
+ [name: string]: string;
122
+ }, fn: F, thisArg?: ThisParameterType<F>, ...args: A): ReturnType<F>;
123
+ declare function withProperties<F extends (this: any, ...args: any[]) => any>(properties: {
124
+ [name: string]: string;
125
+ }, fn: F, thisArg?: ThisParameterType<F>): F;
119
126
  declare function withSessionId<F extends (this: any, ...args: any[]) => any>(sessionId: string, fn: F, thisArg?: ThisParameterType<F>): F;
120
127
  declare function callWithSessionId<A extends unknown[], F extends (...args: A) => ReturnType<F>>(sessionId: string, fn: F, thisArg?: ThisParameterType<F>, ...args: A): ReturnType<F>;
121
128
 
@@ -135,4 +142,4 @@ declare namespace init {
135
142
  export { };
136
143
  }
137
144
 
138
- export { Brizz, DEFAULT_PII_PATTERNS, type IAttributesMaskingRule, type IBrizzInitializeOptions, type IEventMaskingConfig, type IEventMaskingRule, type IInstrumentModules, type ILogMaskingConfig, type ILogger, type IMaskingConfig, type ISpanMaskingConfig, LogLevel, type MaskingMode, type RuntimeInfo, callWithSessionId, detectRuntime, emitEvent, getLogLevel, getMetricsExporter, getMetricsReader, getSpanExporter, getSpanProcessor, init, logger, maskAttributes, maskValue, setLogLevel, withSessionId };
145
+ export { Brizz, DEFAULT_PII_PATTERNS, type IAttributesMaskingRule, type IBrizzInitializeOptions, type IEventMaskingConfig, type IEventMaskingRule, type IInstrumentModules, type ILogMaskingConfig, type ILogger, type IMaskingConfig, type ISpanMaskingConfig, LogLevel, type MaskingMode, type RuntimeInfo, callWithProperties, callWithSessionId, detectRuntime, emitEvent, getLogLevel, getMetricsExporter, getMetricsReader, getSpanExporter, getSpanProcessor, init, logger, maskAttributes, maskValue, setLogLevel, withProperties, withSessionId };
package/dist/index.d.ts CHANGED
@@ -88,6 +88,7 @@ interface IBrizzInitializeOptions {
88
88
  apiKey?: string;
89
89
  headers?: Record<string, string>;
90
90
  disableBatch?: boolean;
91
+ environment?: string;
91
92
  masking?: boolean | IMaskingConfig;
92
93
  logLevel?: LogLevel | string;
93
94
  instrumentModules?: IInstrumentModules;
@@ -116,6 +117,12 @@ declare function emitEvent(name: string, attributes?: Record<string, string | nu
116
117
  declare function getSpanExporter(): SpanExporter;
117
118
  declare function getSpanProcessor(): SpanProcessor;
118
119
 
120
+ declare function callWithProperties<A extends unknown[], F extends (...args: A) => ReturnType<F>>(properties: {
121
+ [name: string]: string;
122
+ }, fn: F, thisArg?: ThisParameterType<F>, ...args: A): ReturnType<F>;
123
+ declare function withProperties<F extends (this: any, ...args: any[]) => any>(properties: {
124
+ [name: string]: string;
125
+ }, fn: F, thisArg?: ThisParameterType<F>): F;
119
126
  declare function withSessionId<F extends (this: any, ...args: any[]) => any>(sessionId: string, fn: F, thisArg?: ThisParameterType<F>): F;
120
127
  declare function callWithSessionId<A extends unknown[], F extends (...args: A) => ReturnType<F>>(sessionId: string, fn: F, thisArg?: ThisParameterType<F>, ...args: A): ReturnType<F>;
121
128
 
@@ -135,4 +142,4 @@ declare namespace init {
135
142
  export { };
136
143
  }
137
144
 
138
- export { Brizz, DEFAULT_PII_PATTERNS, type IAttributesMaskingRule, type IBrizzInitializeOptions, type IEventMaskingConfig, type IEventMaskingRule, type IInstrumentModules, type ILogMaskingConfig, type ILogger, type IMaskingConfig, type ISpanMaskingConfig, LogLevel, type MaskingMode, type RuntimeInfo, callWithSessionId, detectRuntime, emitEvent, getLogLevel, getMetricsExporter, getMetricsReader, getSpanExporter, getSpanProcessor, init, logger, maskAttributes, maskValue, setLogLevel, withSessionId };
145
+ export { Brizz, DEFAULT_PII_PATTERNS, type IAttributesMaskingRule, type IBrizzInitializeOptions, type IEventMaskingConfig, type IEventMaskingRule, type IInstrumentModules, type ILogMaskingConfig, type ILogger, type IMaskingConfig, type ISpanMaskingConfig, LogLevel, type MaskingMode, type RuntimeInfo, callWithProperties, callWithSessionId, detectRuntime, emitEvent, getLogLevel, getMetricsExporter, getMetricsReader, getSpanExporter, getSpanProcessor, init, logger, maskAttributes, maskValue, setLogLevel, withProperties, withSessionId };
package/dist/index.js CHANGED
@@ -280,6 +280,7 @@ function resolveConfig(options) {
280
280
  headers: { ...options.headers },
281
281
  apiKey: process.env["BRIZZ_API_KEY"] || options.apiKey,
282
282
  disableBatch: process.env["BRIZZ_DISABLE_BATCH"] === "true" || !!options.disableBatch,
283
+ environment: process.env["BRIZZ_ENVIRONMENT"] || options.environment,
283
284
  logLevel: resolvedLogLevel,
284
285
  masking: resolvedMasking
285
286
  };
@@ -976,7 +977,7 @@ function maskStringByPattern(value, pattern, mode = "full") {
976
977
  logger.warn("Regex execution failed, skipping masking", error);
977
978
  return value;
978
979
  }
979
- for (const matchInfo of matches.reverse()) {
980
+ for (const matchInfo of matches.toReversed()) {
980
981
  let patternName = "unknown";
981
982
  if (matchInfo.groups) {
982
983
  for (const [groupName, groupValue] of Object.entries(matchInfo.groups)) {
@@ -1222,9 +1223,13 @@ var LoggingModule = class _LoggingModule {
1222
1223
  logger.debug("Creating resource with service name", {
1223
1224
  serviceName: config.appName
1224
1225
  });
1225
- const resource = resourceFromAttributes({
1226
+ const resourceAttributes = {
1226
1227
  "service.name": config.appName
1227
- });
1228
+ };
1229
+ if (config.environment) {
1230
+ resourceAttributes["deployment.environment"] = config.environment;
1231
+ }
1232
+ const resource = resourceFromAttributes(resourceAttributes);
1228
1233
  logger.debug("Creating logger provider with resource");
1229
1234
  this.loggerProvider = new LoggerProvider({
1230
1235
  resource,
@@ -1418,130 +1423,6 @@ import {
1418
1423
  BatchSpanProcessor,
1419
1424
  SimpleSpanProcessor
1420
1425
  } from "@opentelemetry/sdk-trace-base";
1421
-
1422
- // src/internal/trace/transformations/vercel-ai.ts
1423
- import { SpanAttributes } from "@traceloop/ai-semantic-conventions";
1424
- var AI_GENERATE_TEXT_DO_GENERATE = "ai.generateText.doGenerate";
1425
- var AI_STREAM_TEXT_DO_STREAM = "ai.streamText.doStream";
1426
- var HANDLED_SPAN_NAMES = {
1427
- [AI_GENERATE_TEXT_DO_GENERATE]: "gen_ai.chat",
1428
- [AI_STREAM_TEXT_DO_STREAM]: "gen_ai.chat",
1429
- "ai.streamText": "ai.streamText",
1430
- "ai.toolCall": (span) => {
1431
- const toolName = span.attributes["ai.toolCall.name"];
1432
- return `${String(toolName ?? "unknown")}.tool`;
1433
- }
1434
- };
1435
- var AI_RESPONSE_TEXT = "ai.response.text";
1436
- var AI_PROMPT_MESSAGES = "ai.prompt.messages";
1437
- var AI_USAGE_PROMPT_TOKENS = "ai.usage.promptTokens";
1438
- var AI_USAGE_COMPLETION_TOKENS = "ai.usage.completionTokens";
1439
- var AI_MODEL_PROVIDER = "ai.model.provider";
1440
- var transformAiSdkSpanName = (span) => {
1441
- if (span.name in HANDLED_SPAN_NAMES) {
1442
- const handler = HANDLED_SPAN_NAMES[span.name];
1443
- if (typeof handler === "function") {
1444
- span.name = handler(span);
1445
- } else if (handler) {
1446
- span.name = handler;
1447
- }
1448
- }
1449
- };
1450
- var transformResponseText = (attributes) => {
1451
- if (AI_RESPONSE_TEXT in attributes) {
1452
- attributes[`${SpanAttributes.LLM_COMPLETIONS}.0.content`] = attributes[AI_RESPONSE_TEXT];
1453
- attributes[`${SpanAttributes.LLM_COMPLETIONS}.0.role`] = "assistant";
1454
- delete attributes[AI_RESPONSE_TEXT];
1455
- }
1456
- };
1457
- var transformPromptMessages = (attributes) => {
1458
- if (AI_PROMPT_MESSAGES in attributes) {
1459
- try {
1460
- const messages = JSON.parse(attributes[AI_PROMPT_MESSAGES]);
1461
- for (const [index, msg] of messages.entries()) {
1462
- const message = msg;
1463
- logger.debug("Transforming prompt message", { msg: message, type: typeof message.content });
1464
- if (typeof message.content === "string") {
1465
- attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] = message.content;
1466
- } else {
1467
- if (Array.isArray(message.content) && message.content.length > 0) {
1468
- const lastContent = message.content.at(-1);
1469
- if (lastContent?.text) {
1470
- attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] = lastContent.text;
1471
- }
1472
- } else {
1473
- attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] = JSON.stringify(
1474
- message.content
1475
- );
1476
- }
1477
- }
1478
- attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.role`] = message.role;
1479
- }
1480
- delete attributes[AI_PROMPT_MESSAGES];
1481
- } catch (error) {
1482
- logger.debug("Skipping prompt messages transformation because of JSON parsing error", {
1483
- e: error
1484
- });
1485
- }
1486
- }
1487
- };
1488
- var transformPromptTokens = (attributes) => {
1489
- if (AI_USAGE_PROMPT_TOKENS in attributes) {
1490
- attributes[`${SpanAttributes.LLM_USAGE_PROMPT_TOKENS}`] = attributes[AI_USAGE_PROMPT_TOKENS];
1491
- delete attributes[AI_USAGE_PROMPT_TOKENS];
1492
- }
1493
- };
1494
- var transformCompletionTokens = (attributes) => {
1495
- if (AI_USAGE_COMPLETION_TOKENS in attributes) {
1496
- attributes[`${SpanAttributes.LLM_USAGE_COMPLETION_TOKENS}`] = attributes[AI_USAGE_COMPLETION_TOKENS];
1497
- delete attributes[AI_USAGE_COMPLETION_TOKENS];
1498
- }
1499
- };
1500
- var calculateTotalTokens = (attributes) => {
1501
- const promptTokens = attributes[`${SpanAttributes.LLM_USAGE_PROMPT_TOKENS}`];
1502
- const completionTokens = attributes[`${SpanAttributes.LLM_USAGE_COMPLETION_TOKENS}`];
1503
- if (promptTokens && completionTokens) {
1504
- attributes[`${SpanAttributes.LLM_USAGE_TOTAL_TOKENS}`] = Number(promptTokens) + Number(completionTokens);
1505
- }
1506
- };
1507
- var transformVendor = (attributes) => {
1508
- if (AI_MODEL_PROVIDER in attributes) {
1509
- const vendor = attributes[AI_MODEL_PROVIDER];
1510
- attributes[SpanAttributes.LLM_SYSTEM] = vendor && vendor.startsWith("openai") ? "OpenAI" : vendor;
1511
- delete attributes[AI_MODEL_PROVIDER];
1512
- }
1513
- };
1514
- var transformAiSdkAttributes = (attributes) => {
1515
- transformResponseText(attributes);
1516
- transformPromptMessages(attributes);
1517
- transformPromptTokens(attributes);
1518
- transformCompletionTokens(attributes);
1519
- calculateTotalTokens(attributes);
1520
- transformVendor(attributes);
1521
- };
1522
- var shouldHandleSpan = (span) => {
1523
- return span.name in HANDLED_SPAN_NAMES;
1524
- };
1525
- var transformAiSdkSpan = (span) => {
1526
- if (!shouldHandleSpan(span)) {
1527
- logger.debug("Skipping span transformation", { spanName: span.name });
1528
- return;
1529
- }
1530
- for (const key in span.attributes) {
1531
- if (Number.isNaN(span.attributes[key])) {
1532
- span.attributes[key] = 0;
1533
- }
1534
- }
1535
- logger.debug("Transforming AI SDK span", {
1536
- spanName: span.name,
1537
- spanContext: span.spanContext(),
1538
- attributes: span.attributes
1539
- });
1540
- transformAiSdkSpanName(span);
1541
- transformAiSdkAttributes(span.attributes);
1542
- };
1543
-
1544
- // src/internal/trace/processors/span-processor.ts
1545
1426
  var DEFAULT_MASKING_RULES = [
1546
1427
  {
1547
1428
  mode: "partial",
@@ -1584,10 +1465,6 @@ var BrizzSimpleSpanProcessor = class extends SimpleSpanProcessor {
1584
1465
  }
1585
1466
  super.onStart(span, parentContext);
1586
1467
  }
1587
- onEnd(span) {
1588
- transformAiSdkSpan(span);
1589
- super.onEnd(span);
1590
- }
1591
1468
  };
1592
1469
  var BrizzBatchSpanProcessor = class extends BatchSpanProcessor {
1593
1470
  config;
@@ -1608,10 +1485,6 @@ var BrizzBatchSpanProcessor = class extends BatchSpanProcessor {
1608
1485
  }
1609
1486
  super.onStart(span, parentContext);
1610
1487
  }
1611
- onEnd(span) {
1612
- transformAiSdkSpan(span);
1613
- super.onEnd(span);
1614
- }
1615
1488
  };
1616
1489
  function maskSpan(span, config) {
1617
1490
  if (!span.attributes || Object.keys(span.attributes).length === 0) {
@@ -1744,23 +1617,28 @@ function getSpanProcessor() {
1744
1617
 
1745
1618
  // src/internal/trace/session.ts
1746
1619
  import { context as context3 } from "@opentelemetry/api";
1747
- function withProperties(properties, fn, thisArg, ...args) {
1748
- if (Object.keys(properties).length === 0) {
1749
- return fn.apply(thisArg, args);
1750
- }
1751
- const newContext = context3.active().setValue(PROPERTIES_CONTEXT_KEY, properties);
1752
- return context3.with(newContext, fn, thisArg, ...args);
1620
+ function callWithProperties(properties, fn, thisArg, ...args) {
1621
+ const base = context3.active();
1622
+ const prev = base.getValue(PROPERTIES_CONTEXT_KEY);
1623
+ const merged = prev ? { ...prev, ...properties } : properties;
1624
+ const next = base.setValue(PROPERTIES_CONTEXT_KEY, merged);
1625
+ return context3.with(next, fn, thisArg, ...args);
1753
1626
  }
1754
- function withSessionId(sessionId, fn, thisArg) {
1627
+ function withProperties(properties, fn, thisArg) {
1755
1628
  return function wrapped(...args) {
1756
- const base = context3.active();
1757
- const prev = base.getValue(PROPERTIES_CONTEXT_KEY);
1758
- const next = base.setValue(PROPERTIES_CONTEXT_KEY, prev ? { ...prev, [SESSION_ID]: sessionId } : { [SESSION_ID]: sessionId });
1759
- return context3.with(next, fn, thisArg ?? this, ...args);
1629
+ return callWithProperties(
1630
+ properties,
1631
+ fn,
1632
+ thisArg !== void 0 ? thisArg : this,
1633
+ ...args
1634
+ );
1760
1635
  };
1761
1636
  }
1637
+ function withSessionId(sessionId, fn, thisArg) {
1638
+ return withProperties({ [SESSION_ID]: sessionId }, fn, thisArg);
1639
+ }
1762
1640
  function callWithSessionId(sessionId, fn, thisArg, ...args) {
1763
- return withProperties({ [SESSION_ID]: sessionId }, fn, thisArg, ...args);
1641
+ return callWithProperties({ [SESSION_ID]: sessionId }, fn, thisArg, ...args);
1764
1642
  }
1765
1643
 
1766
1644
  // src/internal/sdk.ts
@@ -1833,12 +1711,16 @@ var _Brizz = class __Brizz {
1833
1711
  }
1834
1712
  const registry = InstrumentationRegistry.getInstance();
1835
1713
  const manualInstrumentations = registry.getManualInstrumentations();
1714
+ const resourceAttributes = {
1715
+ "service.name": resolvedConfig.appName
1716
+ };
1717
+ if (resolvedConfig.environment) {
1718
+ resourceAttributes["deployment.environment"] = resolvedConfig.environment;
1719
+ }
1836
1720
  this._sdk = new NodeSDK({
1837
1721
  spanProcessors: [getSpanProcessor()],
1838
1722
  metricReader: getMetricsReader(),
1839
- resource: resourceFromAttributes2({
1840
- "service.name": resolvedConfig.appName
1841
- }),
1723
+ resource: resourceFromAttributes2(resourceAttributes),
1842
1724
  instrumentations: manualInstrumentations
1843
1725
  });
1844
1726
  this._sdk.start();
@@ -1962,7 +1844,10 @@ function detectRuntime() {
1962
1844
  const isESM = noNodeJSGlobals && noModuleSystem;
1963
1845
  const isCJS = hasRequire && typeof module !== "undefined" && typeof exports !== "undefined" && typeof __filename === "string" && typeof __dirname === "string";
1964
1846
  const supportsLoaderAPI = (major ?? 0) >= 21 || (major ?? 0) === 20 && (minor ?? 0) >= 6 || (major ?? 0) === 18 && (minor ?? 0) >= 19;
1965
- const supportsRegister = !!process.features?.typescript || !!globalThis.module?.register;
1847
+ const supportsRegister = (
1848
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1849
+ !!process.features?.typescript || !!globalThis.module?.register
1850
+ );
1966
1851
  logger.debug("Runtime detection results:", {
1967
1852
  nodeVersion: `${major ?? 0}.${minor ?? 0}`,
1968
1853
  isESM,
@@ -1983,6 +1868,7 @@ function detectRuntime() {
1983
1868
  module: typeof module,
1984
1869
  exports: typeof exports,
1985
1870
  "process.features": process.features,
1871
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1986
1872
  "globalThis.module": globalThis.module
1987
1873
  }
1988
1874
  });
@@ -2002,6 +1888,7 @@ export {
2002
1888
  DEFAULT_PII_PATTERNS,
2003
1889
  LogLevel,
2004
1890
  SeverityNumber2 as SeverityNumber,
1891
+ callWithProperties,
2005
1892
  callWithSessionId,
2006
1893
  detectRuntime,
2007
1894
  emitEvent,
@@ -2015,6 +1902,7 @@ export {
2015
1902
  maskAttributes,
2016
1903
  maskValue,
2017
1904
  setLogLevel,
1905
+ withProperties,
2018
1906
  withSessionId
2019
1907
  };
2020
1908
  //# sourceMappingURL=index.js.map