@loglayer/mixin-datadog-http-metrics 1.0.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.
package/dist/index.mjs ADDED
@@ -0,0 +1,539 @@
1
+ import metrics from "datadog-metrics";
2
+
3
+ //#region src/client.ts
4
+ let metricsClient = null;
5
+ /**
6
+ * Set the datadog-metrics client for the mixin.
7
+ * Called during mixin registration.
8
+ *
9
+ * @param client - The BufferedMetricsLogger instance
10
+ */
11
+ function setMetricsClient(client) {
12
+ metricsClient = client;
13
+ }
14
+ /**
15
+ * Check if the client is null (no-op mode)
16
+ *
17
+ * @returns True if the client is null, false otherwise
18
+ */
19
+ function isNoOpClient() {
20
+ return metricsClient === null;
21
+ }
22
+ /**
23
+ * Get the datadog-metrics client.
24
+ * Returns the configured client or null if not configured.
25
+ *
26
+ * @returns The BufferedMetricsLogger instance or null
27
+ */
28
+ function getMetricsClient() {
29
+ return metricsClient;
30
+ }
31
+
32
+ //#endregion
33
+ //#region ../../core/plugin/dist/index.js
34
+ /**
35
+ * List of plugin callbacks that can be called by the plugin manager.
36
+ *
37
+ * @see {@link https://loglayer.dev/plugins/creating-plugins.html#transformloglevel | transformLogLevel Docs}
38
+ * @see {@link https://loglayer.dev/plugins/creating-plugins.html#onbeforedataout | onBeforeDataOut Docs}
39
+ * @see {@link https://loglayer.dev/plugins/creating-plugins.html#shouldsendtologger | shouldSendToLogger Docs}
40
+ * @see {@link https://loglayer.dev/plugins/creating-plugins.html#onmetadatacalled | onMetadataCalled Docs}
41
+ * @see {@link https://loglayer.dev/plugins/creating-plugins.html#onbeforemessageout | onBeforeMessageOut Docs}
42
+ * @see {@link https://loglayer.dev/plugins/creating-plugins.html#oncontextcalled | onContextCalled Docs}
43
+ */
44
+ let PluginCallbackType = /* @__PURE__ */ function(PluginCallbackType) {
45
+ PluginCallbackType["transformLogLevel"] = "transformLogLevel";
46
+ PluginCallbackType["onBeforeDataOut"] = "onBeforeDataOut";
47
+ PluginCallbackType["shouldSendToLogger"] = "shouldSendToLogger";
48
+ PluginCallbackType["onMetadataCalled"] = "onMetadataCalled";
49
+ PluginCallbackType["onBeforeMessageOut"] = "onBeforeMessageOut";
50
+ PluginCallbackType["onContextCalled"] = "onContextCalled";
51
+ return PluginCallbackType;
52
+ }({});
53
+
54
+ //#endregion
55
+ //#region ../../core/loglayer/dist/index.js
56
+ const CALLBACK_LIST = [
57
+ PluginCallbackType.onBeforeDataOut,
58
+ PluginCallbackType.onMetadataCalled,
59
+ PluginCallbackType.onBeforeMessageOut,
60
+ PluginCallbackType.transformLogLevel,
61
+ PluginCallbackType.shouldSendToLogger,
62
+ PluginCallbackType.onContextCalled
63
+ ];
64
+ /**
65
+ * The class that the mixin extends
66
+ */
67
+ let LogLayerMixinAugmentType = /* @__PURE__ */ function(LogLayerMixinAugmentType) {
68
+ /**
69
+ * Mixin extends the LogBuilder prototype
70
+ */
71
+ LogLayerMixinAugmentType["LogBuilder"] = "LogBuilder";
72
+ /**
73
+ * Mixin extends the LogLayer prototype
74
+ */
75
+ LogLayerMixinAugmentType["LogLayer"] = "LogLayer";
76
+ return LogLayerMixinAugmentType;
77
+ }({});
78
+
79
+ //#endregion
80
+ //#region src/CommonMetricsBuilder.ts
81
+ /**
82
+ * Base class for common metrics builder functionality.
83
+ * Provides shared methods for configuring tags and timestamp.
84
+ */
85
+ var CommonMetricsBuilder = class {
86
+ /** Tags to attach to the metric */
87
+ tags;
88
+ /** Timestamp in milliseconds since epoch */
89
+ timestamp;
90
+ /** The datadog-metrics client instance */
91
+ client;
92
+ /**
93
+ * Creates a new CommonMetricsBuilder instance.
94
+ *
95
+ * @param client - The datadog-metrics BufferedMetricsLogger instance
96
+ */
97
+ constructor(client) {
98
+ this.client = client;
99
+ }
100
+ /**
101
+ * Add tags to the metric.
102
+ *
103
+ * @param tags - Tags as an array of strings (e.g., ['env:prod', 'service:api'])
104
+ * @returns The builder instance for method chaining
105
+ */
106
+ withTags(tags) {
107
+ this.tags = tags;
108
+ return this;
109
+ }
110
+ /**
111
+ * Set the timestamp for the metric.
112
+ *
113
+ * @param timestamp - Timestamp in milliseconds since epoch
114
+ * @returns The builder instance for method chaining
115
+ */
116
+ withTimestamp(timestamp) {
117
+ this.timestamp = timestamp;
118
+ return this;
119
+ }
120
+ };
121
+
122
+ //#endregion
123
+ //#region src/builders/DistributionBuilder.ts
124
+ /**
125
+ * Builder for the distribution method.
126
+ * Supports chaining with withTags() and withTimestamp().
127
+ */
128
+ var DistributionBuilder = class extends CommonMetricsBuilder {
129
+ /** The metric key */
130
+ key;
131
+ /** The distribution value */
132
+ value;
133
+ /**
134
+ * Creates a new DistributionBuilder instance.
135
+ *
136
+ * @param client - The datadog-metrics BufferedMetricsLogger instance
137
+ * @param key - The metric key to record
138
+ * @param value - The distribution value to record
139
+ */
140
+ constructor(client, key, value) {
141
+ super(client);
142
+ this.key = key;
143
+ this.value = value;
144
+ }
145
+ /**
146
+ * Send the distribution metric with the configured options.
147
+ */
148
+ send() {
149
+ this.client.distribution(this.key, this.value, this.tags, this.timestamp);
150
+ }
151
+ };
152
+
153
+ //#endregion
154
+ //#region src/builders/GaugeBuilder.ts
155
+ /**
156
+ * Builder for the gauge method.
157
+ * Supports chaining with withTags() and withTimestamp().
158
+ */
159
+ var GaugeBuilder = class extends CommonMetricsBuilder {
160
+ /** The metric key */
161
+ key;
162
+ /** The gauge value */
163
+ value;
164
+ /**
165
+ * Creates a new GaugeBuilder instance.
166
+ *
167
+ * @param client - The datadog-metrics BufferedMetricsLogger instance
168
+ * @param key - The metric key to set
169
+ * @param value - The value to set the gauge to
170
+ */
171
+ constructor(client, key, value) {
172
+ super(client);
173
+ this.key = key;
174
+ this.value = value;
175
+ }
176
+ /**
177
+ * Send the gauge metric with the configured options.
178
+ */
179
+ send() {
180
+ this.client.gauge(this.key, this.value, this.tags, this.timestamp);
181
+ }
182
+ };
183
+
184
+ //#endregion
185
+ //#region src/builders/HistogramBuilder.ts
186
+ /**
187
+ * Builder for the histogram method.
188
+ * Supports chaining with withTags(), withTimestamp(), and withHistogramOptions().
189
+ */
190
+ var HistogramBuilder = class extends CommonMetricsBuilder {
191
+ /** The metric key */
192
+ key;
193
+ /** The histogram value */
194
+ value;
195
+ /** Optional histogram-specific options */
196
+ histogramOptions;
197
+ /**
198
+ * Creates a new HistogramBuilder instance.
199
+ *
200
+ * @param client - The datadog-metrics BufferedMetricsLogger instance
201
+ * @param key - The metric key to record
202
+ * @param value - The histogram value to record
203
+ */
204
+ constructor(client, key, value) {
205
+ super(client);
206
+ this.key = key;
207
+ this.value = value;
208
+ }
209
+ /**
210
+ * Set histogram-specific options for aggregation and percentiles.
211
+ *
212
+ * @param options - Histogram options with aggregates and/or percentiles
213
+ * @returns The builder instance for method chaining
214
+ */
215
+ withHistogramOptions(options) {
216
+ this.histogramOptions = options;
217
+ return this;
218
+ }
219
+ /**
220
+ * Send the histogram metric with the configured options.
221
+ */
222
+ send() {
223
+ this.client.histogram(this.key, this.value, this.tags, this.timestamp, this.histogramOptions);
224
+ }
225
+ };
226
+
227
+ //#endregion
228
+ //#region src/builders/IncrementBuilder.ts
229
+ /**
230
+ * Builder for the increment method.
231
+ * Supports chaining with withValue(), withTags(), and withTimestamp().
232
+ */
233
+ var IncrementBuilder = class extends CommonMetricsBuilder {
234
+ /** The metric key */
235
+ key;
236
+ /** The increment value (optional, defaults to 1) */
237
+ value;
238
+ /**
239
+ * Creates a new IncrementBuilder instance.
240
+ *
241
+ * @param client - The datadog-metrics BufferedMetricsLogger instance
242
+ * @param key - The metric key to increment
243
+ */
244
+ constructor(client, key) {
245
+ super(client);
246
+ this.key = key;
247
+ }
248
+ /**
249
+ * Set the increment value.
250
+ *
251
+ * @param value - The value to increment by (defaults to 1 if not specified)
252
+ * @returns The builder instance for method chaining
253
+ */
254
+ withValue(value) {
255
+ this.value = value;
256
+ return this;
257
+ }
258
+ /**
259
+ * Send the increment metric with the configured options.
260
+ */
261
+ send() {
262
+ this.client.increment(this.key, this.value, this.tags, this.timestamp);
263
+ }
264
+ };
265
+
266
+ //#endregion
267
+ //#region src/MetricsAPI.ts
268
+ /**
269
+ * Metrics API implementation that wraps datadog-metrics BufferedMetricsLogger.
270
+ * Provides a fluent interface for sending metrics to Datadog via HTTP.
271
+ */
272
+ var MetricsAPI = class {
273
+ /**
274
+ * Creates a new MetricsAPI instance.
275
+ *
276
+ * @param client - The datadog-metrics BufferedMetricsLogger instance
277
+ */
278
+ constructor(client) {
279
+ this.client = client;
280
+ }
281
+ /**
282
+ * Increment a counter metric.
283
+ * Returns a builder that supports chaining with withValue(), withTags(), and withTimestamp().
284
+ *
285
+ * @param key - The metric key to increment
286
+ * @returns A builder instance for chaining additional options
287
+ */
288
+ increment(key) {
289
+ return new IncrementBuilder(this.client, key);
290
+ }
291
+ /**
292
+ * Set a gauge metric to a specific value.
293
+ * Returns a builder that supports chaining with withTags() and withTimestamp().
294
+ *
295
+ * @param key - The metric key to set
296
+ * @param value - The value to set the gauge to
297
+ * @returns A builder instance for chaining additional options
298
+ */
299
+ gauge(key, value) {
300
+ return new GaugeBuilder(this.client, key, value);
301
+ }
302
+ /**
303
+ * Record a histogram value.
304
+ * Returns a builder that supports chaining with withTags(), withTimestamp(), and withHistogramOptions().
305
+ *
306
+ * @param key - The metric key to record
307
+ * @param value - The histogram value to record
308
+ * @returns A builder instance for chaining additional options
309
+ */
310
+ histogram(key, value) {
311
+ return new HistogramBuilder(this.client, key, value);
312
+ }
313
+ /**
314
+ * Record a distribution value (server-side calculated).
315
+ * Returns a builder that supports chaining with withTags() and withTimestamp().
316
+ *
317
+ * @param key - The metric key to record
318
+ * @param value - The distribution value to record
319
+ * @returns A builder instance for chaining additional options
320
+ */
321
+ distribution(key, value) {
322
+ return new DistributionBuilder(this.client, key, value);
323
+ }
324
+ /**
325
+ * Flush all buffered metrics to Datadog immediately.
326
+ *
327
+ * @returns A promise that resolves when the flush completes
328
+ */
329
+ flush() {
330
+ return this.client.flush();
331
+ }
332
+ /**
333
+ * Start auto-flushing metrics at the configured interval.
334
+ */
335
+ start() {
336
+ this.client.start();
337
+ }
338
+ /**
339
+ * Stop auto-flushing and optionally flush remaining metrics.
340
+ *
341
+ * @param options - Options for stopping. `flush` defaults to true.
342
+ * @returns A promise that resolves when stopping completes
343
+ */
344
+ stop(options) {
345
+ return this.client.stop(options);
346
+ }
347
+ /**
348
+ * Get the underlying BufferedMetricsLogger instance.
349
+ *
350
+ * @returns The BufferedMetricsLogger instance
351
+ */
352
+ getClient() {
353
+ return this.client;
354
+ }
355
+ };
356
+
357
+ //#endregion
358
+ //#region src/MockMetricsAPI.ts
359
+ /**
360
+ * Base class for mock metrics builders.
361
+ * Provides no-op implementations of common builder methods.
362
+ */
363
+ var MockCommonMetricsBuilder = class {
364
+ withTags(_tags) {
365
+ return this;
366
+ }
367
+ withTimestamp(_timestamp) {
368
+ return this;
369
+ }
370
+ send() {}
371
+ };
372
+ /**
373
+ * Mock implementation of IncrementBuilder.
374
+ * Does nothing when methods are called.
375
+ */
376
+ var MockIncrementBuilder = class extends MockCommonMetricsBuilder {
377
+ constructor(_key) {
378
+ super();
379
+ }
380
+ withValue(_value) {
381
+ return this;
382
+ }
383
+ };
384
+ /**
385
+ * Mock implementation of GaugeBuilder.
386
+ * Does nothing when methods are called.
387
+ */
388
+ var MockGaugeBuilder = class extends MockCommonMetricsBuilder {
389
+ constructor(_key, _value) {
390
+ super();
391
+ }
392
+ };
393
+ /**
394
+ * Mock implementation of HistogramBuilder.
395
+ * Does nothing when methods are called.
396
+ */
397
+ var MockHistogramBuilder = class extends MockCommonMetricsBuilder {
398
+ constructor(_key, _value) {
399
+ super();
400
+ }
401
+ withHistogramOptions(_options) {
402
+ return this;
403
+ }
404
+ };
405
+ /**
406
+ * Mock implementation of DistributionBuilder.
407
+ * Does nothing when methods are called.
408
+ */
409
+ var MockDistributionBuilder = class extends MockCommonMetricsBuilder {
410
+ constructor(_key, _value) {
411
+ super();
412
+ }
413
+ };
414
+ /**
415
+ * No-op implementation of IMetricsAPI.
416
+ * All methods return mock builder instances that do nothing when send() is called.
417
+ * This is used when datadogMetricsMixin(null) is called, allowing the metrics API
418
+ * to be used without actually sending any metrics.
419
+ */
420
+ var MockMetricsAPI = class {
421
+ increment(key) {
422
+ return new MockIncrementBuilder(key);
423
+ }
424
+ gauge(key, value) {
425
+ return new MockGaugeBuilder(key, value);
426
+ }
427
+ histogram(key, value) {
428
+ return new MockHistogramBuilder(key, value);
429
+ }
430
+ distribution(key, value) {
431
+ return new MockDistributionBuilder(key, value);
432
+ }
433
+ flush() {
434
+ return Promise.resolve();
435
+ }
436
+ start() {}
437
+ stop(_options) {
438
+ return Promise.resolve();
439
+ }
440
+ getClient() {
441
+ return {
442
+ gauge: () => {},
443
+ increment: () => {},
444
+ histogram: () => {},
445
+ distribution: () => {},
446
+ flush: () => Promise.resolve(),
447
+ start: () => {},
448
+ stop: () => Promise.resolve()
449
+ };
450
+ }
451
+ };
452
+
453
+ //#endregion
454
+ //#region src/LogLayer.augment.ts
455
+ /**
456
+ * LogLayer mixin that adds a `ddStats` property to LogLayer instances.
457
+ * Provides access to the MetricsAPI for sending metrics to Datadog via HTTP.
458
+ */
459
+ const logLayerDatadogMetricsMixin = {
460
+ augmentationType: LogLayerMixinAugmentType.LogLayer,
461
+ augment: (prototype) => {
462
+ Object.defineProperty(prototype, "ddStats", {
463
+ get() {
464
+ if (!this._ddMetricsAPI) {
465
+ const client = getMetricsClient();
466
+ this._ddMetricsAPI = isNoOpClient() || !client ? new MockMetricsAPI() : new MetricsAPI(client);
467
+ }
468
+ return this._ddMetricsAPI;
469
+ },
470
+ enumerable: true,
471
+ configurable: true
472
+ });
473
+ },
474
+ augmentMock: (prototype) => {
475
+ Object.defineProperty(prototype, "ddStats", {
476
+ get() {
477
+ if (!this._ddMetricsAPI) {
478
+ const client = getMetricsClient();
479
+ this._ddMetricsAPI = isNoOpClient() || !client ? new MockMetricsAPI() : new MetricsAPI(client);
480
+ }
481
+ return this._ddMetricsAPI;
482
+ },
483
+ enumerable: true,
484
+ configurable: true
485
+ });
486
+ }
487
+ };
488
+
489
+ //#endregion
490
+ //#region src/index.ts
491
+ const BufferedMetricsLogger = metrics.BufferedMetricsLogger;
492
+ /**
493
+ * A reporter that discards all metrics. Useful for testing or temporarily
494
+ * disabling metric submission without using the `enabled` flag.
495
+ * Re-exported from the `datadog-metrics` library.
496
+ */
497
+ const NullReporter = metrics.reporters.NullReporter;
498
+ /**
499
+ * The default reporter that sends metrics to Datadog's HTTP API with automatic retries.
500
+ * Re-exported from the `datadog-metrics` library.
501
+ */
502
+ const DatadogReporter = metrics.reporters.DatadogReporter;
503
+ /**
504
+ * Register the datadog-metrics mixin with LogLayer.
505
+ * This adds a `ddStats` property to LogLayer instances,
506
+ * providing a fluent API for sending metrics to Datadog via HTTP.
507
+ *
508
+ * @param options - The BufferedMetricsLogger configuration options, or null for no-op mode.
509
+ * Set `enabled: false` to use no-op mode while still passing options.
510
+ * @returns A LogLayer mixin registration object
511
+ *
512
+ * @example
513
+ * ```typescript
514
+ * import { useLogLayerMixin } from 'loglayer';
515
+ * import { datadogMetricsMixin } from '@loglayer/mixin-datadog-http-metrics';
516
+ *
517
+ * useLogLayerMixin(datadogMetricsMixin({
518
+ * apiKey: 'your-api-key',
519
+ * prefix: 'myapp.',
520
+ * enabled: process.env.NODE_ENV === 'production',
521
+ * }));
522
+ * ```
523
+ */
524
+ function datadogMetricsMixin(options) {
525
+ if (options && options.enabled !== false) {
526
+ const { enabled: _, ...metricsOptions } = options;
527
+ setMetricsClient(new metrics.BufferedMetricsLogger({
528
+ flushIntervalSeconds: 5,
529
+ ...metricsOptions
530
+ }));
531
+ } else setMetricsClient(null);
532
+ return {
533
+ mixinsToAdd: [logLayerDatadogMetricsMixin],
534
+ pluginsToAdd: []
535
+ };
536
+ }
537
+
538
+ //#endregion
539
+ export { BufferedMetricsLogger, DatadogReporter, MetricsAPI, MockMetricsAPI, NullReporter, datadogMetricsMixin };
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@loglayer/mixin-datadog-http-metrics",
3
+ "description": "Adds Datadog HTTP metrics functionality to LogLayer using the datadog-metrics library.",
4
+ "version": "1.0.0",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.mjs",
8
+ "exports": {
9
+ "import": {
10
+ "types": "./dist/index.d.mts",
11
+ "import": "./dist/index.mjs"
12
+ },
13
+ "require": {
14
+ "types": "./dist/index.d.cts",
15
+ "require": "./dist/index.cjs"
16
+ }
17
+ },
18
+ "types": "./dist/index.d.mts",
19
+ "sideEffects": false,
20
+ "license": "MIT",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/loglayer/loglayer.git",
24
+ "directory": "packages/mixins/datadog-http-metrics"
25
+ },
26
+ "author": "Theo Gravity <theo@suteki.nu>",
27
+ "keywords": [
28
+ "logging",
29
+ "log",
30
+ "loglayer",
31
+ "observability",
32
+ "datadog",
33
+ "metrics",
34
+ "datadog-metrics"
35
+ ],
36
+ "dependencies": {
37
+ "datadog-metrics": "0.12.1"
38
+ },
39
+ "devDependencies": {
40
+ "@types/node": "25.2.0",
41
+ "tsdown": "0.20.1",
42
+ "typescript": "5.9.3",
43
+ "vitest": "4.0.18",
44
+ "@internal/tsconfig": "2.1.0",
45
+ "loglayer": "8.5.0",
46
+ "@loglayer/shared": "3.3.0"
47
+ },
48
+ "bugs": "https://github.com/loglayer/loglayer/issues",
49
+ "engines": {
50
+ "node": ">=20"
51
+ },
52
+ "files": [
53
+ "dist"
54
+ ],
55
+ "homepage": "https://loglayer.dev",
56
+ "scripts": {
57
+ "build": "tsdown src/index.ts",
58
+ "test": "vitest --run",
59
+ "clean": "rm -rf .turbo node_modules dist",
60
+ "lint": "biome check --no-errors-on-unmatched --write --unsafe src",
61
+ "lint:staged": "biome check --no-errors-on-unmatched --write --unsafe --staged src",
62
+ "livetest": "tsx src/__tests__/livetest.ts",
63
+ "verify-types": "tsc --noEmit"
64
+ }
65
+ }