@hatchet-dev/typescript-sdk 1.18.0 → 1.19.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.
@@ -9,16 +9,28 @@
9
9
  * enable_hatchet_otel_collector option.
10
10
  */
11
11
  import type { ClientConfig } from '../clients/hatchet-client/client-config';
12
- type SdkTracerProvider = import('@opentelemetry/sdk-trace-base').BasicTracerProvider;
12
+ declare const BatchSpanProcessor: typeof import("@opentelemetry/sdk-trace-base").BatchSpanProcessor;
13
+ type ReadableSpan = import('@opentelemetry/sdk-trace-base').ReadableSpan;
14
+ type SdkSpan = import('@opentelemetry/sdk-trace-base').Span;
15
+ /**
16
+ * HatchetAttributeSpanProcessor wraps a BatchSpanProcessor and injects
17
+ * hatchet.* attributes into every span created within a step run context.
18
+ * This ensures child spans are queryable by the same attributes (e.g.
19
+ * hatchet.step_run_id) as the parent span.
20
+ */
21
+ declare class HatchetAttributeSpanProcessor extends BatchSpanProcessor {
22
+ onStart(span: SdkSpan): void;
23
+ onEnd(span: ReadableSpan): void;
24
+ }
13
25
  export interface HatchetBspConfig {
14
26
  scheduledDelayMillis?: number;
15
27
  maxExportBatchSize?: number;
16
28
  maxQueueSize?: number;
17
29
  }
18
30
  /**
19
- * Adds the Hatchet OTLP exporter to the given TracerProvider.
20
- * The exporter sends spans to the Hatchet engine's collector endpoint
21
- * using the same connection settings as the Hatchet client.
31
+ * Creates a SpanProcessor that sends spans to the Hatchet engine's
32
+ * collector endpoint using the same connection settings as the Hatchet client.
33
+ * Pass the returned processor to BasicTracerProvider's `spanProcessors` option.
22
34
  */
23
- export declare function addHatchetExporter(tracerProvider: SdkTracerProvider, config: ClientConfig, bspConfig?: HatchetBspConfig): void;
35
+ export declare function createHatchetSpanProcessor(config: ClientConfig, bspConfig?: HatchetBspConfig): InstanceType<typeof HatchetAttributeSpanProcessor>;
24
36
  export {};
@@ -10,14 +10,23 @@
10
10
  * enable_hatchet_otel_collector option.
11
11
  */
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.addHatchetExporter = addHatchetExporter;
13
+ exports.createHatchetSpanProcessor = createHatchetSpanProcessor;
14
14
  const hatchet_span_context_1 = require("./hatchet-span-context");
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ let otelDiag;
17
+ try {
18
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
19
+ otelDiag = require('@opentelemetry/api').diag;
20
+ }
21
+ catch (_a) {
22
+ // best-effort
23
+ }
15
24
  try {
16
25
  require.resolve('@opentelemetry/exporter-trace-otlp-grpc');
17
26
  require.resolve('@opentelemetry/sdk-trace-base');
18
27
  require.resolve('@opentelemetry/core');
19
28
  }
20
- catch (_a) {
29
+ catch (_b) {
21
30
  throw new Error('To use HatchetInstrumentor with enableHatchetCollector, you must install: ' +
22
31
  'npm install @opentelemetry/exporter-trace-otlp-grpc @opentelemetry/sdk-trace-base @opentelemetry/core');
23
32
  }
@@ -38,24 +47,38 @@ class HatchetExporterWrapper {
38
47
  this.inner = inner;
39
48
  }
40
49
  export(spans, resultCallback) {
50
+ var _a;
41
51
  if (this.retryAt > 0 && Date.now() < this.retryAt) {
42
52
  resultCallback({ code: ExportResultCode.SUCCESS });
43
53
  return;
44
54
  }
45
- this.inner.export(spans, (result) => {
46
- var _a;
47
- if (result.code !== ExportResultCode.SUCCESS && result.error) {
48
- const err = result.error;
49
- if (err.code === GRPC_STATUS_UNIMPLEMENTED ||
50
- ((_a = err.message) === null || _a === void 0 ? void 0 : _a.toString().includes('UNIMPLEMENTED'))) {
51
- this.retryAt = Date.now() + RETRY_AFTER_MS;
52
- resultCallback({ code: ExportResultCode.SUCCESS });
53
- return;
55
+ try {
56
+ this.inner.export(spans, (result) => {
57
+ var _a;
58
+ if (result.code !== ExportResultCode.SUCCESS && result.error) {
59
+ const err = result.error;
60
+ if (err.code === GRPC_STATUS_UNIMPLEMENTED ||
61
+ ((_a = err.message) === null || _a === void 0 ? void 0 : _a.toString().includes('UNIMPLEMENTED'))) {
62
+ this.retryAt = Date.now() + RETRY_AFTER_MS;
63
+ resultCallback({ code: ExportResultCode.SUCCESS });
64
+ return;
65
+ }
54
66
  }
67
+ this.retryAt = 0;
68
+ resultCallback(result);
69
+ });
70
+ }
71
+ catch (e) {
72
+ if (e instanceof TypeError && ((_a = e.message) === null || _a === void 0 ? void 0 : _a.includes("reading 'name'"))) {
73
+ otelDiag === null || otelDiag === void 0 ? void 0 : otelDiag.error('hatchet instrumentation: OpenTelemetry package version mismatch. ' +
74
+ '@opentelemetry/exporter-trace-otlp-grpc and @opentelemetry/sdk-trace-base must be ' +
75
+ 'from the same release set (1.x + 0.5x.x, or 2.x + 0.20x.x). ' +
76
+ 'See https://github.com/open-telemetry/opentelemetry-js#version-compatibility');
77
+ resultCallback({ code: ExportResultCode.SUCCESS });
78
+ return;
55
79
  }
56
- this.retryAt = 0;
57
- resultCallback(result);
58
- });
80
+ throw e;
81
+ }
59
82
  }
60
83
  shutdown() {
61
84
  return this.inner.shutdown();
@@ -106,18 +129,17 @@ function createHatchetExporter(config) {
106
129
  return new OTLPTraceExporter(opts);
107
130
  }
108
131
  /**
109
- * Adds the Hatchet OTLP exporter to the given TracerProvider.
110
- * The exporter sends spans to the Hatchet engine's collector endpoint
111
- * using the same connection settings as the Hatchet client.
132
+ * Creates a SpanProcessor that sends spans to the Hatchet engine's
133
+ * collector endpoint using the same connection settings as the Hatchet client.
134
+ * Pass the returned processor to BasicTracerProvider's `spanProcessors` option.
112
135
  */
113
- function addHatchetExporter(tracerProvider, config, bspConfig) {
136
+ function createHatchetSpanProcessor(config, bspConfig) {
114
137
  const inner = createHatchetExporter(config);
115
138
  const exporter = new HatchetExporterWrapper(inner);
116
139
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
117
- const processor = new HatchetAttributeSpanProcessor(exporter, {
140
+ return new HatchetAttributeSpanProcessor(exporter, {
118
141
  scheduledDelayMillis: bspConfig === null || bspConfig === void 0 ? void 0 : bspConfig.scheduledDelayMillis,
119
142
  maxExportBatchSize: bspConfig === null || bspConfig === void 0 ? void 0 : bspConfig.maxExportBatchSize,
120
143
  maxQueueSize: bspConfig === null || bspConfig === void 0 ? void 0 : bspConfig.maxQueueSize,
121
144
  });
122
- tracerProvider.addSpanProcessor(processor);
123
145
  }
@@ -117,35 +117,32 @@ class HatchetInstrumentor extends InstrumentationBase {
117
117
  _setupHatchetCollector(clientConfig, bspConfig) {
118
118
  try {
119
119
  /* eslint-disable @typescript-eslint/no-require-imports */
120
- const { addHatchetExporter } = require('./hatchet-exporter.js');
120
+ const { createHatchetSpanProcessor } = require('./hatchet-exporter.js');
121
121
  let config = clientConfig;
122
122
  if (!config) {
123
- // Load config from environment (same as HatchetClient would)
124
123
  const { ConfigLoader } = require('../util/config-loader/config-loader');
125
124
  config = ConfigLoader.loadClientConfig();
126
125
  }
127
- // Get the SDK TracerProvider - either from the global provider or create one
128
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
129
- let sdkTracerProvider;
126
+ const processor = createHatchetSpanProcessor(config, bspConfig);
130
127
  try {
131
128
  const sdkTrace = require('@opentelemetry/sdk-trace-base');
132
129
  /* eslint-enable @typescript-eslint/no-require-imports */
133
- // Check if the global tracer provider is an SDK TracerProvider
134
130
  const globalProvider = otelApi.trace.getTracerProvider();
135
- if (globalProvider instanceof sdkTrace.BasicTracerProvider) {
136
- sdkTracerProvider = globalProvider;
131
+ if (!(globalProvider instanceof sdkTrace.BasicTracerProvider)) {
132
+ const sdkTracerProvider = new sdkTrace.BasicTracerProvider({
133
+ spanProcessors: [processor],
134
+ });
135
+ otelApi.trace.setGlobalTracerProvider(sdkTracerProvider);
137
136
  }
138
137
  else {
139
- // Create a new SDK TracerProvider and set it as global
140
- sdkTracerProvider = new sdkTrace.BasicTracerProvider();
141
- sdkTracerProvider.register();
138
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
139
+ globalProvider.addSpanProcessor(processor);
142
140
  }
143
141
  }
144
142
  catch (_a) {
145
143
  diag.warn('hatchet instrumentation: @opentelemetry/sdk-trace-base is required for enableHatchetCollector');
146
144
  return;
147
145
  }
148
- addHatchetExporter(sdkTracerProvider, config, bspConfig);
149
146
  diag.info('hatchet instrumentation: Hatchet OTLP collector enabled');
150
147
  }
151
148
  catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hatchet-dev/typescript-sdk",
3
- "version": "1.18.0",
3
+ "version": "1.19.0",
4
4
  "description": "Background task orchestration & visibility for developers",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -62,10 +62,10 @@
62
62
  },
63
63
  "optionalDependencies": {
64
64
  "@opentelemetry/api": "^1.9.0",
65
- "@opentelemetry/core": "^1.30.1",
65
+ "@opentelemetry/core": "^2.0.0",
66
66
  "@opentelemetry/exporter-trace-otlp-grpc": "^0.208.0",
67
67
  "@opentelemetry/instrumentation": "^0.208.0",
68
- "@opentelemetry/sdk-trace-base": "^1.30.1",
68
+ "@opentelemetry/sdk-trace-base": "^2.0.0",
69
69
  "prom-client": "^15.1.3"
70
70
  },
71
71
  "scripts": {
@@ -3,3 +3,64 @@ export type WebhookInput = {
3
3
  message: string;
4
4
  };
5
5
  export declare const webhookWorkflow: import("../..").WorkflowDeclaration<WebhookInput, {}, {}>;
6
+ type StripePaymentInput = {
7
+ type: string;
8
+ data: {
9
+ object: {
10
+ customer: string;
11
+ amount: number;
12
+ };
13
+ };
14
+ };
15
+ export declare const handleStripePayment: import("../..").TaskWorkflowDeclaration<StripePaymentInput, {
16
+ customer: string;
17
+ amount: number;
18
+ }, {}, {}, {}, {}>;
19
+ type GitHubPRInput = {
20
+ action: string;
21
+ pull_request: {
22
+ number: number;
23
+ title: string;
24
+ };
25
+ repository: {
26
+ full_name: string;
27
+ };
28
+ };
29
+ export declare const handleGitHubPR: import("../..").TaskWorkflowDeclaration<GitHubPRInput, {
30
+ repo: string;
31
+ pr: number;
32
+ }, {}, {}, {}, {}>;
33
+ type SlackEventInput = {
34
+ event: {
35
+ type: string;
36
+ user: string;
37
+ text: string;
38
+ channel: string;
39
+ };
40
+ };
41
+ export declare const handleSlackMention: import("../..").TaskWorkflowDeclaration<SlackEventInput, {
42
+ handled: true;
43
+ }, {}, {}, {}, {}>;
44
+ type SlackCommandInput = {
45
+ command: string;
46
+ text: string;
47
+ user_name: string;
48
+ response_url: string;
49
+ };
50
+ export declare const handleSlackCommand: import("../..").TaskWorkflowDeclaration<SlackCommandInput, {
51
+ command: string;
52
+ args: string;
53
+ }, {}, {}, {}, {}>;
54
+ type SlackInteractionInput = {
55
+ type: string;
56
+ actions: Array<{
57
+ action_id: string;
58
+ }>;
59
+ user: {
60
+ username: string;
61
+ };
62
+ };
63
+ export declare const handleSlackInteraction: import("../..").TaskWorkflowDeclaration<SlackInteractionInput, {
64
+ action: string;
65
+ }, {}, {}, {}, {}>;
66
+ export {};
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.webhookWorkflow = void 0;
12
+ exports.handleSlackInteraction = exports.handleSlackCommand = exports.handleSlackMention = exports.handleGitHubPR = exports.handleStripePayment = exports.webhookWorkflow = void 0;
13
13
  const hatchet_client_1 = require("../hatchet-client");
14
14
  exports.webhookWorkflow = hatchet_client_1.hatchet.workflow({
15
15
  name: 'webhook-workflow',
@@ -21,3 +21,60 @@ exports.webhookWorkflow.task({
21
21
  return input;
22
22
  }),
23
23
  });
24
+ exports.handleStripePayment = hatchet_client_1.hatchet.task({
25
+ name: 'handle-stripe-payment',
26
+ on: {
27
+ event: 'stripe:payment_intent.succeeded',
28
+ },
29
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
30
+ const { customer, amount } = input.data.object;
31
+ console.log(`Payment of ${amount} from ${customer}`);
32
+ return { customer, amount };
33
+ }),
34
+ });
35
+ exports.handleGitHubPR = hatchet_client_1.hatchet.task({
36
+ name: 'handle-github-pr',
37
+ on: {
38
+ event: 'github:pull_request:opened',
39
+ },
40
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
41
+ const repo = input.repository.full_name;
42
+ const prNumber = input.pull_request.number;
43
+ const { title } = input.pull_request;
44
+ console.log(`PR #${prNumber} opened on ${repo}: ${title}`);
45
+ return { repo, pr: prNumber };
46
+ }),
47
+ });
48
+ exports.handleSlackMention = hatchet_client_1.hatchet.task({
49
+ name: 'handle-slack-mention',
50
+ on: {
51
+ event: 'slack:event:app_mention',
52
+ },
53
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
54
+ const { user, text, channel } = input.event;
55
+ console.log(`Mentioned by ${user} in ${channel}: ${text}`);
56
+ return { handled: true };
57
+ }),
58
+ });
59
+ exports.handleSlackCommand = hatchet_client_1.hatchet.task({
60
+ name: 'handle-slack-command',
61
+ on: {
62
+ event: 'slack:command:/deploy',
63
+ },
64
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
65
+ console.log(`${input.user_name} ran ${input.command} ${input.text}`);
66
+ return { command: input.command, args: input.text };
67
+ }),
68
+ });
69
+ exports.handleSlackInteraction = hatchet_client_1.hatchet.task({
70
+ name: 'handle-slack-interaction',
71
+ on: {
72
+ event: 'slack:interaction:block_actions',
73
+ },
74
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
75
+ const [action] = input.actions;
76
+ console.log(`${input.user.username} clicked button: ${action.action_id}`);
77
+ return { action: action.action_id };
78
+ }),
79
+ });
80
+ // !!
package/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const HATCHET_VERSION = "1.18.0";
1
+ export declare const HATCHET_VERSION = "1.19.0";
package/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HATCHET_VERSION = void 0;
4
- exports.HATCHET_VERSION = '1.18.0';
4
+ exports.HATCHET_VERSION = '1.19.0';