@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.cjs CHANGED
@@ -206,6 +206,7 @@ function resolveConfig(options) {
206
206
  headers: { ...options.headers },
207
207
  apiKey: process.env["BRIZZ_API_KEY"] || options.apiKey,
208
208
  disableBatch: process.env["BRIZZ_DISABLE_BATCH"] === "true" || !!options.disableBatch,
209
+ environment: process.env["BRIZZ_ENVIRONMENT"] || options.environment,
209
210
  logLevel: resolvedLogLevel,
210
211
  masking: resolvedMasking
211
212
  };
@@ -900,7 +901,7 @@ function maskStringByPattern(value, pattern, mode = "full") {
900
901
  logger.warn("Regex execution failed, skipping masking", error);
901
902
  return value;
902
903
  }
903
- for (const matchInfo of matches.reverse()) {
904
+ for (const matchInfo of matches.toReversed()) {
904
905
  let patternName = "unknown";
905
906
  if (matchInfo.groups) {
906
907
  for (const [groupName, groupValue] of Object.entries(matchInfo.groups)) {
@@ -1145,9 +1146,13 @@ var LoggingModule = class _LoggingModule {
1145
1146
  logger.debug("Creating resource with service name", {
1146
1147
  serviceName: config.appName
1147
1148
  });
1148
- const resource = (0, import_resources.resourceFromAttributes)({
1149
+ const resourceAttributes = {
1149
1150
  "service.name": config.appName
1150
- });
1151
+ };
1152
+ if (config.environment) {
1153
+ resourceAttributes["deployment.environment"] = config.environment;
1154
+ }
1155
+ const resource = (0, import_resources.resourceFromAttributes)(resourceAttributes);
1151
1156
  logger.debug("Creating logger provider with resource");
1152
1157
  this.loggerProvider = new import_sdk_logs2.LoggerProvider({
1153
1158
  resource,
@@ -1332,130 +1337,6 @@ var import_exporter_trace_otlp_http = require("@opentelemetry/exporter-trace-otl
1332
1337
  // src/internal/trace/processors/span-processor.ts
1333
1338
  var import_api4 = require("@opentelemetry/api");
1334
1339
  var import_sdk_trace_base = require("@opentelemetry/sdk-trace-base");
1335
-
1336
- // src/internal/trace/transformations/vercel-ai.ts
1337
- var import_ai_semantic_conventions = require("@traceloop/ai-semantic-conventions");
1338
- var AI_GENERATE_TEXT_DO_GENERATE = "ai.generateText.doGenerate";
1339
- var AI_STREAM_TEXT_DO_STREAM = "ai.streamText.doStream";
1340
- var HANDLED_SPAN_NAMES = {
1341
- [AI_GENERATE_TEXT_DO_GENERATE]: "gen_ai.chat",
1342
- [AI_STREAM_TEXT_DO_STREAM]: "gen_ai.chat",
1343
- "ai.streamText": "ai.streamText",
1344
- "ai.toolCall": (span) => {
1345
- const toolName = span.attributes["ai.toolCall.name"];
1346
- return `${String(toolName ?? "unknown")}.tool`;
1347
- }
1348
- };
1349
- var AI_RESPONSE_TEXT = "ai.response.text";
1350
- var AI_PROMPT_MESSAGES = "ai.prompt.messages";
1351
- var AI_USAGE_PROMPT_TOKENS = "ai.usage.promptTokens";
1352
- var AI_USAGE_COMPLETION_TOKENS = "ai.usage.completionTokens";
1353
- var AI_MODEL_PROVIDER = "ai.model.provider";
1354
- var transformAiSdkSpanName = (span) => {
1355
- if (span.name in HANDLED_SPAN_NAMES) {
1356
- const handler = HANDLED_SPAN_NAMES[span.name];
1357
- if (typeof handler === "function") {
1358
- span.name = handler(span);
1359
- } else if (handler) {
1360
- span.name = handler;
1361
- }
1362
- }
1363
- };
1364
- var transformResponseText = (attributes) => {
1365
- if (AI_RESPONSE_TEXT in attributes) {
1366
- attributes[`${import_ai_semantic_conventions.SpanAttributes.LLM_COMPLETIONS}.0.content`] = attributes[AI_RESPONSE_TEXT];
1367
- attributes[`${import_ai_semantic_conventions.SpanAttributes.LLM_COMPLETIONS}.0.role`] = "assistant";
1368
- delete attributes[AI_RESPONSE_TEXT];
1369
- }
1370
- };
1371
- var transformPromptMessages = (attributes) => {
1372
- if (AI_PROMPT_MESSAGES in attributes) {
1373
- try {
1374
- const messages = JSON.parse(attributes[AI_PROMPT_MESSAGES]);
1375
- for (const [index, msg] of messages.entries()) {
1376
- const message = msg;
1377
- logger.debug("Transforming prompt message", { msg: message, type: typeof message.content });
1378
- if (typeof message.content === "string") {
1379
- attributes[`${import_ai_semantic_conventions.SpanAttributes.LLM_PROMPTS}.${index}.content`] = message.content;
1380
- } else {
1381
- if (Array.isArray(message.content) && message.content.length > 0) {
1382
- const lastContent = message.content.at(-1);
1383
- if (lastContent?.text) {
1384
- attributes[`${import_ai_semantic_conventions.SpanAttributes.LLM_PROMPTS}.${index}.content`] = lastContent.text;
1385
- }
1386
- } else {
1387
- attributes[`${import_ai_semantic_conventions.SpanAttributes.LLM_PROMPTS}.${index}.content`] = JSON.stringify(
1388
- message.content
1389
- );
1390
- }
1391
- }
1392
- attributes[`${import_ai_semantic_conventions.SpanAttributes.LLM_PROMPTS}.${index}.role`] = message.role;
1393
- }
1394
- delete attributes[AI_PROMPT_MESSAGES];
1395
- } catch (error) {
1396
- logger.debug("Skipping prompt messages transformation because of JSON parsing error", {
1397
- e: error
1398
- });
1399
- }
1400
- }
1401
- };
1402
- var transformPromptTokens = (attributes) => {
1403
- if (AI_USAGE_PROMPT_TOKENS in attributes) {
1404
- attributes[`${import_ai_semantic_conventions.SpanAttributes.LLM_USAGE_PROMPT_TOKENS}`] = attributes[AI_USAGE_PROMPT_TOKENS];
1405
- delete attributes[AI_USAGE_PROMPT_TOKENS];
1406
- }
1407
- };
1408
- var transformCompletionTokens = (attributes) => {
1409
- if (AI_USAGE_COMPLETION_TOKENS in attributes) {
1410
- attributes[`${import_ai_semantic_conventions.SpanAttributes.LLM_USAGE_COMPLETION_TOKENS}`] = attributes[AI_USAGE_COMPLETION_TOKENS];
1411
- delete attributes[AI_USAGE_COMPLETION_TOKENS];
1412
- }
1413
- };
1414
- var calculateTotalTokens = (attributes) => {
1415
- const promptTokens = attributes[`${import_ai_semantic_conventions.SpanAttributes.LLM_USAGE_PROMPT_TOKENS}`];
1416
- const completionTokens = attributes[`${import_ai_semantic_conventions.SpanAttributes.LLM_USAGE_COMPLETION_TOKENS}`];
1417
- if (promptTokens && completionTokens) {
1418
- attributes[`${import_ai_semantic_conventions.SpanAttributes.LLM_USAGE_TOTAL_TOKENS}`] = Number(promptTokens) + Number(completionTokens);
1419
- }
1420
- };
1421
- var transformVendor = (attributes) => {
1422
- if (AI_MODEL_PROVIDER in attributes) {
1423
- const vendor = attributes[AI_MODEL_PROVIDER];
1424
- attributes[import_ai_semantic_conventions.SpanAttributes.LLM_SYSTEM] = vendor && vendor.startsWith("openai") ? "OpenAI" : vendor;
1425
- delete attributes[AI_MODEL_PROVIDER];
1426
- }
1427
- };
1428
- var transformAiSdkAttributes = (attributes) => {
1429
- transformResponseText(attributes);
1430
- transformPromptMessages(attributes);
1431
- transformPromptTokens(attributes);
1432
- transformCompletionTokens(attributes);
1433
- calculateTotalTokens(attributes);
1434
- transformVendor(attributes);
1435
- };
1436
- var shouldHandleSpan = (span) => {
1437
- return span.name in HANDLED_SPAN_NAMES;
1438
- };
1439
- var transformAiSdkSpan = (span) => {
1440
- if (!shouldHandleSpan(span)) {
1441
- logger.debug("Skipping span transformation", { spanName: span.name });
1442
- return;
1443
- }
1444
- for (const key in span.attributes) {
1445
- if (Number.isNaN(span.attributes[key])) {
1446
- span.attributes[key] = 0;
1447
- }
1448
- }
1449
- logger.debug("Transforming AI SDK span", {
1450
- spanName: span.name,
1451
- spanContext: span.spanContext(),
1452
- attributes: span.attributes
1453
- });
1454
- transformAiSdkSpanName(span);
1455
- transformAiSdkAttributes(span.attributes);
1456
- };
1457
-
1458
- // src/internal/trace/processors/span-processor.ts
1459
1340
  var DEFAULT_MASKING_RULES = [
1460
1341
  {
1461
1342
  mode: "partial",
@@ -1498,10 +1379,6 @@ var BrizzSimpleSpanProcessor = class extends import_sdk_trace_base.SimpleSpanPro
1498
1379
  }
1499
1380
  super.onStart(span, parentContext);
1500
1381
  }
1501
- onEnd(span) {
1502
- transformAiSdkSpan(span);
1503
- super.onEnd(span);
1504
- }
1505
1382
  };
1506
1383
  var BrizzBatchSpanProcessor = class extends import_sdk_trace_base.BatchSpanProcessor {
1507
1384
  config;
@@ -1522,10 +1399,6 @@ var BrizzBatchSpanProcessor = class extends import_sdk_trace_base.BatchSpanProce
1522
1399
  }
1523
1400
  super.onStart(span, parentContext);
1524
1401
  }
1525
- onEnd(span) {
1526
- transformAiSdkSpan(span);
1527
- super.onEnd(span);
1528
- }
1529
1402
  };
1530
1403
  function maskSpan(span, config) {
1531
1404
  if (!span.attributes || Object.keys(span.attributes).length === 0) {
@@ -1726,12 +1599,16 @@ var _Brizz = class __Brizz {
1726
1599
  }
1727
1600
  const registry = InstrumentationRegistry.getInstance();
1728
1601
  const manualInstrumentations = registry.getManualInstrumentations();
1602
+ const resourceAttributes = {
1603
+ "service.name": resolvedConfig.appName
1604
+ };
1605
+ if (resolvedConfig.environment) {
1606
+ resourceAttributes["deployment.environment"] = resolvedConfig.environment;
1607
+ }
1729
1608
  this._sdk = new import_sdk_node.NodeSDK({
1730
1609
  spanProcessors: [getSpanProcessor()],
1731
1610
  metricReader: getMetricsReader(),
1732
- resource: (0, import_resources2.resourceFromAttributes)({
1733
- "service.name": resolvedConfig.appName
1734
- }),
1611
+ resource: (0, import_resources2.resourceFromAttributes)(resourceAttributes),
1735
1612
  instrumentations: manualInstrumentations
1736
1613
  });
1737
1614
  this._sdk.start();
@@ -1934,7 +1811,10 @@ function detectRuntime() {
1934
1811
  const isESM = noNodeJSGlobals && noModuleSystem;
1935
1812
  const isCJS = hasRequire && typeof module !== "undefined" && typeof exports !== "undefined" && typeof __filename === "string" && typeof __dirname === "string";
1936
1813
  const supportsLoaderAPI = (major ?? 0) >= 21 || (major ?? 0) === 20 && (minor ?? 0) >= 6 || (major ?? 0) === 18 && (minor ?? 0) >= 19;
1937
- const supportsRegister = !!process.features?.typescript || !!globalThis.module?.register;
1814
+ const supportsRegister = (
1815
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1816
+ !!process.features?.typescript || !!globalThis.module?.register
1817
+ );
1938
1818
  logger.debug("Runtime detection results:", {
1939
1819
  nodeVersion: `${major ?? 0}.${minor ?? 0}`,
1940
1820
  isESM,
@@ -1955,6 +1835,7 @@ function detectRuntime() {
1955
1835
  module: typeof module,
1956
1836
  exports: typeof exports,
1957
1837
  "process.features": process.features,
1838
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1958
1839
  "globalThis.module": globalThis.module
1959
1840
  }
1960
1841
  });
@@ -1977,6 +1858,7 @@ try {
1977
1858
  apiKey: process.env["BRIZZ_API_KEY"],
1978
1859
  baseUrl: process.env["BRIZZ_BASE_URL"],
1979
1860
  appName: process.env["BRIZZ_APP_NAME"] || process.env["OTEL_SERVICE_NAME"],
1861
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1980
1862
  logLevel: process.env["BRIZZ_LOG_LEVEL"] || DEFAULT_LOG_LEVEL.toString(),
1981
1863
  // Enable auto-instrumentation by default in preload mode
1982
1864
  disableNodeSdk: false