@redthreadlabs/tracelog-client 1.6.0 → 1.8.0

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.
@@ -13,6 +13,13 @@ export declare class LogClient {
13
13
  event(type?: string): EventBuilder;
14
14
  startPerf(name: string, parent?: PerfToken): PerfToken;
15
15
  endPerf(token: PerfToken, context?: Record<string, JsonValue>): void;
16
+ /**
17
+ * Record a complete perf measurement in one call — for operations timed
18
+ * elsewhere (a pre-measured duration with no live start/end pair). Buffers a
19
+ * parent-less, root perf, which the server maps to a `transaction` record.
20
+ * Reach for startPerf/endPerf instead when you need a live span tree.
21
+ */
22
+ recordPerf(name: string, durationMs: number, context?: Record<string, JsonValue>, outcome?: 'success' | 'failure' | 'unknown'): void;
16
23
  flush(): Promise<void>;
17
24
  dispose(): void;
18
25
  private _enqueueEvent;
package/dist/LogClient.js CHANGED
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LogClient = void 0;
4
4
  const EventBuilder_1 = require("./EventBuilder");
5
5
  const util_1 = require("./util");
6
+ // Level ordering for the optional getMinLevel gate. An event is dropped when
7
+ // its level ranks below the host-supplied minimum.
8
+ const LEVEL_RANK = { debug: 0, info: 1, warn: 2, error: 3 };
6
9
  // Defaults (all overridable via LogClientOptions)
7
10
  const DEFAULT_FLUSH_CADENCE_MS = 5000;
8
11
  const DEFAULT_MAX_BUFFER_SIZE = 100;
@@ -89,6 +92,40 @@ class LogClient {
89
92
  this.flush();
90
93
  }
91
94
  }
95
+ /**
96
+ * Record a complete perf measurement in one call — for operations timed
97
+ * elsewhere (a pre-measured duration with no live start/end pair). Buffers a
98
+ * parent-less, root perf, which the server maps to a `transaction` record.
99
+ * Reach for startPerf/endPerf instead when you need a live span tree.
100
+ */
101
+ recordPerf(name, durationMs, context, outcome = 'success') {
102
+ if (this._disposed)
103
+ return;
104
+ if (!isFinite(durationMs) || durationMs < 0)
105
+ durationMs = 0;
106
+ const id = randomHex(16);
107
+ const end = now();
108
+ const perf = {
109
+ id,
110
+ trace_id: randomHex(32),
111
+ root_id: id,
112
+ name,
113
+ type: 'client-perf',
114
+ // No live start time, so back-compute it from the measured duration.
115
+ timestamp: Math.round(end - durationMs),
116
+ duration: Math.round(durationMs),
117
+ outcome,
118
+ tz_offset: (0, util_1.tzOffsetMinutes)(),
119
+ };
120
+ if (context && Object.keys(context).length > 0) {
121
+ perf.context = { tags: context };
122
+ }
123
+ this._perfBuffer.push(perf);
124
+ this._schedulePersist();
125
+ if (this._perfBuffer.length + this._eventBuffer.length >= (this._opts.maxBufferSize ?? DEFAULT_MAX_BUFFER_SIZE)) {
126
+ this.flush();
127
+ }
128
+ }
92
129
  // ---- Transport ----
93
130
  async flush() {
94
131
  if (this._disposed || this._flushing)
@@ -125,6 +162,11 @@ class LogClient {
125
162
  _enqueueEvent(event) {
126
163
  if (this._disposed)
127
164
  return;
165
+ // Level gate: drop events below the host-supplied minimum before they ever
166
+ // buffer, persist, or ship. No callback ⇒ emit everything (back-compat).
167
+ const minLevel = this._opts.getMinLevel?.();
168
+ if (minLevel && LEVEL_RANK[event.level] < LEVEL_RANK[minLevel])
169
+ return;
128
170
  this._eventBuffer.push(event);
129
171
  this._schedulePersist();
130
172
  if (this._eventBuffer.length >= (this._opts.maxBufferSize ?? DEFAULT_MAX_BUFFER_SIZE)) {
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ClientInfo } from '@redthreadlabs/tracelog-schema';
1
+ import type { ClientInfo, LogLevel } from '@redthreadlabs/tracelog-schema';
2
2
  export type { JsonValue, LogLevel, LogBatch, LogEventItem, LogPerfItem, ClientInfo, } from '@redthreadlabs/tracelog-schema';
3
3
  export interface PerfToken {
4
4
  /** 16-char hex ID for this perf */
@@ -23,6 +23,13 @@ export interface LogClientOptions {
23
23
  getSessionRef?: () => string | undefined;
24
24
  /** Returns native device identifier */
25
25
  getDeviceId?: () => string | undefined;
26
+ /**
27
+ * Returns the minimum level to emit. Events whose level ranks below this
28
+ * (debug < info < warn < error) are dropped before buffering — they never
29
+ * persist or ship. Called once per event, so keep it cheap and synchronous.
30
+ * Omit to emit every level (default). Perfs are unaffected.
31
+ */
32
+ getMinLevel?: () => LogLevel;
26
33
  /** Flush cadence in ms. Default: 5000 */
27
34
  flushCadenceMs?: number;
28
35
  /** Max events buffered before forced flush. Default: 100 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redthreadlabs/tracelog-client",
3
- "version": "1.6.0",
3
+ "version": "1.8.0",
4
4
  "description": "Lightweight logging client for tracelog — works in React Native and browsers",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/",