@mastra/observability 1.10.3-alpha.0 → 1.11.0-alpha.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/CHANGELOG.md CHANGED
@@ -1,5 +1,126 @@
1
1
  # @mastra/observability
2
2
 
3
+ ## 1.11.0-alpha.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Auto-attach the Mastra-level `environment` to all observability signals. ([#15956](https://github.com/mastra-ai/mastra/pull/15956))
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [[`1723e09`](https://github.com/mastra-ai/mastra/commit/1723e099829892419ddbfe49287acfeac2522724), [`629f9e9`](https://github.com/mastra-ai/mastra/commit/629f9e9a7e56aa8f129515a3923c5813298790c7), [`25168fb`](https://github.com/mastra-ai/mastra/commit/25168fb9c1de9db7f8171df4f58ceb842c53aa29), [`ab34b5a`](https://github.com/mastra-ai/mastra/commit/ab34b5a2191b8e4353df1dbf7b9155e7d6628d79), [`5fb6c2a`](https://github.com/mastra-ai/mastra/commit/5fb6c2a95c1843cc231704b91354311fc1f34a71), [`394f0cf`](https://github.com/mastra-ai/mastra/commit/394f0cfc31e6b4d801219fdef2e9cc69e5bc8682), [`3d7f709`](https://github.com/mastra-ai/mastra/commit/3d7f709b615e588050bb6283c4ee5cfe2978cbde), [`48a42f1`](https://github.com/mastra-ai/mastra/commit/48a42f114a4006a95e0b7a1b5ad1a24815a175c2), [`2c83efc`](https://github.com/mastra-ai/mastra/commit/2c83efc4482b3efe50830e3b8b4ba9a8d219edff), [`282a10c`](https://github.com/mastra-ai/mastra/commit/282a10c9446e9922afe80e10e3770481c8ac8a28)]:
12
+ - @mastra/core@1.31.0-alpha.0
13
+
14
+ ## 1.10.3
15
+
16
+ ### Patch Changes
17
+
18
+ - Add durable agents with resumable streams ([#12557](https://github.com/mastra-ai/mastra/pull/12557))
19
+
20
+ Durable agents make agent execution resilient to disconnections, crashes, and long-running operations.
21
+
22
+ ### The Problem
23
+
24
+ Standard agent streaming has two fragility points:
25
+ 1. **Connection drops** - If a client disconnects mid-stream (network blip, browser refresh, mobile app backgrounded), all subsequent events are lost. The client has no way to "catch up" on what they missed.
26
+ 2. **Long-running operations** - Agent loops with tool calls can take minutes. Holding an HTTP connection open that long is unreliable. If the server restarts or the connection times out, the work is lost.
27
+
28
+ ### The Solution
29
+
30
+ **Resumable streams** solve connection drops. Every event is cached with a sequential index. If a client disconnects at event 5, they can reconnect and request events starting from index 6. They receive cached events immediately, then continue with live events as they arrive.
31
+
32
+ **Durable execution** solves long-running operations. Instead of executing the agent loop directly in the HTTP request, execution happens in a workflow engine (built-in evented engine or Inngest). The HTTP request just subscribes to events. If the connection drops, execution continues. The client can reconnect anytime to observe progress.
33
+
34
+ ### Usage
35
+
36
+ Wrap any existing `Agent` with durability using factory functions:
37
+
38
+ ```typescript
39
+ import { Agent } from '@mastra/core/agent';
40
+ import { createDurableAgent } from '@mastra/core/agent/durable';
41
+
42
+ const agent = new Agent({
43
+ id: 'my-agent',
44
+ model: openai('gpt-4'),
45
+ instructions: 'You are helpful',
46
+ });
47
+
48
+ const durableAgent = createDurableAgent({ agent });
49
+ ```
50
+
51
+ **Factory functions for different execution strategies:**
52
+
53
+ | Factory | Execution | Use Case |
54
+ | ---------------------------------------- | ----------------------------------- | ------------------------------- |
55
+ | `createDurableAgent({ agent })` | Local, synchronous | Development, simple deployments |
56
+ | `createEventedAgent({ agent })` | Fire-and-forget via workflow engine | Long-running operations |
57
+ | `createInngestAgent({ agent, inngest })` | Inngest-powered | Production, distributed systems |
58
+
59
+ ### Resumable Streams
60
+
61
+ ```typescript
62
+ // Start streaming
63
+ const { runId, output } = await durableAgent.stream('Analyze this data...');
64
+
65
+ // Client disconnects at event 5...
66
+
67
+ // Reconnect and resume from where we left off
68
+ const { output: resumed } = await durableAgent.observe(runId, { offset: 6 });
69
+ // Receives events 6, 7, 8... from cache, then continues with live events
70
+ ```
71
+
72
+ ### PubSub and Cache
73
+
74
+ Durable agents use two infrastructure components:
75
+
76
+ | Component | Purpose | Default |
77
+ | ---------- | ----------------------------------------- | --------------------- |
78
+ | **PubSub** | Real-time event delivery during streaming | `EventEmitterPubSub` |
79
+ | **Cache** | Stores events for replay on reconnection | `InMemoryServerCache` |
80
+
81
+ When `stream()` is called, events flow through pubsub in real-time. The cache stores each event with a sequential index. When `observe()` is called, missed events replay from cache before continuing with live events.
82
+
83
+ **Configure via Mastra instance (recommended):**
84
+
85
+ ```typescript
86
+ const mastra = new Mastra({
87
+ cache: new RedisServerCache({ url: 'redis://...' }),
88
+ pubsub: new RedisPubSub({ url: 'redis://...' }),
89
+ agents: {
90
+ // Inherits cache and pubsub from Mastra
91
+ myAgent: createDurableAgent({ agent }),
92
+ },
93
+ });
94
+ ```
95
+
96
+ **Configure per-agent (overrides Mastra):**
97
+
98
+ ```typescript
99
+ const durableAgent = createDurableAgent({
100
+ agent,
101
+ cache: new RedisServerCache({ url: 'redis://...' }),
102
+ pubsub: new RedisPubSub({ url: 'redis://...' }),
103
+ });
104
+ ```
105
+
106
+ **Disable caching (streams won't be resumable):**
107
+
108
+ ```typescript
109
+ const durableAgent = createDurableAgent({ agent, cache: false });
110
+ ```
111
+
112
+ For single-instance deployments, the defaults work fine. For multi-instance deployments (load balancer, horizontal scaling), use Redis-backed implementations so any instance can serve reconnection requests.
113
+
114
+ ### Class Hierarchy
115
+ - `DurableAgent` extends `Agent` - base class with resumable streams
116
+ - `EventedAgent` extends `DurableAgent` - fire-and-forget execution
117
+ - `InngestAgent` extends `DurableAgent` - Inngest-powered execution
118
+
119
+ - Reduced default cloud observability volume by filtering model chunk spans from CloudExporter uploads by default and raising the default observability log level to `warn`. ([#15815](https://github.com/mastra-ai/mastra/pull/15815))
120
+
121
+ - Updated dependencies [[`920c757`](https://github.com/mastra-ai/mastra/commit/920c75799c6bd71787d86deaf654a35af4c839ca), [`d587199`](https://github.com/mastra-ai/mastra/commit/d5871993c0371bde2b0717d6b47194755baa1443), [`1fe2533`](https://github.com/mastra-ai/mastra/commit/1fe2533c4382ca6858aac7c4b63e888c2eac6541), [`f8694b6`](https://github.com/mastra-ai/mastra/commit/f8694b6fa0b7a5cde71d794c3bbef4957c55bcb8)]:
122
+ - @mastra/core@1.30.0
123
+
3
124
  ## 1.10.3-alpha.0
4
125
 
5
126
  ### Patch Changes
@@ -1 +1 @@
1
- {"version":3,"file":"default.d.ts","sourceRoot":"","sources":["../src/default.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAG/C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EACV,kBAAkB,EAClB,cAAc,EACd,qBAAqB,EACrB,aAAa,EAEb,uBAAuB,EACvB,qBAAqB,EACrB,aAAa,EACb,UAAU,EAEX,MAAM,4BAA4B,CAAC;AAIpC,OAAO,KAAK,EAA+B,2BAA2B,EAAE,MAAM,UAAU,CAAC;AAsBzF;;;GAGG;AACH,qBAAa,aAAc,SAAQ,UAAW,YAAW,uBAAuB;;gBAIlE,MAAM,EAAE,2BAA2B;IAkG/C,yFAAyF;IACzF,gBAAgB,CAAC,OAAO,EAAE;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI;IAwBnD,sFAAsF;IACtF,SAAS,CAAC,OAAO,EAAE;QAAE,MAAM,EAAE,aAAa,CAAA;KAAE,GAAG,IAAI;IAOnD,0FAA0F;IAC1F,mBAAmB,CAAC,OAAO,EAAE,qBAAqB,GAAG,qBAAqB,GAAG,SAAS;IAIhF,gBAAgB,CAAC,IAAI,EAAE;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IA6B1E,QAAQ,CAAC,IAAI,EAAE;QACnB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,MAAM,CAAC,EAAE,MAAM,CAA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qBAAqB,GAAG,SAAS;IAI5D,8CAA8C;IAC9C,kBAAkB,IAAI,qBAAqB,GAAG,SAAS;IAIvD,mDAAmD;IACnD,aAAa,IAAI,WAAW,CAAC,MAAM,EAAE,qBAAqB,CAAC;IAI3D,gFAAgF;IAChF,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAIzC,mEAAmE;IACnE,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAIlC,qEAAqE;IACrE,iBAAiB,CAAC,QAAQ,EAAE,cAAc,GAAG,IAAI;IAIjD,8DAA8D;IAC9D,KAAK,IAAI,IAAI;IAIb,qEAAqE;IAC/D,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAqDhC"}
package/dist/index.cjs CHANGED
@@ -19443,7 +19443,7 @@ var BaseSpan = class {
19443
19443
  sessionId: getMetadataString("sessionId"),
19444
19444
  threadId: getMetadataString("threadId"),
19445
19445
  requestId: getMetadataString("requestId"),
19446
- environment: getMetadataString("environment"),
19446
+ environment: getMetadataString("environment") ?? this.observabilityInstance.getMastraEnvironment?.(),
19447
19447
  source: getMetadataString("source"),
19448
19448
  serviceName: getMetadataString("serviceName") ?? this.observabilityInstance.getConfig().serviceName,
19449
19449
  experimentId: getMetadataString("experimentId")
@@ -19715,6 +19715,12 @@ var BaseObservabilityInstance = class extends base.MastraBase {
19715
19715
  * Cardinality filter for metrics label protection.
19716
19716
  */
19717
19717
  cardinalityFilter;
19718
+ /**
19719
+ * Deployment environment propagated from the parent Mastra instance.
19720
+ * Set by `Observability.setMastraContext`, read by spans as a fallback when
19721
+ * a span's `metadata.environment` isn't set.
19722
+ */
19723
+ #mastraEnvironment;
19718
19724
  constructor(config2) {
19719
19725
  super({ component: logger.RegisteredLogger.OBSERVABILITY, name: config2.serviceName });
19720
19726
  this.config = {
@@ -19807,6 +19813,7 @@ var BaseObservabilityInstance = class extends base.MastraBase {
19807
19813
  const tracingMetadata = !options.parent ? tracingOptions?.metadata : void 0;
19808
19814
  const mergedMetadata = metadata || tracingMetadata ? { ...metadata, ...tracingMetadata } : void 0;
19809
19815
  const enrichedMetadata = this.extractMetadataFromRequestContext(requestContext, mergedMetadata, traceState);
19816
+ const finalMetadata = !options.parent && this.#mastraEnvironment !== void 0 && (enrichedMetadata === void 0 || enrichedMetadata.environment === void 0) ? { ...enrichedMetadata ?? {}, environment: this.#mastraEnvironment } : enrichedMetadata;
19810
19817
  const tags = !options.parent ? tracingOptions?.tags : void 0;
19811
19818
  const traceId = !options.parent ? options.traceId ?? tracingOptions?.traceId : options.traceId;
19812
19819
  const parentSpanId = !options.parent ? options.parentSpanId ?? tracingOptions?.parentSpanId : options.parentSpanId;
@@ -19814,7 +19821,7 @@ var BaseObservabilityInstance = class extends base.MastraBase {
19814
19821
  ...rest,
19815
19822
  traceId,
19816
19823
  parentSpanId,
19817
- metadata: enrichedMetadata,
19824
+ metadata: finalMetadata,
19818
19825
  traceState,
19819
19826
  tags,
19820
19827
  requestContext
@@ -19867,6 +19874,20 @@ var BaseObservabilityInstance = class extends base.MastraBase {
19867
19874
  getConfig() {
19868
19875
  return { ...this.config };
19869
19876
  }
19877
+ /**
19878
+ * Returns the deployment environment propagated from the parent Mastra instance.
19879
+ * Spans use this as a fallback when `metadata.environment` isn't set.
19880
+ */
19881
+ getMastraEnvironment() {
19882
+ return this.#mastraEnvironment;
19883
+ }
19884
+ /**
19885
+ * Internal hook used by `Observability.setMastraContext` to push the
19886
+ * resolved Mastra-level environment into this instance.
19887
+ */
19888
+ __setMastraEnvironment(environment) {
19889
+ this.#mastraEnvironment = environment;
19890
+ }
19870
19891
  // ============================================================================
19871
19892
  // Plugin Access
19872
19893
  // ============================================================================
@@ -20881,7 +20902,9 @@ var Observability = class extends base.MastraBase {
20881
20902
  const instances = this.listInstances();
20882
20903
  const { mastra } = options;
20883
20904
  this.#mastra = mastra;
20905
+ const mastraEnvironment = mastra.getEnvironment?.();
20884
20906
  instances.forEach((instance) => {
20907
+ instance.__setMastraEnvironment?.(mastraEnvironment);
20885
20908
  const config2 = instance.getConfig();
20886
20909
  const exporters = instance.getExporters();
20887
20910
  exporters.forEach((exporter) => {
@@ -20998,6 +21021,9 @@ var Observability = class extends base.MastraBase {
20998
21021
  /** Register a named observability instance, optionally marking it as default. */
20999
21022
  registerInstance(name, instance, isDefault = false) {
21000
21023
  this.#registry.register(name, instance, isDefault);
21024
+ if (this.#mastra) {
21025
+ instance.__setMastraEnvironment?.(this.#mastra.getEnvironment?.());
21026
+ }
21001
21027
  }
21002
21028
  /** Get a registered instance by name. */
21003
21029
  getInstance(name) {