@brizz/sdk 0.1.4 → 0.1.5

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/preload.js CHANGED
@@ -189,6 +189,7 @@ function resolveConfig(options) {
189
189
  headers: { ...options.headers },
190
190
  apiKey: process.env["BRIZZ_API_KEY"] || options.apiKey,
191
191
  disableBatch: process.env["BRIZZ_DISABLE_BATCH"] === "true" || !!options.disableBatch,
192
+ environment: process.env["BRIZZ_ENVIRONMENT"] || options.environment,
192
193
  logLevel: resolvedLogLevel,
193
194
  masking: resolvedMasking
194
195
  };
@@ -885,7 +886,7 @@ function maskStringByPattern(value, pattern, mode = "full") {
885
886
  logger.warn("Regex execution failed, skipping masking", error);
886
887
  return value;
887
888
  }
888
- for (const matchInfo of matches.reverse()) {
889
+ for (const matchInfo of matches.toReversed()) {
889
890
  let patternName = "unknown";
890
891
  if (matchInfo.groups) {
891
892
  for (const [groupName, groupValue] of Object.entries(matchInfo.groups)) {
@@ -1130,9 +1131,13 @@ var LoggingModule = class _LoggingModule {
1130
1131
  logger.debug("Creating resource with service name", {
1131
1132
  serviceName: config.appName
1132
1133
  });
1133
- const resource = resourceFromAttributes({
1134
+ const resourceAttributes = {
1134
1135
  "service.name": config.appName
1135
- });
1136
+ };
1137
+ if (config.environment) {
1138
+ resourceAttributes["deployment.environment"] = config.environment;
1139
+ }
1140
+ const resource = resourceFromAttributes(resourceAttributes);
1136
1141
  logger.debug("Creating logger provider with resource");
1137
1142
  this.loggerProvider = new LoggerProvider({
1138
1143
  resource,
@@ -1320,130 +1325,6 @@ import {
1320
1325
  BatchSpanProcessor,
1321
1326
  SimpleSpanProcessor
1322
1327
  } from "@opentelemetry/sdk-trace-base";
1323
-
1324
- // src/internal/trace/transformations/vercel-ai.ts
1325
- import { SpanAttributes } from "@traceloop/ai-semantic-conventions";
1326
- var AI_GENERATE_TEXT_DO_GENERATE = "ai.generateText.doGenerate";
1327
- var AI_STREAM_TEXT_DO_STREAM = "ai.streamText.doStream";
1328
- var HANDLED_SPAN_NAMES = {
1329
- [AI_GENERATE_TEXT_DO_GENERATE]: "gen_ai.chat",
1330
- [AI_STREAM_TEXT_DO_STREAM]: "gen_ai.chat",
1331
- "ai.streamText": "ai.streamText",
1332
- "ai.toolCall": (span) => {
1333
- const toolName = span.attributes["ai.toolCall.name"];
1334
- return `${String(toolName ?? "unknown")}.tool`;
1335
- }
1336
- };
1337
- var AI_RESPONSE_TEXT = "ai.response.text";
1338
- var AI_PROMPT_MESSAGES = "ai.prompt.messages";
1339
- var AI_USAGE_PROMPT_TOKENS = "ai.usage.promptTokens";
1340
- var AI_USAGE_COMPLETION_TOKENS = "ai.usage.completionTokens";
1341
- var AI_MODEL_PROVIDER = "ai.model.provider";
1342
- var transformAiSdkSpanName = (span) => {
1343
- if (span.name in HANDLED_SPAN_NAMES) {
1344
- const handler = HANDLED_SPAN_NAMES[span.name];
1345
- if (typeof handler === "function") {
1346
- span.name = handler(span);
1347
- } else if (handler) {
1348
- span.name = handler;
1349
- }
1350
- }
1351
- };
1352
- var transformResponseText = (attributes) => {
1353
- if (AI_RESPONSE_TEXT in attributes) {
1354
- attributes[`${SpanAttributes.LLM_COMPLETIONS}.0.content`] = attributes[AI_RESPONSE_TEXT];
1355
- attributes[`${SpanAttributes.LLM_COMPLETIONS}.0.role`] = "assistant";
1356
- delete attributes[AI_RESPONSE_TEXT];
1357
- }
1358
- };
1359
- var transformPromptMessages = (attributes) => {
1360
- if (AI_PROMPT_MESSAGES in attributes) {
1361
- try {
1362
- const messages = JSON.parse(attributes[AI_PROMPT_MESSAGES]);
1363
- for (const [index, msg] of messages.entries()) {
1364
- const message = msg;
1365
- logger.debug("Transforming prompt message", { msg: message, type: typeof message.content });
1366
- if (typeof message.content === "string") {
1367
- attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] = message.content;
1368
- } else {
1369
- if (Array.isArray(message.content) && message.content.length > 0) {
1370
- const lastContent = message.content.at(-1);
1371
- if (lastContent?.text) {
1372
- attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] = lastContent.text;
1373
- }
1374
- } else {
1375
- attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.content`] = JSON.stringify(
1376
- message.content
1377
- );
1378
- }
1379
- }
1380
- attributes[`${SpanAttributes.LLM_PROMPTS}.${index}.role`] = message.role;
1381
- }
1382
- delete attributes[AI_PROMPT_MESSAGES];
1383
- } catch (error) {
1384
- logger.debug("Skipping prompt messages transformation because of JSON parsing error", {
1385
- e: error
1386
- });
1387
- }
1388
- }
1389
- };
1390
- var transformPromptTokens = (attributes) => {
1391
- if (AI_USAGE_PROMPT_TOKENS in attributes) {
1392
- attributes[`${SpanAttributes.LLM_USAGE_PROMPT_TOKENS}`] = attributes[AI_USAGE_PROMPT_TOKENS];
1393
- delete attributes[AI_USAGE_PROMPT_TOKENS];
1394
- }
1395
- };
1396
- var transformCompletionTokens = (attributes) => {
1397
- if (AI_USAGE_COMPLETION_TOKENS in attributes) {
1398
- attributes[`${SpanAttributes.LLM_USAGE_COMPLETION_TOKENS}`] = attributes[AI_USAGE_COMPLETION_TOKENS];
1399
- delete attributes[AI_USAGE_COMPLETION_TOKENS];
1400
- }
1401
- };
1402
- var calculateTotalTokens = (attributes) => {
1403
- const promptTokens = attributes[`${SpanAttributes.LLM_USAGE_PROMPT_TOKENS}`];
1404
- const completionTokens = attributes[`${SpanAttributes.LLM_USAGE_COMPLETION_TOKENS}`];
1405
- if (promptTokens && completionTokens) {
1406
- attributes[`${SpanAttributes.LLM_USAGE_TOTAL_TOKENS}`] = Number(promptTokens) + Number(completionTokens);
1407
- }
1408
- };
1409
- var transformVendor = (attributes) => {
1410
- if (AI_MODEL_PROVIDER in attributes) {
1411
- const vendor = attributes[AI_MODEL_PROVIDER];
1412
- attributes[SpanAttributes.LLM_SYSTEM] = vendor && vendor.startsWith("openai") ? "OpenAI" : vendor;
1413
- delete attributes[AI_MODEL_PROVIDER];
1414
- }
1415
- };
1416
- var transformAiSdkAttributes = (attributes) => {
1417
- transformResponseText(attributes);
1418
- transformPromptMessages(attributes);
1419
- transformPromptTokens(attributes);
1420
- transformCompletionTokens(attributes);
1421
- calculateTotalTokens(attributes);
1422
- transformVendor(attributes);
1423
- };
1424
- var shouldHandleSpan = (span) => {
1425
- return span.name in HANDLED_SPAN_NAMES;
1426
- };
1427
- var transformAiSdkSpan = (span) => {
1428
- if (!shouldHandleSpan(span)) {
1429
- logger.debug("Skipping span transformation", { spanName: span.name });
1430
- return;
1431
- }
1432
- for (const key in span.attributes) {
1433
- if (Number.isNaN(span.attributes[key])) {
1434
- span.attributes[key] = 0;
1435
- }
1436
- }
1437
- logger.debug("Transforming AI SDK span", {
1438
- spanName: span.name,
1439
- spanContext: span.spanContext(),
1440
- attributes: span.attributes
1441
- });
1442
- transformAiSdkSpanName(span);
1443
- transformAiSdkAttributes(span.attributes);
1444
- };
1445
-
1446
- // src/internal/trace/processors/span-processor.ts
1447
1328
  var DEFAULT_MASKING_RULES = [
1448
1329
  {
1449
1330
  mode: "partial",
@@ -1486,10 +1367,6 @@ var BrizzSimpleSpanProcessor = class extends SimpleSpanProcessor {
1486
1367
  }
1487
1368
  super.onStart(span, parentContext);
1488
1369
  }
1489
- onEnd(span) {
1490
- transformAiSdkSpan(span);
1491
- super.onEnd(span);
1492
- }
1493
1370
  };
1494
1371
  var BrizzBatchSpanProcessor = class extends BatchSpanProcessor {
1495
1372
  config;
@@ -1510,10 +1387,6 @@ var BrizzBatchSpanProcessor = class extends BatchSpanProcessor {
1510
1387
  }
1511
1388
  super.onStart(span, parentContext);
1512
1389
  }
1513
- onEnd(span) {
1514
- transformAiSdkSpan(span);
1515
- super.onEnd(span);
1516
- }
1517
1390
  };
1518
1391
  function maskSpan(span, config) {
1519
1392
  if (!span.attributes || Object.keys(span.attributes).length === 0) {
@@ -1714,12 +1587,16 @@ var _Brizz = class __Brizz {
1714
1587
  }
1715
1588
  const registry = InstrumentationRegistry.getInstance();
1716
1589
  const manualInstrumentations = registry.getManualInstrumentations();
1590
+ const resourceAttributes = {
1591
+ "service.name": resolvedConfig.appName
1592
+ };
1593
+ if (resolvedConfig.environment) {
1594
+ resourceAttributes["deployment.environment"] = resolvedConfig.environment;
1595
+ }
1717
1596
  this._sdk = new NodeSDK({
1718
1597
  spanProcessors: [getSpanProcessor()],
1719
1598
  metricReader: getMetricsReader(),
1720
- resource: resourceFromAttributes2({
1721
- "service.name": resolvedConfig.appName
1722
- }),
1599
+ resource: resourceFromAttributes2(resourceAttributes),
1723
1600
  instrumentations: manualInstrumentations
1724
1601
  });
1725
1602
  this._sdk.start();
@@ -1921,7 +1798,10 @@ function detectRuntime() {
1921
1798
  const isESM = noNodeJSGlobals && noModuleSystem;
1922
1799
  const isCJS = hasRequire && typeof module !== "undefined" && typeof exports !== "undefined" && typeof __filename === "string" && typeof __dirname === "string";
1923
1800
  const supportsLoaderAPI = (major ?? 0) >= 21 || (major ?? 0) === 20 && (minor ?? 0) >= 6 || (major ?? 0) === 18 && (minor ?? 0) >= 19;
1924
- const supportsRegister = !!process.features?.typescript || !!globalThis.module?.register;
1801
+ const supportsRegister = (
1802
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1803
+ !!process.features?.typescript || !!globalThis.module?.register
1804
+ );
1925
1805
  logger.debug("Runtime detection results:", {
1926
1806
  nodeVersion: `${major ?? 0}.${minor ?? 0}`,
1927
1807
  isESM,
@@ -1942,6 +1822,7 @@ function detectRuntime() {
1942
1822
  module: typeof module,
1943
1823
  exports: typeof exports,
1944
1824
  "process.features": process.features,
1825
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1945
1826
  "globalThis.module": globalThis.module
1946
1827
  }
1947
1828
  });
@@ -1964,6 +1845,7 @@ try {
1964
1845
  apiKey: process.env["BRIZZ_API_KEY"],
1965
1846
  baseUrl: process.env["BRIZZ_BASE_URL"],
1966
1847
  appName: process.env["BRIZZ_APP_NAME"] || process.env["OTEL_SERVICE_NAME"],
1848
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1967
1849
  logLevel: process.env["BRIZZ_LOG_LEVEL"] || DEFAULT_LOG_LEVEL.toString(),
1968
1850
  // Enable auto-instrumentation by default in preload mode
1969
1851
  disableNodeSdk: false