@tinycloud/sdk-services 2.3.0-beta.7 → 2.3.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.
@@ -43,6 +43,7 @@ var defaultRetryPolicy = {
43
43
  retryableErrors: [ErrorCodes.NETWORK_ERROR, ErrorCodes.TIMEOUT]
44
44
  };
45
45
  var TelemetryEvents = {
46
+ SPAN: "telemetry.span",
46
47
  SERVICE_REQUEST: "service.request",
47
48
  SERVICE_RESPONSE: "service.response",
48
49
  SERVICE_ERROR: "service.error",
@@ -183,9 +184,11 @@ var BaseService = class {
183
184
  * @param key - Optional key/path being accessed
184
185
  */
185
186
  emitRequest(action, key) {
187
+ const service = this.getServiceName();
186
188
  this.emit(TelemetryEvents.SERVICE_REQUEST, {
187
- service: this.getServiceName(),
189
+ service,
188
190
  action,
191
+ span: this.spanName(action),
189
192
  key,
190
193
  timestamp: Date.now()
191
194
  });
@@ -199,11 +202,24 @@ var BaseService = class {
199
202
  * @param status - Optional HTTP status code
200
203
  */
201
204
  emitResponse(action, ok, startTime, status) {
205
+ const service = this.getServiceName();
206
+ const durationMs = Date.now() - startTime;
207
+ const span = this.spanName(action);
202
208
  this.emit(TelemetryEvents.SERVICE_RESPONSE, {
203
- service: this.getServiceName(),
209
+ service,
210
+ action,
211
+ span,
212
+ ok,
213
+ duration: durationMs,
214
+ durationMs,
215
+ status
216
+ });
217
+ this.emit(TelemetryEvents.SPAN, {
218
+ span,
219
+ service,
204
220
  action,
205
221
  ok,
206
- duration: Date.now() - startTime,
222
+ durationMs,
207
223
  status
208
224
  });
209
225
  }
@@ -212,9 +228,11 @@ var BaseService = class {
212
228
  *
213
229
  * @param error - The service error
214
230
  */
215
- emitError(error) {
231
+ emitError(error, action) {
232
+ const span = action ? this.spanName(action) : void 0;
216
233
  this.emit(TelemetryEvents.SERVICE_ERROR, {
217
234
  service: this.getServiceName(),
235
+ ...span ? { span } : {},
218
236
  error
219
237
  });
220
238
  }
@@ -225,6 +243,12 @@ var BaseService = class {
225
243
  getServiceName() {
226
244
  return this.constructor.serviceName;
227
245
  }
246
+ /**
247
+ * Stable span name used by SDK telemetry sinks.
248
+ */
249
+ spanName(action) {
250
+ return `sdk.${this.getServiceName()}.${action}`;
251
+ }
228
252
  /**
229
253
  * Create a combined abort signal from multiple sources.
230
254
  *
@@ -262,13 +286,13 @@ var BaseService = class {
262
286
  this.emitResponse(action, true, startTime);
263
287
  } else {
264
288
  this.emitResponse(action, false, startTime);
265
- this.emitError(result.error);
289
+ this.emitError(result.error, action);
266
290
  }
267
291
  return result;
268
292
  } catch (error) {
269
293
  const serviceError2 = wrapError(this.getServiceName(), error);
270
294
  this.emitResponse(action, false, startTime);
271
- this.emitError(serviceError2);
295
+ this.emitError(serviceError2, action);
272
296
  return err(serviceError2);
273
297
  }
274
298
  }