@rhinestone/service-sdk 0.2.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/README.md +1 -0
- package/dist/chains.d.ts +3 -0
- package/dist/chains.d.ts.map +1 -0
- package/dist/chains.js +25 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/opentelemetry/amqp.d.ts +3 -0
- package/dist/opentelemetry/amqp.d.ts.map +1 -0
- package/dist/opentelemetry/amqp.js +7 -0
- package/dist/opentelemetry/api.d.ts +45 -0
- package/dist/opentelemetry/api.d.ts.map +1 -0
- package/dist/opentelemetry/api.js +135 -0
- package/dist/opentelemetry/express.d.ts +3 -0
- package/dist/opentelemetry/express.d.ts.map +1 -0
- package/dist/opentelemetry/express.js +14 -0
- package/dist/opentelemetry/http.d.ts +3 -0
- package/dist/opentelemetry/http.d.ts.map +1 -0
- package/dist/opentelemetry/http.js +14 -0
- package/dist/opentelemetry/index.d.ts +3 -0
- package/dist/opentelemetry/index.d.ts.map +1 -0
- package/dist/opentelemetry/index.js +28 -0
- package/dist/opentelemetry/pino.d.ts +3 -0
- package/dist/opentelemetry/pino.d.ts.map +1 -0
- package/dist/opentelemetry/pino.js +9 -0
- package/dist/opentelemetry/prisma.d.ts +3 -0
- package/dist/opentelemetry/prisma.d.ts.map +1 -0
- package/dist/opentelemetry/prisma.js +7 -0
- package/dist/opentelemetry/setup.d.ts +9 -0
- package/dist/opentelemetry/setup.d.ts.map +1 -0
- package/dist/opentelemetry/setup.js +95 -0
- package/package.json +58 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# service-sdk
|
package/dist/chains.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chains.d.ts","sourceRoot":"","sources":["../src/chains.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AAM1D,wBAAsB,eAAe,CACnC,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,gBAAgB,CAAC,CAK3B"}
|
package/dist/chains.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.estimateGasFees = estimateGasFees;
|
|
4
|
+
const chains_1 = require("viem/chains");
|
|
5
|
+
const SEPOLIA_FEE_HISTORY_BLOCKS = 3;
|
|
6
|
+
const SEPOLIA_REWARD_PERCENTILE = 75;
|
|
7
|
+
async function estimateGasFees(client, chainId) {
|
|
8
|
+
if (chainId === chains_1.sepolia.id) {
|
|
9
|
+
return estimateSepoliaGasFees(client);
|
|
10
|
+
}
|
|
11
|
+
return client.estimateFeesPerGas();
|
|
12
|
+
}
|
|
13
|
+
async function estimateSepoliaGasFees(client) {
|
|
14
|
+
const feeHistory = await client.getFeeHistory({
|
|
15
|
+
blockCount: SEPOLIA_FEE_HISTORY_BLOCKS,
|
|
16
|
+
rewardPercentiles: [SEPOLIA_REWARD_PERCENTILE],
|
|
17
|
+
});
|
|
18
|
+
const priorityFee = feeHistory.reward?.at(-1)?.[0] ?? 0n;
|
|
19
|
+
const baseFee = feeHistory.baseFeePerGas.at(-1) ?? 0n;
|
|
20
|
+
const maxFeePerGas = baseFee * 2n + priorityFee;
|
|
21
|
+
return {
|
|
22
|
+
maxFeePerGas,
|
|
23
|
+
maxPriorityFeePerGas: priorityFee,
|
|
24
|
+
};
|
|
25
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"amqp.d.ts","sourceRoot":"","sources":["../../src/opentelemetry/amqp.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,wCAAwC,CAAA;AAE/E,wBAAgB,wBAAwB,2BAEvC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setupAmqpInstrumentation = setupAmqpInstrumentation;
|
|
4
|
+
const instrumentation_amqplib_1 = require("@opentelemetry/instrumentation-amqplib");
|
|
5
|
+
function setupAmqpInstrumentation() {
|
|
6
|
+
return new instrumentation_amqplib_1.AmqplibInstrumentation();
|
|
7
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type Attributes, type Context, type Counter, type Gauge, type Histogram, type Meter, type MetricOptions, type ObservableGauge, type Span, type SpanOptions, SpanStatusCode, ValueType } from '@opentelemetry/api';
|
|
2
|
+
export type { Attributes, Context, Counter, Gauge, Histogram, Meter, MetricOptions, ObservableGauge, Span, SpanOptions, };
|
|
3
|
+
export { SpanStatusCode, ValueType };
|
|
4
|
+
export declare function currentSpan(): Span | undefined;
|
|
5
|
+
export declare function currentContext(): Context;
|
|
6
|
+
export declare function currentTraceId(): string;
|
|
7
|
+
export declare function addSpanAttributes(attributes: Attributes): void;
|
|
8
|
+
export declare function recordSpanError(err: any): void;
|
|
9
|
+
export declare function withSpan<T>(name: string, fn: () => T, options?: SpanOptions): T;
|
|
10
|
+
export declare function withSpan<T>(name: string, fn: () => Promise<T>, options?: SpanOptions): Promise<T>;
|
|
11
|
+
export declare function withRootContext<T>(callback: () => T): T;
|
|
12
|
+
export declare function withRootContext<T>(callback: () => Promise<T>): Promise<T>;
|
|
13
|
+
export declare const CreateSpan: (name?: string) => (originalMethod: (...args: any[]) => any, context: ClassMethodDecoratorContext) => (this: any, ...args: any[]) => any;
|
|
14
|
+
export declare const createHistogram: (name: string, options?: MetricOptions) => Histogram<Attributes>;
|
|
15
|
+
export declare const createCounter: (name: string, options?: MetricOptions) => Counter<Attributes>;
|
|
16
|
+
export declare const createGauge: (name: string, options?: MetricOptions) => Gauge<Attributes>;
|
|
17
|
+
export declare const createObservableGauge: (name: string, options?: MetricOptions) => ObservableGauge<Attributes>;
|
|
18
|
+
/**
|
|
19
|
+
* Returns W3C trace context headers (traceparent, tracestate) from the
|
|
20
|
+
* current active OTel context as a plain object. Useful for axios / generic HTTP.
|
|
21
|
+
*/
|
|
22
|
+
export declare function getTraceHeaders(): Record<string, string>;
|
|
23
|
+
/**
|
|
24
|
+
* Viem onFetchRequest hook that injects W3C trace context headers into outgoing
|
|
25
|
+
* RPC requests. Must return the modified init so viem uses it for the actual fetch.
|
|
26
|
+
*/
|
|
27
|
+
export declare function injectTraceHeaders(_request: Request, init: RequestInit): {
|
|
28
|
+
headers: {
|
|
29
|
+
[x: string]: string;
|
|
30
|
+
};
|
|
31
|
+
body?: BodyInit | null;
|
|
32
|
+
cache?: RequestCache;
|
|
33
|
+
credentials?: RequestCredentials;
|
|
34
|
+
integrity?: string;
|
|
35
|
+
keepalive?: boolean;
|
|
36
|
+
method?: string;
|
|
37
|
+
mode?: RequestMode;
|
|
38
|
+
priority?: RequestPriority;
|
|
39
|
+
redirect?: RequestRedirect;
|
|
40
|
+
referrer?: string;
|
|
41
|
+
referrerPolicy?: ReferrerPolicy;
|
|
42
|
+
signal?: AbortSignal | null;
|
|
43
|
+
window?: null;
|
|
44
|
+
} | undefined;
|
|
45
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/opentelemetry/api.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,UAAU,EACf,KAAK,OAAO,EACZ,KAAK,OAAO,EAEZ,KAAK,KAAK,EACV,KAAK,SAAS,EACd,KAAK,KAAK,EACV,KAAK,aAAa,EAElB,KAAK,eAAe,EAGpB,KAAK,IAAI,EACT,KAAK,WAAW,EAChB,cAAc,EAEd,SAAS,EACV,MAAM,oBAAoB,CAAA;AAG3B,YAAY,EACV,UAAU,EACV,OAAO,EACP,OAAO,EACP,KAAK,EACL,SAAS,EACT,KAAK,EACL,aAAa,EACb,eAAe,EACf,IAAI,EACJ,WAAW,GACZ,CAAA;AACD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,CAAA;AAKpC,wBAAgB,WAAW,IAAI,IAAI,GAAG,SAAS,CAE9C;AAED,wBAAgB,cAAc,IAAI,OAAO,CAExC;AAED,wBAAgB,cAAc,IAAI,MAAM,CAEvC;AAED,wBAAgB,iBAAiB,CAAC,UAAU,EAAE,UAAU,QAEvD;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,GAAG,QAavC;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,CAAC,CAAA;AAChF,wBAAgB,QAAQ,CAAC,CAAC,EACxB,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,CAAC,EAAE,WAAW,GACpB,OAAO,CAAC,CAAC,CAAC,CAAA;AA2Cb,wBAAgB,eAAe,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,GAAG,CAAC,CAAA;AACxD,wBAAgB,eAAe,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;AAO1E,eAAO,MAAM,UAAU,GAAI,OAAO,MAAM,MAEpC,gBAAgB,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,EACvC,SAAS,2BAA2B,MAGnB,MAAM,GAAG,EAAE,GAAG,MAAM,GAAG,EAAE,QAI7C,CAAA;AAMD,eAAO,MAAM,eAAe,GAC1B,MAAM,MAAM,EACZ,UAAU,aAAa,KACtB,SAAS,CAAC,UAAU,CAEtB,CAAA;AAED,eAAO,MAAM,aAAa,GACxB,MAAM,MAAM,EACZ,UAAU,aAAa,KACtB,OAAO,CAAC,UAAU,CAEpB,CAAA;AAED,eAAO,MAAM,WAAW,GACtB,MAAM,MAAM,EACZ,UAAU,aAAa,KACtB,KAAK,CAAC,UAAU,CAElB,CAAA;AAED,eAAO,MAAM,qBAAqB,GAChC,MAAM,MAAM,EACZ,UAAU,aAAa,KACtB,eAAe,CAAC,UAAU,CAE5B,CAAA;AAED;;;GAGG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAIxD;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,WAAW;;;;;;;;;;;;;;;;;cAUtE"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createObservableGauge = exports.createGauge = exports.createCounter = exports.createHistogram = exports.CreateSpan = exports.ValueType = exports.SpanStatusCode = void 0;
|
|
4
|
+
exports.currentSpan = currentSpan;
|
|
5
|
+
exports.currentContext = currentContext;
|
|
6
|
+
exports.currentTraceId = currentTraceId;
|
|
7
|
+
exports.addSpanAttributes = addSpanAttributes;
|
|
8
|
+
exports.recordSpanError = recordSpanError;
|
|
9
|
+
exports.withSpan = withSpan;
|
|
10
|
+
exports.withRootContext = withRootContext;
|
|
11
|
+
exports.getTraceHeaders = getTraceHeaders;
|
|
12
|
+
exports.injectTraceHeaders = injectTraceHeaders;
|
|
13
|
+
const api_1 = require("@opentelemetry/api");
|
|
14
|
+
Object.defineProperty(exports, "SpanStatusCode", { enumerable: true, get: function () { return api_1.SpanStatusCode; } });
|
|
15
|
+
Object.defineProperty(exports, "ValueType", { enumerable: true, get: function () { return api_1.ValueType; } });
|
|
16
|
+
const INSTRUMENTATION_NAME = 'internal.instrumentation';
|
|
17
|
+
const INSTRUMENTATION_VERSION = '0.0.1';
|
|
18
|
+
function currentSpan() {
|
|
19
|
+
return api_1.trace.getActiveSpan();
|
|
20
|
+
}
|
|
21
|
+
function currentContext() {
|
|
22
|
+
return api_1.context.active();
|
|
23
|
+
}
|
|
24
|
+
function currentTraceId() {
|
|
25
|
+
return currentSpan()?.spanContext().traceId ?? '<no trace id>';
|
|
26
|
+
}
|
|
27
|
+
function addSpanAttributes(attributes) {
|
|
28
|
+
currentSpan()?.setAttributes(attributes);
|
|
29
|
+
}
|
|
30
|
+
function recordSpanError(err) {
|
|
31
|
+
const errorMessage = err?.message || String(err) || 'Unknown error';
|
|
32
|
+
const errorStack = err?.stack || 'No stack trace available';
|
|
33
|
+
currentSpan()
|
|
34
|
+
?.setStatus({
|
|
35
|
+
code: api_1.SpanStatusCode.ERROR,
|
|
36
|
+
message: errorMessage,
|
|
37
|
+
})
|
|
38
|
+
.recordException({
|
|
39
|
+
stack: errorStack,
|
|
40
|
+
message: errorMessage,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function withSpan(name, fn, options) {
|
|
44
|
+
return api_1.trace
|
|
45
|
+
.getTracer(INSTRUMENTATION_NAME, INSTRUMENTATION_VERSION)
|
|
46
|
+
.startActiveSpan(name, options ? options : {}, (span) => {
|
|
47
|
+
try {
|
|
48
|
+
const result = fn();
|
|
49
|
+
if (result instanceof Promise) {
|
|
50
|
+
return result
|
|
51
|
+
.catch((reason) => {
|
|
52
|
+
span
|
|
53
|
+
.setStatus({
|
|
54
|
+
code: api_1.SpanStatusCode.ERROR,
|
|
55
|
+
message: reason.message,
|
|
56
|
+
})
|
|
57
|
+
.recordException({
|
|
58
|
+
stack: reason.stack,
|
|
59
|
+
message: reason.message,
|
|
60
|
+
});
|
|
61
|
+
throw reason;
|
|
62
|
+
})
|
|
63
|
+
.finally(() => span.end());
|
|
64
|
+
}
|
|
65
|
+
span.end();
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
span
|
|
70
|
+
.setStatus({
|
|
71
|
+
code: api_1.SpanStatusCode.ERROR,
|
|
72
|
+
message: e.message,
|
|
73
|
+
})
|
|
74
|
+
.recordException({ stack: e.stack, message: e.message });
|
|
75
|
+
span.end();
|
|
76
|
+
throw e;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
function withRootContext(callback) {
|
|
81
|
+
return api_1.context.with(api_1.ROOT_CONTEXT, callback);
|
|
82
|
+
}
|
|
83
|
+
const CreateSpan = (name) => {
|
|
84
|
+
return (originalMethod, context) => {
|
|
85
|
+
const methodName = name ?? String(context.name);
|
|
86
|
+
return function (...args) {
|
|
87
|
+
return withSpan(methodName, () => originalMethod.apply(this, args));
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
exports.CreateSpan = CreateSpan;
|
|
92
|
+
const getMeter = () => {
|
|
93
|
+
return api_1.metrics.getMeter(INSTRUMENTATION_NAME, INSTRUMENTATION_VERSION);
|
|
94
|
+
};
|
|
95
|
+
const createHistogram = (name, options) => {
|
|
96
|
+
return getMeter().createHistogram(name, options);
|
|
97
|
+
};
|
|
98
|
+
exports.createHistogram = createHistogram;
|
|
99
|
+
const createCounter = (name, options) => {
|
|
100
|
+
return getMeter().createCounter(name, options);
|
|
101
|
+
};
|
|
102
|
+
exports.createCounter = createCounter;
|
|
103
|
+
const createGauge = (name, options) => {
|
|
104
|
+
return getMeter().createGauge(name, options);
|
|
105
|
+
};
|
|
106
|
+
exports.createGauge = createGauge;
|
|
107
|
+
const createObservableGauge = (name, options) => {
|
|
108
|
+
return getMeter().createObservableGauge(name, options);
|
|
109
|
+
};
|
|
110
|
+
exports.createObservableGauge = createObservableGauge;
|
|
111
|
+
/**
|
|
112
|
+
* Returns W3C trace context headers (traceparent, tracestate) from the
|
|
113
|
+
* current active OTel context as a plain object. Useful for axios / generic HTTP.
|
|
114
|
+
*/
|
|
115
|
+
function getTraceHeaders() {
|
|
116
|
+
const headers = {};
|
|
117
|
+
api_1.propagation.inject(api_1.context.active(), headers);
|
|
118
|
+
return headers;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Viem onFetchRequest hook that injects W3C trace context headers into outgoing
|
|
122
|
+
* RPC requests. Must return the modified init so viem uses it for the actual fetch.
|
|
123
|
+
*/
|
|
124
|
+
function injectTraceHeaders(_request, init) {
|
|
125
|
+
const traceHeaders = getTraceHeaders();
|
|
126
|
+
if (Object.keys(traceHeaders).length === 0)
|
|
127
|
+
return;
|
|
128
|
+
return {
|
|
129
|
+
...init,
|
|
130
|
+
headers: {
|
|
131
|
+
...init.headers,
|
|
132
|
+
...traceHeaders,
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"express.d.ts","sourceRoot":"","sources":["../../src/opentelemetry/express.ts"],"names":[],"mappings":"AACA,OAAO,EACL,sBAAsB,EAGvB,MAAM,wCAAwC,CAAA;AAE/C,wBAAgB,2BAA2B,2BAe1C"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setupExpressInstrumentation = setupExpressInstrumentation;
|
|
4
|
+
const instrumentation_express_1 = require("@opentelemetry/instrumentation-express");
|
|
5
|
+
function setupExpressInstrumentation() {
|
|
6
|
+
return new instrumentation_express_1.ExpressInstrumentation({
|
|
7
|
+
requestHook: (span, info) => {
|
|
8
|
+
if (info.layerType === instrumentation_express_1.ExpressLayerType.REQUEST_HANDLER) {
|
|
9
|
+
span.setAttribute('http.request.referer', info.request.header('referer') ?? 'not defined');
|
|
10
|
+
span.setAttribute('http.request.body', JSON.stringify(info.request.body, undefined, 2));
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/opentelemetry/http.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAA;AAEzE,wBAAgB,wBAAwB,wBASvC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setupHttpInstrumentation = setupHttpInstrumentation;
|
|
4
|
+
const instrumentation_http_1 = require("@opentelemetry/instrumentation-http");
|
|
5
|
+
function setupHttpInstrumentation() {
|
|
6
|
+
return new instrumentation_http_1.HttpInstrumentation({
|
|
7
|
+
// Ignore WebSocket upgrade requests to prevent huge long-lived spans
|
|
8
|
+
ignoreOutgoingRequestHook: (request) => {
|
|
9
|
+
const headers = request.headers;
|
|
10
|
+
const upgrade = headers?.upgrade ?? headers?.Upgrade;
|
|
11
|
+
return upgrade === 'websocket';
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { type Attributes, addSpanAttributes, type Context, type Counter, CreateSpan, createCounter, createGauge, createHistogram, createObservableGauge, currentContext, currentSpan, currentTraceId, type Gauge, getTraceHeaders, type Histogram, injectTraceHeaders, type Meter, type MetricOptions, type ObservableGauge, recordSpanError, type Span, type SpanOptions, SpanStatusCode, ValueType, withRootContext, withSpan, } from './api';
|
|
2
|
+
export { configFromEnv, type SDKConfig, setupSDK, shutdownOpentelemetry, } from './setup';
|
|
3
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/opentelemetry/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,UAAU,EACf,iBAAiB,EACjB,KAAK,OAAO,EACZ,KAAK,OAAO,EACZ,UAAU,EACV,aAAa,EACb,WAAW,EAEX,eAAe,EACf,qBAAqB,EACrB,cAAc,EAEd,WAAW,EACX,cAAc,EACd,KAAK,KAAK,EAEV,eAAe,EACf,KAAK,SAAS,EACd,kBAAkB,EAClB,KAAK,KAAK,EACV,KAAK,aAAa,EAClB,KAAK,eAAe,EACpB,eAAe,EACf,KAAK,IAAI,EACT,KAAK,WAAW,EAChB,cAAc,EACd,SAAS,EACT,eAAe,EACf,QAAQ,GACT,MAAM,OAAO,CAAA;AACd,OAAO,EACL,aAAa,EACb,KAAK,SAAS,EAEd,QAAQ,EACR,qBAAqB,GACtB,MAAM,SAAS,CAAA"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.shutdownOpentelemetry = exports.setupSDK = exports.configFromEnv = exports.withSpan = exports.withRootContext = exports.ValueType = exports.SpanStatusCode = exports.recordSpanError = exports.injectTraceHeaders = exports.getTraceHeaders = exports.currentTraceId = exports.currentSpan = exports.currentContext = exports.createObservableGauge = exports.createHistogram = exports.createGauge = exports.createCounter = exports.CreateSpan = exports.addSpanAttributes = void 0;
|
|
4
|
+
var api_1 = require("./api");
|
|
5
|
+
Object.defineProperty(exports, "addSpanAttributes", { enumerable: true, get: function () { return api_1.addSpanAttributes; } });
|
|
6
|
+
Object.defineProperty(exports, "CreateSpan", { enumerable: true, get: function () { return api_1.CreateSpan; } });
|
|
7
|
+
Object.defineProperty(exports, "createCounter", { enumerable: true, get: function () { return api_1.createCounter; } });
|
|
8
|
+
Object.defineProperty(exports, "createGauge", { enumerable: true, get: function () { return api_1.createGauge; } });
|
|
9
|
+
// Metrics
|
|
10
|
+
Object.defineProperty(exports, "createHistogram", { enumerable: true, get: function () { return api_1.createHistogram; } });
|
|
11
|
+
Object.defineProperty(exports, "createObservableGauge", { enumerable: true, get: function () { return api_1.createObservableGauge; } });
|
|
12
|
+
Object.defineProperty(exports, "currentContext", { enumerable: true, get: function () { return api_1.currentContext; } });
|
|
13
|
+
// Tracing
|
|
14
|
+
Object.defineProperty(exports, "currentSpan", { enumerable: true, get: function () { return api_1.currentSpan; } });
|
|
15
|
+
Object.defineProperty(exports, "currentTraceId", { enumerable: true, get: function () { return api_1.currentTraceId; } });
|
|
16
|
+
// Propagation
|
|
17
|
+
Object.defineProperty(exports, "getTraceHeaders", { enumerable: true, get: function () { return api_1.getTraceHeaders; } });
|
|
18
|
+
Object.defineProperty(exports, "injectTraceHeaders", { enumerable: true, get: function () { return api_1.injectTraceHeaders; } });
|
|
19
|
+
Object.defineProperty(exports, "recordSpanError", { enumerable: true, get: function () { return api_1.recordSpanError; } });
|
|
20
|
+
Object.defineProperty(exports, "SpanStatusCode", { enumerable: true, get: function () { return api_1.SpanStatusCode; } });
|
|
21
|
+
Object.defineProperty(exports, "ValueType", { enumerable: true, get: function () { return api_1.ValueType; } });
|
|
22
|
+
Object.defineProperty(exports, "withRootContext", { enumerable: true, get: function () { return api_1.withRootContext; } });
|
|
23
|
+
Object.defineProperty(exports, "withSpan", { enumerable: true, get: function () { return api_1.withSpan; } });
|
|
24
|
+
var setup_1 = require("./setup");
|
|
25
|
+
Object.defineProperty(exports, "configFromEnv", { enumerable: true, get: function () { return setup_1.configFromEnv; } });
|
|
26
|
+
// Setup
|
|
27
|
+
Object.defineProperty(exports, "setupSDK", { enumerable: true, get: function () { return setup_1.setupSDK; } });
|
|
28
|
+
Object.defineProperty(exports, "shutdownOpentelemetry", { enumerable: true, get: function () { return setup_1.shutdownOpentelemetry; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pino.d.ts","sourceRoot":"","sources":["../../src/opentelemetry/pino.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAA;AAEzE,wBAAgB,wBAAwB,wBAIvC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setupPinoInstrumentation = setupPinoInstrumentation;
|
|
4
|
+
const instrumentation_pino_1 = require("@opentelemetry/instrumentation-pino");
|
|
5
|
+
function setupPinoInstrumentation() {
|
|
6
|
+
return new instrumentation_pino_1.PinoInstrumentation({
|
|
7
|
+
disableLogSending: true,
|
|
8
|
+
});
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prisma.d.ts","sourceRoot":"","sources":["../../src/opentelemetry/prisma.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AAE/D,wBAAgB,0BAA0B,0BAEzC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setupPrismaInstrumentation = setupPrismaInstrumentation;
|
|
4
|
+
const instrumentation_1 = require("@prisma/instrumentation");
|
|
5
|
+
function setupPrismaInstrumentation() {
|
|
6
|
+
return new instrumentation_1.PrismaInstrumentation();
|
|
7
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface SDKConfig {
|
|
2
|
+
serviceName: string;
|
|
3
|
+
version?: string;
|
|
4
|
+
env?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function configFromEnv(): Pick<SDKConfig, 'version' | 'env'>;
|
|
7
|
+
export declare function setupSDK(config: SDKConfig): void;
|
|
8
|
+
export declare function shutdownOpentelemetry(): Promise<void>;
|
|
9
|
+
//# sourceMappingURL=setup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/opentelemetry/setup.ts"],"names":[],"mappings":"AAsBA,MAAM,WAAW,SAAS;IACxB,WAAW,EAAE,MAAM,CAAA;IACnB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,GAAG,CAAC,EAAE,MAAM,CAAA;CACb;AAED,wBAAgB,aAAa,IAAI,IAAI,CAAC,SAAS,EAAE,SAAS,GAAG,KAAK,CAAC,CAKlE;AAMD,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,QAkEzC;AAED,wBAAsB,qBAAqB,kBAI1C"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.configFromEnv = configFromEnv;
|
|
4
|
+
exports.setupSDK = setupSDK;
|
|
5
|
+
exports.shutdownOpentelemetry = shutdownOpentelemetry;
|
|
6
|
+
const api_1 = require("@opentelemetry/api");
|
|
7
|
+
const context_async_hooks_1 = require("@opentelemetry/context-async-hooks");
|
|
8
|
+
const exporter_metrics_otlp_grpc_1 = require("@opentelemetry/exporter-metrics-otlp-grpc");
|
|
9
|
+
const exporter_trace_otlp_grpc_1 = require("@opentelemetry/exporter-trace-otlp-grpc");
|
|
10
|
+
const host_metrics_1 = require("@opentelemetry/host-metrics");
|
|
11
|
+
const otlp_exporter_base_1 = require("@opentelemetry/otlp-exporter-base");
|
|
12
|
+
const resources_1 = require("@opentelemetry/resources");
|
|
13
|
+
const sdk_metrics_1 = require("@opentelemetry/sdk-metrics");
|
|
14
|
+
const sdk_node_1 = require("@opentelemetry/sdk-node");
|
|
15
|
+
const sdk_trace_node_1 = require("@opentelemetry/sdk-trace-node");
|
|
16
|
+
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
|
|
17
|
+
const opentelemetry_instrumentation_viem_1 = require("@pimlico/opentelemetry-instrumentation-viem");
|
|
18
|
+
const amqp_1 = require("./amqp");
|
|
19
|
+
const api_2 = require("./api");
|
|
20
|
+
const express_1 = require("./express");
|
|
21
|
+
const http_1 = require("./http");
|
|
22
|
+
const pino_1 = require("./pino");
|
|
23
|
+
const prisma_1 = require("./prisma");
|
|
24
|
+
function configFromEnv() {
|
|
25
|
+
return {
|
|
26
|
+
version: process.env.VERSION ?? '0.0.1',
|
|
27
|
+
env: process.env.DEPLOYMENT_ENV ?? 'local-dev',
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
let instance;
|
|
31
|
+
let deploymentTime;
|
|
32
|
+
function setupSDK(config) {
|
|
33
|
+
if (instance)
|
|
34
|
+
return;
|
|
35
|
+
// url should be controlled by OTEL_EXPORTER_OTLP_ENDPOINT as per OTEL spec
|
|
36
|
+
// Ensure AsyncHooksContextManager is being used
|
|
37
|
+
api_1.context.setGlobalContextManager(new context_async_hooks_1.AsyncLocalStorageContextManager().enable());
|
|
38
|
+
const traceExporter = new exporter_trace_otlp_grpc_1.OTLPTraceExporter({
|
|
39
|
+
compression: otlp_exporter_base_1.CompressionAlgorithm.GZIP,
|
|
40
|
+
});
|
|
41
|
+
const metricExporter = new exporter_metrics_otlp_grpc_1.OTLPMetricExporter({
|
|
42
|
+
compression: otlp_exporter_base_1.CompressionAlgorithm.GZIP,
|
|
43
|
+
});
|
|
44
|
+
instance = new sdk_node_1.NodeSDK({
|
|
45
|
+
spanProcessors: [
|
|
46
|
+
new sdk_trace_node_1.BatchSpanProcessor(traceExporter, {
|
|
47
|
+
maxQueueSize: 4096,
|
|
48
|
+
maxExportBatchSize: 1024,
|
|
49
|
+
scheduledDelayMillis: 1000,
|
|
50
|
+
exportTimeoutMillis: 30000,
|
|
51
|
+
}),
|
|
52
|
+
],
|
|
53
|
+
metricReader: new sdk_metrics_1.PeriodicExportingMetricReader({
|
|
54
|
+
exporter: metricExporter,
|
|
55
|
+
}),
|
|
56
|
+
instrumentations: [
|
|
57
|
+
(0, http_1.setupHttpInstrumentation)(),
|
|
58
|
+
(0, express_1.setupExpressInstrumentation)(),
|
|
59
|
+
(0, amqp_1.setupAmqpInstrumentation)(),
|
|
60
|
+
(0, prisma_1.setupPrismaInstrumentation)(),
|
|
61
|
+
(0, pino_1.setupPinoInstrumentation)(),
|
|
62
|
+
new opentelemetry_instrumentation_viem_1.ViemInstrumentation({
|
|
63
|
+
requireParentSpan: true,
|
|
64
|
+
captureOperationResult: true,
|
|
65
|
+
}),
|
|
66
|
+
],
|
|
67
|
+
serviceName: config.serviceName,
|
|
68
|
+
resource: (0, resources_1.resourceFromAttributes)({
|
|
69
|
+
[semantic_conventions_1.ATTR_SERVICE_NAME]: config.serviceName,
|
|
70
|
+
[semantic_conventions_1.ATTR_SERVICE_VERSION]: config.version ?? '0.0.1',
|
|
71
|
+
'deployment.environment.name': config.env ?? 'local-dev',
|
|
72
|
+
}),
|
|
73
|
+
});
|
|
74
|
+
instance.start();
|
|
75
|
+
const hostMetrics = new host_metrics_1.HostMetrics();
|
|
76
|
+
hostMetrics.start();
|
|
77
|
+
const startupTime = Date.now();
|
|
78
|
+
deploymentTime = (0, api_2.createObservableGauge)('startup_time', {
|
|
79
|
+
description: 'Service start timestamp in milliseconds',
|
|
80
|
+
valueType: api_1.ValueType.INT,
|
|
81
|
+
});
|
|
82
|
+
deploymentTime.addCallback((observableResult) => {
|
|
83
|
+
observableResult.observe(startupTime);
|
|
84
|
+
});
|
|
85
|
+
// Auto-flush telemetry when the event loop drains (after service cleanup)
|
|
86
|
+
process.on('beforeExit', async () => {
|
|
87
|
+
await shutdownOpentelemetry();
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
async function shutdownOpentelemetry() {
|
|
91
|
+
if (!instance)
|
|
92
|
+
return;
|
|
93
|
+
await instance.shutdown();
|
|
94
|
+
instance = undefined;
|
|
95
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rhinestone/service-sdk",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Shared SDK for Rhinestone services",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Rhinestone",
|
|
7
|
+
"url": "https://rhinestone.dev"
|
|
8
|
+
},
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"main": "dist/index.js",
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
"./*": "./dist/*.js"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"clean": "rimraf dist",
|
|
22
|
+
"compile": "tsc",
|
|
23
|
+
"build": "bun run clean && bun run compile",
|
|
24
|
+
"check": "biome check .",
|
|
25
|
+
"check:fix": "biome check --write .",
|
|
26
|
+
"test": "bun test"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@opentelemetry/api": "1.9.0",
|
|
30
|
+
"@opentelemetry/context-async-hooks": "2.6.0",
|
|
31
|
+
"@opentelemetry/exporter-metrics-otlp-grpc": "0.201.1",
|
|
32
|
+
"@opentelemetry/exporter-trace-otlp-grpc": "0.201.1",
|
|
33
|
+
"@opentelemetry/host-metrics": "0.38.3",
|
|
34
|
+
"@opentelemetry/instrumentation-amqplib": "0.46.1",
|
|
35
|
+
"@opentelemetry/instrumentation-express": "0.47.1",
|
|
36
|
+
"@opentelemetry/instrumentation-http": "0.201.1",
|
|
37
|
+
"@opentelemetry/instrumentation-pino": "0.48.0",
|
|
38
|
+
"@opentelemetry/otlp-exporter-base": "0.201.1",
|
|
39
|
+
"@opentelemetry/resources": "2.6.0",
|
|
40
|
+
"@opentelemetry/sdk-metrics": "2.6.0",
|
|
41
|
+
"@opentelemetry/sdk-node": "0.201.1",
|
|
42
|
+
"@opentelemetry/sdk-trace-node": "2.6.0",
|
|
43
|
+
"@opentelemetry/semantic-conventions": "1.40.0",
|
|
44
|
+
"@pimlico/opentelemetry-instrumentation-viem": "0.0.4",
|
|
45
|
+
"@prisma/instrumentation": "6.19.2"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@biomejs/biome": "^2.1.4",
|
|
49
|
+
"@types/bun": "latest",
|
|
50
|
+
"rimraf": "^6.0.1",
|
|
51
|
+
"typescript": "^5.6.3",
|
|
52
|
+
"viem": "^2.46.1"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"typescript": "^5",
|
|
56
|
+
"viem": "^2.40.1"
|
|
57
|
+
}
|
|
58
|
+
}
|