@atproto/bsync 0.0.41 → 0.0.44

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 (49) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/dist/config.d.ts +9 -0
  3. package/dist/config.d.ts.map +1 -1
  4. package/dist/config.js +13 -0
  5. package/dist/config.js.map +1 -1
  6. package/dist/context.d.ts +3 -0
  7. package/dist/context.d.ts.map +1 -1
  8. package/dist/context.js +14 -1
  9. package/dist/context.js.map +1 -1
  10. package/dist/dataplane.d.ts +9 -0
  11. package/dist/dataplane.d.ts.map +1 -0
  12. package/dist/dataplane.js +30 -0
  13. package/dist/dataplane.js.map +1 -0
  14. package/dist/proto/bsync_connect.d.ts +10 -1
  15. package/dist/proto/bsync_connect.d.ts.map +1 -1
  16. package/dist/proto/bsync_connect.js +11 -2
  17. package/dist/proto/bsync_connect.js.map +1 -1
  18. package/dist/proto/bsync_pb.d.ts +35 -1
  19. package/dist/proto/bsync_pb.d.ts.map +1 -1
  20. package/dist/proto/bsync_pb.js +56 -1
  21. package/dist/proto/bsync_pb.js.map +1 -1
  22. package/dist/proto/dataplane_connect.d.ts +20 -0
  23. package/dist/proto/dataplane_connect.d.ts.map +1 -0
  24. package/dist/proto/dataplane_connect.js +24 -0
  25. package/dist/proto/dataplane_connect.js.map +1 -0
  26. package/dist/proto/dataplane_pb.d.ts +37 -0
  27. package/dist/proto/dataplane_pb.d.ts.map +1 -0
  28. package/dist/proto/dataplane_pb.js +61 -0
  29. package/dist/proto/dataplane_pb.js.map +1 -0
  30. package/dist/routes/fanout-notification-seen.d.ts +6 -0
  31. package/dist/routes/fanout-notification-seen.d.ts.map +1 -0
  32. package/dist/routes/fanout-notification-seen.js +26 -0
  33. package/dist/routes/fanout-notification-seen.js.map +1 -0
  34. package/dist/routes/index.d.ts.map +1 -1
  35. package/dist/routes/index.js +12 -2
  36. package/dist/routes/index.js.map +1 -1
  37. package/dist/telemetry/attributes.d.ts +14 -0
  38. package/dist/telemetry/attributes.d.ts.map +1 -0
  39. package/dist/telemetry/attributes.js +14 -0
  40. package/dist/telemetry/attributes.js.map +1 -0
  41. package/dist/telemetry/rpc.d.ts +30 -0
  42. package/dist/telemetry/rpc.d.ts.map +1 -0
  43. package/dist/telemetry/rpc.js +113 -0
  44. package/dist/telemetry/rpc.js.map +1 -0
  45. package/dist/telemetry/telemetry.d.ts +2 -0
  46. package/dist/telemetry/telemetry.d.ts.map +1 -0
  47. package/dist/telemetry/telemetry.js +7 -0
  48. package/dist/telemetry/telemetry.js.map +1 -0
  49. package/package.json +16 -6
@@ -0,0 +1,14 @@
1
+ import { ATTR_BSYNC_NAMESPACE, ATTR_BSYNC_OPERATION, } from '@atproto-labs/opentelemetry-node/conventions';
2
+ import { Method } from '../proto/bsync_pb.js';
3
+ /**
4
+ * Attributes describing a stash operation.
5
+ *
6
+ *
7
+ * Both values are low-cardinality: the namespace is an NSID drawn from a fixed
8
+ * set of record types, and the method is one of create/update/delete.
9
+ */
10
+ export const operationAttributes = (op) => ({
11
+ [ATTR_BSYNC_NAMESPACE]: op.namespace,
12
+ [ATTR_BSYNC_OPERATION]: Method[op.method]?.toLowerCase() ?? 'unknown',
13
+ });
14
+ //# sourceMappingURL=attributes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attributes.js","sourceRoot":"","sources":["../../src/telemetry/attributes.ts"],"names":[],"mappings":"AACA,OAAO,EACL,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,8CAA8C,CAAA;AACrD,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAE7C;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,EAGnC,EAAc,EAAE,CAAC,CAAC;IACjB,CAAC,oBAAoB,CAAC,EAAE,EAAE,CAAC,SAAS;IACpC,CAAC,oBAAoB,CAAC,EAAE,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,IAAI,SAAS;CACtE,CAAC,CAAA","sourcesContent":["import type { Attributes } from '@opentelemetry/api'\nimport {\n ATTR_BSYNC_NAMESPACE,\n ATTR_BSYNC_OPERATION,\n} from '@atproto-labs/opentelemetry-node/conventions'\nimport { Method } from '../proto/bsync_pb.js'\n\n/**\n * Attributes describing a stash operation.\n *\n *\n * Both values are low-cardinality: the namespace is an NSID drawn from a fixed\n * set of record types, and the method is one of create/update/delete.\n */\nexport const operationAttributes = (op: {\n namespace: string\n method: Method\n}): Attributes => ({\n [ATTR_BSYNC_NAMESPACE]: op.namespace,\n [ATTR_BSYNC_OPERATION]: Method[op.method]?.toLowerCase() ?? 'unknown',\n})\n"]}
@@ -0,0 +1,30 @@
1
+ import { type Interceptor, type ServiceImpl, type StreamRequest, type UnaryRequest } from '@connectrpc/connect';
2
+ import { type Attributes } from '@opentelemetry/api';
3
+ import type { Service } from '../proto/bsync_connect.js';
4
+ type ServiceMethods = Partial<ServiceImpl<typeof Service>>;
5
+ type RpcRequest = UnaryRequest | StreamRequest;
6
+ type GetClientAttributes = (request: RpcRequest) => Attributes;
7
+ /**
8
+ * Derives extra attributes from a request. The returned attributes land on both
9
+ * the duration histogram and the request span, so they must stay
10
+ * low-cardinality: no DIDs, keys or URIs, which would explode the metric series.
11
+ */
12
+ export type GetAttributes<M extends keyof ServiceMethods> = (request: Parameters<NonNullable<ServiceMethods[M]>>[0]) => Attributes;
13
+ export declare const createRpcClientInterceptor: (getAttributes?: GetClientAttributes) => Interceptor;
14
+ /**
15
+ * Wraps every handler of a service implementation with server-side RPC
16
+ * telemetry: an `rpc.server.call.duration` histogram, plus RPC attributes and
17
+ * error status on the span the HTTP instrumentation already opened for the
18
+ * request.
19
+ *
20
+ * The client half of the same call is recorded by the AppView, in
21
+ * `@atproto/bsky`'s `createRpcClientInterceptor`. Both sides use the same
22
+ * attribute keys, so queue time (client duration minus server duration) is a
23
+ * subtraction rather than a join across differently-shaped series.
24
+ *
25
+ * @note Connect has no server-side counterpart to a client `Interceptor`, so
26
+ * this wraps the implementations rather than plugging into the router.
27
+ */
28
+ export declare const withRpcServerTelemetry: <T extends ServiceMethods>(methods: T, getAttributes?: { [M in keyof T]?: GetAttributes<M & keyof ServiceMethods>; }) => T;
29
+ export {};
30
+ //# sourceMappingURL=rpc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rpc.d.ts","sourceRoot":"","sources":["../../src/telemetry/rpc.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,YAAY,EAClB,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EACL,KAAK,UAAU,EAShB,MAAM,oBAAoB,CAAA;AAY3B,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAA;AAExD,KAAK,cAAc,GAAG,OAAO,CAAC,WAAW,CAAC,OAAO,OAAO,CAAC,CAAC,CAAA;AAC1D,KAAK,UAAU,GAAG,YAAY,GAAG,aAAa,CAAA;AAC9C,KAAK,mBAAmB,GAAG,CAAC,OAAO,EAAE,UAAU,KAAK,UAAU,CAAA;AAE9D;;;;GAIG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,MAAM,cAAc,IAAI,CAC1D,OAAO,EAAE,UAAU,CAAC,WAAW,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KACnD,UAAU,CAAA;AA6Bf,eAAO,MAAM,0BAA0B,mBACrB,mBAAmB,KAClC,WAwCF,CAAA;AAED;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,sBAAsB,GAAI,CAAC,SAAS,cAAc,WACpD,CAAC,kBACM,GAAG,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC,GAAG,MAAM,cAAc,CAAC,GAAE,KAC3E,CAMK,CAAA"}
@@ -0,0 +1,113 @@
1
+ import { performance } from 'node:perf_hooks';
2
+ import { Code, ConnectError, } from '@connectrpc/connect';
3
+ import { SpanKind, SpanStatusCode, ValueType, context, metrics, propagation, trace, } from '@opentelemetry/api';
4
+ import { ATTR_ERROR_TYPE } from '@opentelemetry/semantic-conventions';
5
+ import { ATTR_RPC_METHOD, ATTR_RPC_RESPONSE_STATUS_CODE, ATTR_RPC_SYSTEM_NAME, METRIC_RPC_CLIENT_CALL_DURATION, METRIC_RPC_SERVER_CALL_DURATION, RPC_SYSTEM_NAME_VALUE_CONNECTRPC, } from '@opentelemetry/semantic-conventions/incubating';
6
+ import { RPC_CALL_DURATION_BUCKETS } from '@atproto-labs/opentelemetry-node/conventions';
7
+ import { statusCodeToString } from '@atproto-labs/opentelemetry-node/util';
8
+ const meter = metrics.getMeter('@atproto/bsync');
9
+ const tracer = trace.getTracer('@atproto/bsync');
10
+ const rpcClientDuration = meter.createHistogram(METRIC_RPC_CLIENT_CALL_DURATION, {
11
+ description: 'Measures the duration of an outgoing Remote Procedure Call',
12
+ unit: 's',
13
+ valueType: ValueType.DOUBLE,
14
+ advice: { explicitBucketBoundaries: RPC_CALL_DURATION_BUCKETS },
15
+ });
16
+ const rpcServerDuration = meter.createHistogram(METRIC_RPC_SERVER_CALL_DURATION, {
17
+ description: 'Measures the duration of an incoming Remote Procedure Call',
18
+ unit: 's',
19
+ valueType: ValueType.DOUBLE,
20
+ advice: { explicitBucketBoundaries: RPC_CALL_DURATION_BUCKETS },
21
+ });
22
+ const headersSetter = {
23
+ set(carrier, key, value) {
24
+ carrier.set(key, value);
25
+ },
26
+ };
27
+ export const createRpcClientInterceptor = (getAttributes) => {
28
+ return (next) => async (req) => {
29
+ const method = `${req.service.typeName}/${req.method.name}`;
30
+ const attributes = {
31
+ [ATTR_RPC_SYSTEM_NAME]: RPC_SYSTEM_NAME_VALUE_CONNECTRPC,
32
+ [ATTR_RPC_METHOD]: method,
33
+ ...getAttributes?.(req),
34
+ };
35
+ const start = performance.now();
36
+ return tracer.startActiveSpan(method, { kind: SpanKind.CLIENT, attributes }, async (span) => {
37
+ propagation.inject(context.active(), req.header, headersSetter);
38
+ let code;
39
+ try {
40
+ return await next(req);
41
+ }
42
+ catch (err) {
43
+ code = err instanceof ConnectError ? err.code : Code.Unknown;
44
+ span.setStatus({ code: SpanStatusCode.ERROR });
45
+ span.recordException(err instanceof Error ? err : String(err));
46
+ throw err;
47
+ }
48
+ finally {
49
+ const statusCode = statusCodeToString(code);
50
+ const completedAttributes = {
51
+ ...attributes,
52
+ [ATTR_RPC_RESPONSE_STATUS_CODE]: statusCode,
53
+ ...(code !== undefined && { [ATTR_ERROR_TYPE]: statusCode }),
54
+ };
55
+ span.setAttributes(completedAttributes);
56
+ rpcClientDuration.record((performance.now() - start) / 1000, completedAttributes);
57
+ span.end();
58
+ }
59
+ });
60
+ };
61
+ };
62
+ /**
63
+ * Wraps every handler of a service implementation with server-side RPC
64
+ * telemetry: an `rpc.server.call.duration` histogram, plus RPC attributes and
65
+ * error status on the span the HTTP instrumentation already opened for the
66
+ * request.
67
+ *
68
+ * The client half of the same call is recorded by the AppView, in
69
+ * `@atproto/bsky`'s `createRpcClientInterceptor`. Both sides use the same
70
+ * attribute keys, so queue time (client duration minus server duration) is a
71
+ * subtraction rather than a join across differently-shaped series.
72
+ *
73
+ * @note Connect has no server-side counterpart to a client `Interceptor`, so
74
+ * this wraps the implementations rather than plugging into the router.
75
+ */
76
+ export const withRpcServerTelemetry = (methods, getAttributes) => Object.fromEntries(Object.entries(methods).map(([name, impl]) => [
77
+ name,
78
+ instrument(impl, getAttributes?.[name]),
79
+ ]));
80
+ const instrument = (impl, getAttributes) => async function (request, ctx) {
81
+ // @NOTE The RPC convention requires `rpc.method` to be the fully-qualified
82
+ // method name, so the service name is part of it rather than an attribute
83
+ // of its own.
84
+ const attributes = {
85
+ [ATTR_RPC_SYSTEM_NAME]: RPC_SYSTEM_NAME_VALUE_CONNECTRPC,
86
+ [ATTR_RPC_METHOD]: `${ctx.service.typeName}/${ctx.method.name}`,
87
+ ...getAttributes?.(request),
88
+ };
89
+ const span = trace.getActiveSpan();
90
+ span?.setAttributes(attributes);
91
+ const start = performance.now();
92
+ let code;
93
+ try {
94
+ return await impl.call(this, request, ctx);
95
+ }
96
+ catch (err) {
97
+ code = err instanceof ConnectError ? err.code : Code.Unknown;
98
+ span?.setStatus({ code: SpanStatusCode.ERROR });
99
+ span?.recordException(err instanceof Error ? err : String(err));
100
+ throw err;
101
+ }
102
+ finally {
103
+ const statusCode = statusCodeToString(code);
104
+ rpcServerDuration.record((performance.now() - start) / 1000, {
105
+ ...attributes,
106
+ [ATTR_RPC_RESPONSE_STATUS_CODE]: statusCode,
107
+ // @NOTE The convention requires `error.type` to be unset on success,
108
+ // and to carry the status code on failure.
109
+ ...(code !== undefined && { [ATTR_ERROR_TYPE]: statusCode }),
110
+ });
111
+ }
112
+ };
113
+ //# sourceMappingURL=rpc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rpc.js","sourceRoot":"","sources":["../../src/telemetry/rpc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAC7C,OAAO,EACL,IAAI,EACJ,YAAY,GAMb,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EAEL,QAAQ,EACR,cAAc,EAEd,SAAS,EACT,OAAO,EACP,OAAO,EACP,WAAW,EACX,KAAK,GACN,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAA;AACrE,OAAO,EACL,eAAe,EACf,6BAA6B,EAC7B,oBAAoB,EACpB,+BAA+B,EAC/B,+BAA+B,EAC/B,gCAAgC,GACjC,MAAM,gDAAgD,CAAA;AACvD,OAAO,EAAE,yBAAyB,EAAE,MAAM,8CAA8C,CAAA;AACxF,OAAO,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAA;AAgB1E,MAAM,KAAK,GAAG,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAC,CAAA;AAChD,MAAM,MAAM,GAAG,KAAK,C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CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAA;QAC/D,MAAM,GAAG,CAAA;IACX,CAAC;YAAS,CAAC;QACT,MAAM,UAAU,GAAG,kBAAkB,CAAC,IAAI,CAAC,CAAA;QAC3C,iBAAiB,CAAC,MAAM,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,GAAG,IAAI,EAAE;YAC3D,GAAG,UAAU;YACb,CAAC,6BAA6B,CAAC,EAAE,UAAU;YAC3C,qEAAqE;YACrE,2CAA2C;YAC3C,GAAG,CAAC,IAAI,KAAK,SAAS,IAAI,EAAE,CAAC,eAAe,CAAC,EAAE,UAAU,EAAE,CAAC;SAC7D,CAAC,CAAA;IACJ,CAAC;AACH,CAAC,CAAA","sourcesContent":["import { performance } from 'node:perf_hooks'\nimport {\n Code,\n ConnectError,\n type HandlerContext,\n type Interceptor,\n type ServiceImpl,\n type StreamRequest,\n type UnaryRequest,\n} from '@connectrpc/connect'\nimport {\n type Attributes,\n SpanKind,\n SpanStatusCode,\n type TextMapSetter,\n ValueType,\n context,\n metrics,\n propagation,\n trace,\n} from '@opentelemetry/api'\nimport { ATTR_ERROR_TYPE } from '@opentelemetry/semantic-conventions'\nimport {\n ATTR_RPC_METHOD,\n ATTR_RPC_RESPONSE_STATUS_CODE,\n ATTR_RPC_SYSTEM_NAME,\n METRIC_RPC_CLIENT_CALL_DURATION,\n METRIC_RPC_SERVER_CALL_DURATION,\n RPC_SYSTEM_NAME_VALUE_CONNECTRPC,\n} from '@opentelemetry/semantic-conventions/incubating'\nimport { RPC_CALL_DURATION_BUCKETS } from '@atproto-labs/opentelemetry-node/conventions'\nimport { statusCodeToString } from '@atproto-labs/opentelemetry-node/util'\nimport type { Service } from '../proto/bsync_connect.js'\n\ntype ServiceMethods = Partial<ServiceImpl<typeof Service>>\ntype RpcRequest = UnaryRequest | StreamRequest\ntype GetClientAttributes = (request: RpcRequest) => Attributes\n\n/**\n * Derives extra attributes from a request. The returned attributes land on both\n * the duration histogram and the request span, so they must stay\n * low-cardinality: no DIDs, keys or URIs, which would explode the metric series.\n */\nexport type GetAttributes<M extends keyof ServiceMethods> = (\n request: Parameters<NonNullable<ServiceMethods[M]>>[0],\n) => Attributes\n\nconst meter = metrics.getMeter('@atproto/bsync')\nconst tracer = trace.getTracer('@atproto/bsync')\nconst rpcClientDuration = meter.createHistogram(\n METRIC_RPC_CLIENT_CALL_DURATION,\n {\n description: 'Measures the duration of an outgoing Remote Procedure Call',\n unit: 's',\n valueType: ValueType.DOUBLE,\n advice: { explicitBucketBoundaries: RPC_CALL_DURATION_BUCKETS },\n },\n)\nconst rpcServerDuration = meter.createHistogram(\n METRIC_RPC_SERVER_CALL_DURATION,\n {\n description: 'Measures the duration of an incoming Remote Procedure Call',\n unit: 's',\n valueType: ValueType.DOUBLE,\n advice: { explicitBucketBoundaries: RPC_CALL_DURATION_BUCKETS },\n },\n)\n\nconst headersSetter: TextMapSetter<Headers> = {\n set(carrier, key, value) {\n carrier.set(key, value)\n },\n}\n\nexport const createRpcClientInterceptor = (\n getAttributes?: GetClientAttributes,\n): Interceptor => {\n return (next) => async (req: RpcRequest) => {\n const method = `${req.service.typeName}/${req.method.name}`\n const attributes: Attributes = {\n [ATTR_RPC_SYSTEM_NAME]: RPC_SYSTEM_NAME_VALUE_CONNECTRPC,\n [ATTR_RPC_METHOD]: method,\n ...getAttributes?.(req),\n }\n const start = performance.now()\n\n return tracer.startActiveSpan(\n method,\n { kind: SpanKind.CLIENT, attributes },\n async (span) => {\n propagation.inject(context.active(), req.header, headersSetter)\n let code: Code | undefined\n try {\n return await next(req)\n } catch (err) {\n code = err instanceof ConnectError ? err.code : Code.Unknown\n span.setStatus({ code: SpanStatusCode.ERROR })\n span.recordException(err instanceof Error ? err : String(err))\n throw err\n } finally {\n const statusCode = statusCodeToString(code)\n const completedAttributes: Attributes = {\n ...attributes,\n [ATTR_RPC_RESPONSE_STATUS_CODE]: statusCode,\n ...(code !== undefined && { [ATTR_ERROR_TYPE]: statusCode }),\n }\n span.setAttributes(completedAttributes)\n rpcClientDuration.record(\n (performance.now() - start) / 1000,\n completedAttributes,\n )\n span.end()\n }\n },\n )\n }\n}\n\n/**\n * Wraps every handler of a service implementation with server-side RPC\n * telemetry: an `rpc.server.call.duration` histogram, plus RPC attributes and\n * error status on the span the HTTP instrumentation already opened for the\n * request.\n *\n * The client half of the same call is recorded by the AppView, in\n * `@atproto/bsky`'s `createRpcClientInterceptor`. Both sides use the same\n * attribute keys, so queue time (client duration minus server duration) is a\n * subtraction rather than a join across differently-shaped series.\n *\n * @note Connect has no server-side counterpart to a client `Interceptor`, so\n * this wraps the implementations rather than plugging into the router.\n */\nexport const withRpcServerTelemetry = <T extends ServiceMethods>(\n methods: T,\n getAttributes?: { [M in keyof T]?: GetAttributes<M & keyof ServiceMethods> },\n): T =>\n Object.fromEntries(\n Object.entries(methods).map(([name, impl]) => [\n name,\n instrument(impl as Handler, getAttributes?.[name as keyof T]),\n ]),\n ) as T\n\ntype Handler = (request: never, ctx: HandlerContext) => Promise<unknown>\n\nconst instrument = (\n impl: Handler,\n getAttributes?: (request: never) => Attributes,\n): Handler =>\n async function (this: unknown, request, ctx) {\n // @NOTE The RPC convention requires `rpc.method` to be the fully-qualified\n // method name, so the service name is part of it rather than an attribute\n // of its own.\n const attributes: Attributes = {\n [ATTR_RPC_SYSTEM_NAME]: RPC_SYSTEM_NAME_VALUE_CONNECTRPC,\n [ATTR_RPC_METHOD]: `${ctx.service.typeName}/${ctx.method.name}`,\n ...getAttributes?.(request),\n }\n\n const span = trace.getActiveSpan()\n span?.setAttributes(attributes)\n\n const start = performance.now()\n let code: Code | undefined\n try {\n return await impl.call(this, request, ctx)\n } catch (err) {\n code = err instanceof ConnectError ? err.code : Code.Unknown\n span?.setStatus({ code: SpanStatusCode.ERROR })\n span?.recordException(err instanceof Error ? err : String(err))\n throw err\n } finally {\n const statusCode = statusCodeToString(code)\n rpcServerDuration.record((performance.now() - start) / 1000, {\n ...attributes,\n [ATTR_RPC_RESPONSE_STATUS_CODE]: statusCode,\n // @NOTE The convention requires `error.type` to be unset on success,\n // and to carry the status code on failure.\n ...(code !== undefined && { [ATTR_ERROR_TYPE]: statusCode }),\n })\n }\n }\n"]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=telemetry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telemetry.d.ts","sourceRoot":"","sources":["../../src/telemetry/telemetry.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ import pkg from '@atproto/bsync/package.json' with { type: 'json' };
2
+ import { setup } from '@atproto-labs/opentelemetry-node';
3
+ await setup(() => ({
4
+ name: pkg.name,
5
+ version: pkg.version,
6
+ }));
7
+ //# sourceMappingURL=telemetry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"telemetry.js","sourceRoot":"","sources":["../../src/telemetry/telemetry.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,6BAA6B,CAAC,OAAO,IAAI,EAAE,MAAM,EAAE,CAAA;AACnE,OAAO,EAAE,KAAK,EAAE,MAAM,kCAAkC,CAAA;AAExD,MAAM,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC;IACjB,IAAI,EAAE,GAAG,CAAC,IAAI;IACd,OAAO,EAAE,GAAG,CAAC,OAAO;CACrB,CAAC,CAAC,CAAA","sourcesContent":["import pkg from '@atproto/bsync/package.json' with { type: 'json' }\nimport { setup } from '@atproto-labs/opentelemetry-node'\n\nawait setup(() => ({\n name: pkg.name,\n version: pkg.version,\n}))\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atproto/bsync",
3
- "version": "0.0.41",
3
+ "version": "0.0.44",
4
4
  "license": "MIT",
5
5
  "description": "Sychronizing service for app.bsky App View (Bluesky API)",
6
6
  "keywords": [
@@ -23,6 +23,13 @@
23
23
  ".": {
24
24
  "types": "./dist/index.d.ts",
25
25
  "default": "./dist/index.js"
26
+ },
27
+ "./telemetry": {
28
+ "types": "./dist/telemetry/telemetry.d.ts",
29
+ "default": "./dist/telemetry/telemetry.js"
30
+ },
31
+ "./package.json": {
32
+ "default": "./package.json"
26
33
  }
27
34
  },
28
35
  "engines": {
@@ -32,20 +39,23 @@
32
39
  "@bufbuild/protobuf": "^1.5.0",
33
40
  "@connectrpc/connect": "^1.1.4",
34
41
  "@connectrpc/connect-node": "^1.1.4",
42
+ "@opentelemetry/api": "^1.9.1",
43
+ "@opentelemetry/semantic-conventions": "^1.41.1",
35
44
  "http-terminator": "^3.2.0",
36
45
  "kysely": "^0.29.2",
37
46
  "pg": "^8.10.0",
38
47
  "pino-http": "^11.0.0",
39
- "@atproto/common": "^0.8.0",
40
- "@atproto/syntax": "^0.7.4"
48
+ "@atproto/common": "^0.8.3",
49
+ "@atproto/syntax": "^0.7.6",
50
+ "@atproto-labs/opentelemetry-node": "^0.2.1"
41
51
  },
42
52
  "devDependencies": {
43
- "@bufbuild/buf": "^1.28.1",
53
+ "@bufbuild/buf": "^1.72.0",
44
54
  "@bufbuild/protoc-gen-es": "^1.5.0",
45
- "@connectrpc/protoc-gen-connect-es": "^1.1.4",
55
+ "@connectrpc/protoc-gen-connect-es": "^1.7.0",
46
56
  "@types/pg": "^8.15.5",
47
57
  "get-port": "^5.1.1",
48
- "jest": "^30.0.0"
58
+ "jest": "^30.5.1"
49
59
  },
50
60
  "scripts": {
51
61
  "codegen:buf": "buf generate proto",