@reactionary/provider-algolia 0.0.28 → 0.0.30

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.
Files changed (2) hide show
  1. package/index.js +255 -8
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,12 +1,244 @@
1
1
  // core/src/cache/redis-cache.ts
2
2
  import { Redis } from "@upstash/redis";
3
3
 
4
+ // otel/src/trpc-middleware.ts
5
+ import { TRPCError } from "@trpc/server";
6
+ import {
7
+ SpanKind as SpanKind2,
8
+ SpanStatusCode as SpanStatusCode3
9
+ } from "@opentelemetry/api";
10
+
11
+ // otel/src/tracer.ts
12
+ import {
13
+ trace,
14
+ SpanStatusCode,
15
+ context as otelContext
16
+ } from "@opentelemetry/api";
17
+
18
+ // otel/src/sdk.ts
19
+ import { NodeSDK } from "@opentelemetry/sdk-node";
20
+ var sdk = null;
21
+ var isInitialized = false;
22
+ var initializationPromise = null;
23
+ function isBrowser() {
24
+ return typeof window !== "undefined" && typeof process === "undefined";
25
+ }
26
+ function ensureInitialized() {
27
+ if (isInitialized || initializationPromise) {
28
+ return;
29
+ }
30
+ if (isBrowser()) {
31
+ isInitialized = true;
32
+ return;
33
+ }
34
+ initializationPromise = Promise.resolve().then(() => {
35
+ sdk = new NodeSDK();
36
+ sdk.start();
37
+ isInitialized = true;
38
+ process.on("SIGTERM", async () => {
39
+ try {
40
+ await shutdownOtel();
41
+ if (process.env["OTEL_LOG_LEVEL"] === "debug") {
42
+ console.log("OpenTelemetry terminated successfully");
43
+ }
44
+ } catch (error) {
45
+ console.error("Error terminating OpenTelemetry", error);
46
+ }
47
+ });
48
+ });
49
+ }
50
+ async function shutdownOtel() {
51
+ if (sdk) {
52
+ await sdk.shutdown();
53
+ sdk = null;
54
+ isInitialized = false;
55
+ }
56
+ }
57
+ function isOtelInitialized() {
58
+ ensureInitialized();
59
+ return isInitialized;
60
+ }
61
+
62
+ // otel/src/tracer.ts
63
+ import { SpanKind, SpanStatusCode as SpanStatusCode2 } from "@opentelemetry/api";
64
+ var TRACER_NAME = "@reactionary/otel";
65
+ var TRACER_VERSION = "0.0.1";
66
+ var globalTracer = null;
67
+ function getTracer() {
68
+ if (!globalTracer) {
69
+ isOtelInitialized();
70
+ globalTracer = trace.getTracer(TRACER_NAME, TRACER_VERSION);
71
+ }
72
+ return globalTracer;
73
+ }
74
+ function withSpan(name, fn, options) {
75
+ const tracer = getTracer();
76
+ return tracer.startActiveSpan(name, options || {}, async (span) => {
77
+ try {
78
+ const result = await fn(span);
79
+ span.setStatus({ code: SpanStatusCode.OK });
80
+ return result;
81
+ } catch (error) {
82
+ span.setStatus({
83
+ code: SpanStatusCode.ERROR,
84
+ message: error instanceof Error ? error.message : String(error)
85
+ });
86
+ if (error instanceof Error) {
87
+ span.recordException(error);
88
+ }
89
+ throw error;
90
+ } finally {
91
+ span.end();
92
+ }
93
+ });
94
+ }
95
+ function setSpanAttributes(span, attributes) {
96
+ Object.entries(attributes).forEach(([key, value]) => {
97
+ if (value !== void 0 && value !== null) {
98
+ span.setAttribute(key, value);
99
+ }
100
+ });
101
+ }
102
+
103
+ // otel/src/metrics.ts
104
+ import { metrics } from "@opentelemetry/api";
105
+ var METER_NAME = "@reactionary/otel";
106
+ var METER_VERSION = "0.0.1";
107
+ var globalMeter = null;
108
+ function getMeter() {
109
+ if (!globalMeter) {
110
+ isOtelInitialized();
111
+ globalMeter = metrics.getMeter(METER_NAME, METER_VERSION);
112
+ }
113
+ return globalMeter;
114
+ }
115
+ var metricsInstance = null;
116
+ function initializeMetrics() {
117
+ if (metricsInstance) {
118
+ return metricsInstance;
119
+ }
120
+ const meter = getMeter();
121
+ metricsInstance = {
122
+ requestCounter: meter.createCounter("reactionary.requests", {
123
+ description: "Total number of requests"
124
+ }),
125
+ requestDuration: meter.createHistogram("reactionary.request.duration", {
126
+ description: "Request duration in milliseconds",
127
+ unit: "ms"
128
+ }),
129
+ activeRequests: meter.createUpDownCounter("reactionary.requests.active", {
130
+ description: "Number of active requests"
131
+ }),
132
+ errorCounter: meter.createCounter("reactionary.errors", {
133
+ description: "Total number of errors"
134
+ }),
135
+ providerCallCounter: meter.createCounter("reactionary.provider.calls", {
136
+ description: "Total number of provider calls"
137
+ }),
138
+ providerCallDuration: meter.createHistogram("reactionary.provider.duration", {
139
+ description: "Provider call duration in milliseconds",
140
+ unit: "ms"
141
+ }),
142
+ cacheHitCounter: meter.createCounter("reactionary.cache.hits", {
143
+ description: "Total number of cache hits"
144
+ }),
145
+ cacheMissCounter: meter.createCounter("reactionary.cache.misses", {
146
+ description: "Total number of cache misses"
147
+ })
148
+ };
149
+ return metricsInstance;
150
+ }
151
+ function getMetrics() {
152
+ if (!metricsInstance) {
153
+ return initializeMetrics();
154
+ }
155
+ return metricsInstance;
156
+ }
157
+
158
+ // otel/src/provider-instrumentation.ts
159
+ import { SpanKind as SpanKind3 } from "@opentelemetry/api";
160
+ async function withProviderSpan(options, fn) {
161
+ const { providerName, operationType, operationName, attributes = {} } = options;
162
+ const metrics2 = getMetrics();
163
+ const spanName = `provider.${providerName}.${operationType}${operationName ? `.${operationName}` : ""}`;
164
+ const startTime = Date.now();
165
+ metrics2.providerCallCounter.add(1, {
166
+ "provider.name": providerName,
167
+ "provider.operation.type": operationType,
168
+ "provider.operation.name": operationName || "unknown"
169
+ });
170
+ return withSpan(
171
+ spanName,
172
+ async (span) => {
173
+ setSpanAttributes(span, {
174
+ "provider.name": providerName,
175
+ "provider.operation.type": operationType,
176
+ "provider.operation.name": operationName,
177
+ ...attributes
178
+ });
179
+ try {
180
+ const result = await fn(span);
181
+ const duration = Date.now() - startTime;
182
+ metrics2.providerCallDuration.record(duration, {
183
+ "provider.name": providerName,
184
+ "provider.operation.type": operationType,
185
+ "provider.operation.name": operationName || "unknown",
186
+ "status": "success"
187
+ });
188
+ return result;
189
+ } catch (error) {
190
+ const duration = Date.now() - startTime;
191
+ metrics2.providerCallDuration.record(duration, {
192
+ "provider.name": providerName,
193
+ "provider.operation.type": operationType,
194
+ "provider.operation.name": operationName || "unknown",
195
+ "status": "error"
196
+ });
197
+ metrics2.errorCounter.add(1, {
198
+ "provider.name": providerName,
199
+ "provider.operation.type": operationType,
200
+ "provider.operation.name": operationName || "unknown"
201
+ });
202
+ throw error;
203
+ }
204
+ },
205
+ { kind: SpanKind3.CLIENT }
206
+ );
207
+ }
208
+ function createProviderInstrumentation(providerName) {
209
+ return {
210
+ traceQuery: (operationName, fn, attributes) => {
211
+ return withProviderSpan(
212
+ {
213
+ providerName,
214
+ operationType: "query",
215
+ operationName,
216
+ attributes
217
+ },
218
+ fn
219
+ );
220
+ },
221
+ traceMutation: (operationName, fn, attributes) => {
222
+ return withProviderSpan(
223
+ {
224
+ providerName,
225
+ operationType: "mutation",
226
+ operationName,
227
+ attributes
228
+ },
229
+ fn
230
+ );
231
+ }
232
+ };
233
+ }
234
+
4
235
  // core/src/providers/base.provider.ts
5
236
  var BaseProvider = class {
6
237
  constructor(schema, querySchema, mutationSchema) {
7
238
  this.schema = schema;
8
239
  this.querySchema = querySchema;
9
240
  this.mutationSchema = mutationSchema;
241
+ this.instrumentation = createProviderInstrumentation(this.constructor.name);
10
242
  }
11
243
  /**
12
244
  * Validates that the final domain model constructed by the provider
@@ -28,20 +260,35 @@ var BaseProvider = class {
28
260
  * of the results will match the order of the queries.
29
261
  */
30
262
  async query(queries, session) {
31
- const results = await this.fetch(queries, session);
32
- for (const result of results) {
33
- this.assert(result);
34
- }
35
- return results;
263
+ return this.instrumentation.traceQuery(
264
+ "query",
265
+ async (span) => {
266
+ span.setAttribute("provider.query.count", queries.length);
267
+ const results = await this.fetch(queries, session);
268
+ for (const result of results) {
269
+ this.assert(result);
270
+ }
271
+ span.setAttribute("provider.result.count", results.length);
272
+ return results;
273
+ },
274
+ { queryCount: queries.length }
275
+ );
36
276
  }
37
277
  /**
38
278
  * Executes the listed mutations in order and returns the final state
39
279
  * resulting from that set of operations.
40
280
  */
41
281
  async mutate(mutations, session) {
42
- const result = await this.process(mutations, session);
43
- this.assert(result);
44
- return result;
282
+ return this.instrumentation.traceMutation(
283
+ "mutate",
284
+ async (span) => {
285
+ span.setAttribute("provider.mutation.count", mutations.length);
286
+ const result = await this.process(mutations, session);
287
+ this.assert(result);
288
+ return result;
289
+ },
290
+ { mutationCount: mutations.length }
291
+ );
45
292
  }
46
293
  };
47
294
 
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@reactionary/provider-algolia",
3
- "version": "0.0.28",
3
+ "version": "0.0.30",
4
4
  "dependencies": {
5
- "@reactionary/core": "0.0.28",
5
+ "@reactionary/core": "0.0.30",
6
6
  "algoliasearch": "^5.23.4",
7
7
  "zod": "4.0.0-beta.20250430T185432"
8
8
  }