@agoric/telemetry 0.6.3-other-dev-3eb1a1d.0 → 0.6.3-other-dev-fbe72e7.0.fbe72e7

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.
@@ -1,13 +1,13 @@
1
1
  import path from 'path';
2
2
  import tmp from 'tmp';
3
- import { PromiseAllOrErrors } from '@agoric/internal';
3
+ import { PromiseAllOrErrors, unprefixedProperties } from '@agoric/internal';
4
4
  import { serializeSlogObj } from './serialize-slog-obj.js';
5
5
 
6
+ export const DEFAULT_SLOGSENDER_AGENT = 'self';
6
7
  export const DEFAULT_SLOGSENDER_MODULE =
7
8
  '@agoric/telemetry/src/flight-recorder.js';
8
9
  export const SLOGFILE_SENDER_MODULE = '@agoric/telemetry/src/slog-file.js';
9
-
10
- export const DEFAULT_SLOGSENDER_AGENT = 'self';
10
+ export const PROMETHEUS_SENDER_MODULE = '@agoric/telemetry/src/prometheus.js';
11
11
 
12
12
  /** @import {SlogSender} from './index.js' */
13
13
 
@@ -19,6 +19,23 @@ export const DEFAULT_SLOGSENDER_AGENT = 'self';
19
19
  const filterTruthy = arr => /** @type {any[]} */ (arr.filter(Boolean));
20
20
 
21
21
  /**
22
+ * Create an aggregate slog sender that fans out inbound slog entries to modules
23
+ * as indicated by variables in the supplied `env` option. The SLOGSENDER value
24
+ * (or a default DEFAULT_SLOGSENDER_MODULE defined above) is split on commas
25
+ * into a list of module identifiers and adjusted by automatic insertions (a
26
+ * non-empty SLOGFILE value inserts DEFAULT_SLOGSENDER_AGENT defined above), and
27
+ * then each identifier is dynamically `import`ed for its own `makeSlogSender`
28
+ * export, which is invoked with a non-empty `stateDir` option and a modified
29
+ * `env` in which SLOGSENDER_AGENT_* variables have overridden their unprefixed
30
+ * equivalents to produce a subordinate slog sender.
31
+ * Subordinate slog senders remain isolated from each other, and any errors from
32
+ * them are caught and held until the next `forceFlush()` without disrupting
33
+ * any remaining slog entry fanout.
34
+ * If SLOGSENDER_AGENT is 'process', 'slog-sender-pipe.js' is used to load the
35
+ * subordinates in a child process rather than the main process.
36
+ * When there are no subordinates, the return value will be `undefined` rather
37
+ * than a slog sender function.
38
+ *
22
39
  * @type {import('./index.js').MakeSlogSender}
23
40
  */
24
41
  export const makeSlogSender = async (opts = {}) => {
@@ -26,95 +43,95 @@ export const makeSlogSender = async (opts = {}) => {
26
43
  const {
27
44
  SLOGSENDER = DEFAULT_SLOGSENDER_MODULE,
28
45
  SLOGSENDER_AGENT = DEFAULT_SLOGSENDER_AGENT,
46
+ // While cosmic-swingset/kernel code includes its own Prometheus metrics
47
+ // export, that trumps a slog sender module doing so.
48
+ // This extraction can be removed when that changes, but in the meantime,
49
+ // opt-in is only by SLOGSENDER_AGENT_OTEL_EXPORTER_PROMETHEUS_PORT.
50
+ OTEL_EXPORTER_PROMETHEUS_PORT: _prometheusExportPort,
29
51
  ...otherEnv
30
52
  } = env;
31
53
 
32
54
  const agentEnv = {
33
55
  ...otherEnv,
34
- ...Object.fromEntries(
35
- Object.entries(otherEnv)
36
- .filter(([k]) => k.match(/^(?:SLOGSENDER_AGENT_)+/)) // narrow to SLOGSENDER_AGENT_ prefixes.
37
- .map(([k, v]) => [k.replace(/^(?:SLOGSENDER_AGENT_)+/, ''), v]), // Rewrite SLOGSENDER_AGENT_ to un-prefixed version.
38
- ),
56
+ ...unprefixedProperties(otherEnv, 'SLOGSENDER_AGENT_'),
39
57
  };
40
58
 
41
- const slogSenderModules = [
42
- ...new Set([
43
- ...(agentEnv.SLOGFILE ? [SLOGFILE_SENDER_MODULE] : []),
44
- ...SLOGSENDER.split(',')
45
- .filter(Boolean)
46
- .map(modulePath =>
47
- modulePath.startsWith('.')
48
- ? // Resolve relative to the current working directory.
49
- path.resolve(modulePath)
50
- : modulePath,
51
- ),
52
- ]),
53
- ];
54
-
55
- if (!slogSenderModules.length) {
59
+ const slogSenderModules = new Set();
60
+ if (agentEnv.OTEL_EXPORTER_PROMETHEUS_PORT) {
61
+ slogSenderModules.add(PROMETHEUS_SENDER_MODULE);
62
+ }
63
+ if (agentEnv.SLOGFILE) {
64
+ slogSenderModules.add(SLOGFILE_SENDER_MODULE);
65
+ }
66
+ for (const moduleIdentifier of filterTruthy(SLOGSENDER.split(','))) {
67
+ if (moduleIdentifier.startsWith('-')) {
68
+ // Opt out of an automatically-included sender.
69
+ slogSenderModules.delete(moduleIdentifier.slice(1));
70
+ } else if (moduleIdentifier.startsWith('.')) {
71
+ // Resolve relative to the current working directory.
72
+ slogSenderModules.add(path.resolve(moduleIdentifier));
73
+ } else {
74
+ slogSenderModules.add(moduleIdentifier);
75
+ }
76
+ }
77
+
78
+ if (!slogSenderModules.size) {
56
79
  return undefined;
57
80
  }
58
81
 
59
- switch (SLOGSENDER_AGENT) {
60
- case '':
61
- case 'self':
62
- break;
63
- case 'process': {
64
- console.warn('Loading slog sender in subprocess');
65
- return import('./slog-sender-pipe.js').then(
66
- async ({ makeSlogSender: makeSogSenderPipe }) =>
67
- makeSogSenderPipe({
68
- env: {
69
- ...agentEnv,
70
- SLOGSENDER,
71
- SLOGSENDER_AGENT: 'self',
72
- },
73
- stateDir: stateDirOption,
74
- ...otherOpts,
75
- }),
76
- );
77
- }
78
- case 'worker':
79
- default:
80
- console.warn(`Unknown SLOGSENDER_AGENT=${SLOGSENDER_AGENT}`);
82
+ if (SLOGSENDER_AGENT === 'process') {
83
+ console.warn('Loading slog sender in subprocess');
84
+ return import('./slog-sender-pipe.js').then(async module =>
85
+ module.makeSlogSender({
86
+ env: {
87
+ ...agentEnv,
88
+ SLOGSENDER,
89
+ SLOGSENDER_AGENT: 'self',
90
+ },
91
+ stateDir: stateDirOption,
92
+ ...otherOpts,
93
+ }),
94
+ );
95
+ } else if (SLOGSENDER_AGENT && SLOGSENDER_AGENT !== 'self') {
96
+ console.warn(
97
+ `Unknown SLOGSENDER_AGENT=${SLOGSENDER_AGENT}; defaulting to 'self'`,
98
+ );
81
99
  }
82
100
 
83
101
  if (SLOGSENDER) {
84
102
  console.warn('Loading slog sender modules:', ...slogSenderModules);
85
103
  }
86
104
 
87
- const makersInfo = await Promise.all(
88
- slogSenderModules.map(async moduleIdentifier =>
89
- import(moduleIdentifier)
90
- .then(
91
- /** @param {{makeSlogSender: import('./index.js').MakeSlogSender}} module */ ({
92
- makeSlogSender: maker,
93
- }) => {
94
- if (typeof maker !== 'function') {
95
- return Promise.reject(
96
- Error(`No 'makeSlogSender' function exported by module`),
97
- );
98
- } else if (maker === makeSlogSender) {
99
- return Promise.reject(
100
- Error(`Cannot recursively load 'makeSlogSender' aggregator`),
101
- );
102
- }
103
-
104
- return /** @type {const} */ ([maker, moduleIdentifier]);
105
- },
106
- )
107
- .catch(err => {
105
+ /** @type {Map<import('./index.js').MakeSlogSender, string>} */
106
+ const makerMap = new Map();
107
+ await Promise.all(
108
+ [...slogSenderModules].map(async moduleIdentifier => {
109
+ await null;
110
+ try {
111
+ const module = await import(moduleIdentifier);
112
+ const { makeSlogSender: maker } = module;
113
+ if (typeof maker !== 'function') {
114
+ throw Error(`No 'makeSlogSender' function exported by module`);
115
+ } else if (maker === makeSlogSender) {
116
+ throw Error(`Cannot recursively load 'makeSlogSender' aggregator`);
117
+ }
118
+ const isReplacing = makerMap.get(maker);
119
+ if (isReplacing) {
108
120
  console.warn(
109
- `Failed to load slog sender from ${moduleIdentifier}.`,
110
- err,
121
+ `The slog sender from ${moduleIdentifier} matches the one from ${isReplacing}.`,
111
122
  );
112
- return undefined;
113
- }),
114
- ),
115
- ).then(makerEntries => [...new Map(filterTruthy(makerEntries)).entries()]);
123
+ }
124
+ makerMap.set(maker, moduleIdentifier);
125
+ } catch (err) {
126
+ console.warn(
127
+ `Failed to load slog sender from ${moduleIdentifier}.`,
128
+ err,
129
+ );
130
+ }
131
+ }),
132
+ );
116
133
 
117
- if (!makersInfo.length) {
134
+ if (!makerMap.size) {
118
135
  return undefined;
119
136
  }
120
137
 
@@ -122,11 +139,11 @@ export const makeSlogSender = async (opts = {}) => {
122
139
 
123
140
  if (stateDir === undefined) {
124
141
  stateDir = tmp.dirSync().name;
125
- console.warn(`Using ${stateDir} for stateDir`);
142
+ console.warn(`Using ${stateDir} for slog sender stateDir`);
126
143
  }
127
144
 
128
145
  const senders = await Promise.all(
129
- makersInfo.map(async ([maker, moduleIdentifier]) =>
146
+ [...makerMap.entries()].map(async ([maker, moduleIdentifier]) =>
130
147
  maker({
131
148
  ...otherOpts,
132
149
  stateDir,
@@ -137,37 +154,37 @@ export const makeSlogSender = async (opts = {}) => {
137
154
 
138
155
  if (!senders.length) {
139
156
  return undefined;
140
- } else {
141
- // Optimize creating a JSON serialization only if needed
142
- // by any of the sender modules
143
- const hasSenderUsingJsonObj = senders.some(
144
- ({ usesJsonObject = true }) => usesJsonObject,
145
- );
146
- const getJsonObj = hasSenderUsingJsonObj
147
- ? serializeSlogObj
148
- : () => undefined;
149
- const sendErrors = [];
150
- /** @type {SlogSender} */
151
- const slogSender = (slogObj, jsonObj = getJsonObj(slogObj)) => {
152
- for (const sender of senders) {
153
- try {
154
- sender(slogObj, jsonObj);
155
- } catch (err) {
156
- sendErrors.push(err);
157
- }
158
- }
159
- };
160
- return Object.assign(slogSender, {
161
- forceFlush: async () =>
162
- PromiseAllOrErrors([
163
- ...senders.map(sender => sender.forceFlush?.()),
164
- ...sendErrors.splice(0).map(err => Promise.reject(err)),
165
- ]).then(() => {}),
166
- shutdown: async () =>
167
- PromiseAllOrErrors(senders.map(sender => sender.shutdown?.())).then(
168
- () => {},
169
- ),
170
- usesJsonObject: hasSenderUsingJsonObj,
171
- });
172
157
  }
158
+
159
+ // Optimize creating a JSON serialization only if needed
160
+ // by at least one of the senders.
161
+ const hasSenderUsingJsonObj = senders.some(
162
+ ({ usesJsonObject = true }) => usesJsonObject,
163
+ );
164
+ const getJsonObj = hasSenderUsingJsonObj ? serializeSlogObj : () => undefined;
165
+
166
+ const sendErrors = [];
167
+
168
+ /** @type {SlogSender} */
169
+ const slogSender = (slogObj, jsonObj = getJsonObj(slogObj)) => {
170
+ for (const sender of senders) {
171
+ try {
172
+ sender(slogObj, jsonObj);
173
+ } catch (err) {
174
+ sendErrors.push(err);
175
+ }
176
+ }
177
+ };
178
+ return Object.assign(slogSender, {
179
+ forceFlush: async () => {
180
+ await PromiseAllOrErrors([
181
+ ...senders.map(sender => sender.forceFlush?.()),
182
+ ...sendErrors.splice(0).map(err => Promise.reject(err)),
183
+ ]);
184
+ },
185
+ shutdown: async () => {
186
+ await PromiseAllOrErrors(senders.map(sender => sender.shutdown?.()));
187
+ },
188
+ usesJsonObject: hasSenderUsingJsonObj,
189
+ });
173
190
  };
@@ -82,9 +82,9 @@ export const makeSlogSender = async options => {
82
82
  * @param {import('./context-aware-slog.js').Slog} slog
83
83
  */
84
84
  const slogSender = slog => {
85
- const { timestamp, ...logRecord } = contextualSlogProcessor(slog);
85
+ const { time, ...logRecord } = contextualSlogProcessor(slog);
86
86
 
87
- const [secondsStr, fractionStr] = String(timestamp).split('.');
87
+ const [secondsStr, fractionStr] = String(time).split('.');
88
88
  const seconds = parseInt(secondsStr, 10);
89
89
  const nanoSeconds = parseInt(
90
90
  (fractionStr || String(0)).padEnd(9, String(0)).slice(0, 9),
@@ -0,0 +1,229 @@
1
+ import { q, Fail } from '@endo/errors';
2
+
3
+ import * as ActionType from '@agoric/internal/src/action-types.js';
4
+ import { objectMapMutable } from '@agoric/internal/src/js-utils.js';
5
+ import {
6
+ HISTOGRAM_METRICS,
7
+ BLOCK_HISTOGRAM_METRICS,
8
+ KERNEL_STATS_METRICS,
9
+ makeQueueMetrics,
10
+ } from '@agoric/internal/src/metrics.js';
11
+
12
+ /**
13
+ * @import {MetricOptions, ObservableCounter, ObservableUpDownCounter} from '@opentelemetry/api';
14
+ * @import {MeterProvider} from '@opentelemetry/sdk-metrics';
15
+ * @import {TotalMap} from '@agoric/internal';
16
+ */
17
+
18
+ const knownActionTypes = new Set(Object.values(ActionType.QueuedActionType));
19
+
20
+ /** @param {import('./index.js').MakeSlogSenderOptions & {otelMeterName: string, otelMeterProvider?: MeterProvider}} opts */
21
+ export const makeSlogSender = async (opts = /** @type {any} */ ({})) => {
22
+ const { otelMeterName, otelMeterProvider } = opts;
23
+ if (!otelMeterName) throw Fail`OTel meter name is required`;
24
+ if (!otelMeterProvider) return;
25
+
26
+ const shutdown = async () => {
27
+ await otelMeterProvider.shutdown();
28
+ };
29
+
30
+ const otelMeter = otelMeterProvider.getMeter(otelMeterName);
31
+
32
+ const processedInboundActionCounter = otelMeter.createCounter(
33
+ 'cosmic_swingset_inbound_actions',
34
+ { description: 'Processed inbound action counts by type' },
35
+ );
36
+ const histograms = {
37
+ ...objectMapMutable(HISTOGRAM_METRICS, (desc, name) => {
38
+ const { boundaries, ...options } = desc;
39
+ const advice = boundaries && { explicitBucketBoundaries: boundaries };
40
+ return otelMeter.createHistogram(name, { ...options, advice });
41
+ }),
42
+ ...objectMapMutable(BLOCK_HISTOGRAM_METRICS, (desc, name) =>
43
+ otelMeter.createHistogram(name, desc),
44
+ ),
45
+ };
46
+
47
+ const inboundQueueMetrics = makeQueueMetrics({
48
+ otelMeter,
49
+ namePrefix: 'cosmic_swingset_inbound_queue',
50
+ descPrefix: 'inbound queue',
51
+ console,
52
+ });
53
+
54
+ // Values for KERNEL_STATS_METRICS could be built up locally by observing slog
55
+ // entries, but they are all collectively reported in "kernel-stats"
56
+ // (@see {@link ../../cosmic-swingset/src/kernel-stats.js exportKernelStats})
57
+ // and for now we just reflect that, which requires implementation as async
58
+ // ("observable") instruments rather than synchronous ones.
59
+ /** @typedef {string} KernelStatsKey */
60
+ /** @typedef {string} KernelMetricName */
61
+ /** @type {TotalMap<KernelStatsKey, number>} */
62
+ const kernelStats = new Map();
63
+ /** @type {Map<KernelMetricName, ObservableCounter | ObservableUpDownCounter>} */
64
+ const kernelStatsCounters = new Map();
65
+ for (const meta of KERNEL_STATS_METRICS) {
66
+ const { key, name, sub, metricType, ...options } = meta;
67
+ kernelStats.set(key, 0);
68
+ if (metricType === 'gauge') {
69
+ kernelStats.set(`${key}Up`, 0);
70
+ kernelStats.set(`${key}Down`, 0);
71
+ kernelStats.set(`${key}Max`, 0);
72
+ } else if (metricType !== 'counter') {
73
+ Fail`Unknown metric type ${q(metricType)} for key ${q(key)} name ${q(name)}`;
74
+ }
75
+ let counter = kernelStatsCounters.get(name);
76
+ if (!counter) {
77
+ counter =
78
+ metricType === 'counter'
79
+ ? otelMeter.createObservableCounter(name, options)
80
+ : otelMeter.createObservableUpDownCounter(name, options);
81
+ kernelStatsCounters.set(name, counter);
82
+ }
83
+ const attributes = sub ? { [sub.dimension]: sub.value } : {};
84
+ counter.addCallback(observer => {
85
+ observer.observe(kernelStats.get(key), attributes);
86
+ });
87
+ }
88
+ const expectedKernelStats = new Set(kernelStats.keys());
89
+
90
+ /**
91
+ * @typedef {object} LazyStats
92
+ * @property {string} namePrefix
93
+ * @property {MetricOptions} options
94
+ * @property {Set<string>} keys
95
+ * @property {Record<string, number>} data
96
+ */
97
+ /** @type {(namePrefix: string, description: string) => LazyStats} */
98
+ const makeLazyStats = (namePrefix, description) => {
99
+ return { namePrefix, options: { description }, keys: new Set(), data: {} };
100
+ };
101
+ const dynamicAfterCommitStatsCounters = {
102
+ memoryUsage: makeLazyStats(
103
+ 'memoryUsage_',
104
+ 'kernel process memory statistic',
105
+ ),
106
+ heapStats: makeLazyStats('heapStats_', 'v8 kernel heap statistic'),
107
+ };
108
+
109
+ const slogSender = ({ type: slogType, ...slogObj }) => {
110
+ switch (slogType) {
111
+ // Consume cosmic-swingset block lifecycle slog entries.
112
+ case 'cosmic-swingset-init': {
113
+ const { inboundQueueInitialLengths: lengths } = slogObj;
114
+ inboundQueueMetrics.initLengths(lengths);
115
+ break;
116
+ }
117
+ case 'cosmic-swingset-begin-block': {
118
+ const {
119
+ interBlockSeconds,
120
+ afterCommitHangoverSeconds,
121
+ blockLagSeconds,
122
+ } = slogObj;
123
+
124
+ Number.isFinite(interBlockSeconds) &&
125
+ histograms.interBlockSeconds.record(interBlockSeconds);
126
+ histograms.afterCommitHangoverSeconds.record(
127
+ afterCommitHangoverSeconds,
128
+ );
129
+ Number.isFinite(blockLagSeconds) &&
130
+ histograms.blockLagSeconds.record(blockLagSeconds);
131
+ break;
132
+ }
133
+ case 'cosmic-swingset-run-finish': {
134
+ histograms.swingset_block_processing_seconds.record(slogObj.seconds);
135
+ break;
136
+ }
137
+ case 'cosmic-swingset-end-block-finish': {
138
+ const { inboundQueueStartLengths, processedActionCounts } = slogObj;
139
+ inboundQueueMetrics.updateLengths(inboundQueueStartLengths);
140
+ for (const processedActionRecord of processedActionCounts) {
141
+ const { count, phase, type: actionType } = processedActionRecord;
142
+ if (!knownActionTypes.has(actionType)) {
143
+ console.warn('Unknown inbound action type', actionType);
144
+ }
145
+ processedInboundActionCounter.add(count, { actionType });
146
+ inboundQueueMetrics.decLength(phase);
147
+ }
148
+ break;
149
+ }
150
+ case 'cosmic-swingset-commit-block-finish': {
151
+ const {
152
+ runSeconds,
153
+ chainTime,
154
+ saveTime,
155
+ cosmosCommitSeconds,
156
+ fullSaveTime,
157
+ } = slogObj;
158
+ histograms.swingsetRunSeconds.record(runSeconds);
159
+ histograms.swingsetChainSaveSeconds.record(chainTime);
160
+ histograms.swingsetCommitSeconds.record(saveTime);
161
+ histograms.cosmosCommitSeconds.record(cosmosCommitSeconds);
162
+ histograms.fullCommitSeconds.record(fullSaveTime);
163
+ break;
164
+ }
165
+
166
+ // Consume Swingset kernel slog entries.
167
+ case 'vat-startup-finish': {
168
+ histograms.swingset_vat_startup.record(slogObj.seconds * 1000);
169
+ break;
170
+ }
171
+ case 'crank-finish': {
172
+ const { crankType, messageType, seconds } = slogObj;
173
+ // TODO: Reflect crankType/messageType as proper dimensional attributes.
174
+ // For now, we're going for parity with direct metrics.
175
+ if (crankType !== 'routing' && messageType !== 'create-vat') {
176
+ histograms.swingset_crank_processing_time.record(seconds * 1000);
177
+ }
178
+ break;
179
+ }
180
+
181
+ // Consume miscellaneous slog entries.
182
+ case 'kernel-stats': {
183
+ const { stats } = slogObj;
184
+ const notYetFoundKernelStats = new Set(expectedKernelStats);
185
+ for (const [key, value] of Object.entries(stats)) {
186
+ notYetFoundKernelStats.delete(key);
187
+ if (!kernelStats.has(key)) {
188
+ console.warn('Unexpected SwingSet kernel statistic', key);
189
+ }
190
+ kernelStats.set(key, value);
191
+ }
192
+ if (notYetFoundKernelStats.size) {
193
+ console.warn('Expected SwingSet kernel statistics not found', [
194
+ ...notYetFoundKernelStats,
195
+ ]);
196
+ }
197
+ break;
198
+ }
199
+ case 'cosmic-swingset-after-commit-stats': {
200
+ const dynamicCounterEntries = Object.entries(
201
+ dynamicAfterCommitStatsCounters,
202
+ );
203
+ for (const [slogKey, meta] of dynamicCounterEntries) {
204
+ const { namePrefix, options, keys } = meta;
205
+ meta.data = slogObj[slogKey] || {};
206
+ const newKeys = Object.keys(meta.data).filter(key => !keys.has(key));
207
+ for (const key of newKeys) {
208
+ keys.add(key);
209
+ const name = `${namePrefix}${key}`;
210
+ const gauge = otelMeter.createObservableUpDownCounter(
211
+ name,
212
+ options,
213
+ );
214
+ gauge.addCallback(observer => {
215
+ observer.observe(meta.data[key]);
216
+ });
217
+ }
218
+ }
219
+ break;
220
+ }
221
+ default:
222
+ break;
223
+ }
224
+ };
225
+ return Object.assign(slogSender, {
226
+ shutdown,
227
+ usesJsonObject: false,
228
+ });
229
+ };
package/src/otel-trace.js CHANGED
@@ -22,16 +22,26 @@ export const SPAN_EXPORT_DELAY_MS = 1_000;
22
22
  export const makeOtelTracingProvider = opts => {
23
23
  const { env = process.env } = opts || {};
24
24
 
25
+ // https://opentelemetry.io/docs/concepts/signals/
26
+ // https://opentelemetry.io/docs/specs/otel/protocol/exporter/#endpoint-urls-for-otlphttp
27
+ // https://github.com/open-telemetry/opentelemetry-js/blob/experimental/v0.57.1/experimental/packages/exporter-trace-otlp-http/README.md#configuration-options-as-environment-variables
25
28
  const { OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT } =
26
29
  env;
27
30
  if (!OTEL_EXPORTER_OTLP_ENDPOINT && !OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) {
31
+ console.debug(
32
+ 'Not enabling OTLP Traces Exporter; enable with OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=<target URL> or OTEL_EXPORTER_OTLP_ENDPOINT=<target URL prefix>',
33
+ );
28
34
  return undefined;
29
35
  }
30
36
 
31
37
  const resource = new Resource(getResourceAttributes(opts));
32
38
 
33
39
  const exporter = new OTLPTraceExporter();
34
- console.info('Enabling OTLP Traces Exporter to', exporter.getDefaultUrl({}));
40
+ console.info(
41
+ 'Enabling OTLP Traces Exporter to',
42
+ OTEL_EXPORTER_OTLP_TRACES_ENDPOINT ||
43
+ `${OTEL_EXPORTER_OTLP_ENDPOINT}/v1/traces`,
44
+ );
35
45
 
36
46
  const provider = new BasicTracerProvider({ resource });
37
47
  provider.addSpanProcessor(
@@ -0,0 +1,18 @@
1
+ import { Fail } from '@endo/errors';
2
+
3
+ import { getPrometheusMeterProvider } from './index.js';
4
+ import { makeSlogSender as makeOtelMetricsSender } from './otel-metrics.js';
5
+
6
+ /** @param {import('./index.js').MakeSlogSenderOptions & {otelMeterName?: string}} opts */
7
+ export const makeSlogSender = async (opts = {}) => {
8
+ const { env, otelMeterName, serviceName } = opts;
9
+ if (!otelMeterName) throw Fail`OTel meter name is required`;
10
+ const otelMeterProvider = getPrometheusMeterProvider({
11
+ console,
12
+ env,
13
+ serviceName,
14
+ });
15
+ if (!otelMeterProvider) return;
16
+
17
+ return makeOtelMetricsSender({ ...opts, otelMeterName, otelMeterProvider });
18
+ };
@@ -1,4 +1,32 @@
1
- export const serializeSlogObj = slogObj =>
2
- JSON.stringify(slogObj, (_, arg) =>
3
- typeof arg === 'bigint' ? Number(arg) : arg,
4
- );
1
+ const { hasOwn } = Object;
2
+
3
+ const replacer = (_key, value) => {
4
+ switch (typeof value) {
5
+ case 'object': {
6
+ if (value instanceof Error) {
7
+ // Represent each error as a serialization-friendly
8
+ // { errorType, message, cause?, errors?, stack? } object
9
+ // (itself subject to recursive replacement, particularly in `cause` and
10
+ // `errors`).
11
+ const obj = { errorType: value.name, message: value.message };
12
+ if (hasOwn(value, 'cause')) obj.cause = value.cause;
13
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
14
+ // @ts-ignore TS2339 property "errors" is only on AggregateError
15
+ if (hasOwn(value, 'errors')) obj.errors = value.errors;
16
+ const stack = value.stack;
17
+ if (stack) obj.stack = stack;
18
+ return obj;
19
+ }
20
+ break;
21
+ }
22
+ case 'bigint':
23
+ // Represent each bigint as a JSON-serializable number, accepting the
24
+ // possible loss of precision.
25
+ return Number(value);
26
+ default:
27
+ break;
28
+ }
29
+ return value;
30
+ };
31
+
32
+ export const serializeSlogObj = slogObj => JSON.stringify(slogObj, replacer);