@mastra/posthog 1.3.7-alpha.0 → 1.3.7-alpha.2
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 +13 -166
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @mastra/posthog
|
|
2
2
|
|
|
3
|
-
PostHog AI
|
|
3
|
+
Export Mastra traces to PostHog AI observability as structured events, with project credentials, host configuration, and serverless flushing.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -10,186 +10,33 @@ npm install @mastra/posthog
|
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
The exporter automatically reads credentials from environment variables:
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
# Required
|
|
19
|
-
POSTHOG_API_KEY=phc_...
|
|
20
|
-
|
|
21
|
-
# Optional
|
|
22
|
-
POSTHOG_HOST=https://us.i.posthog.com # or eu.i.posthog.com
|
|
23
|
-
```
|
|
13
|
+
Set `POSTHOG_API_KEY` before creating the exporter.
|
|
24
14
|
|
|
25
15
|
```typescript
|
|
16
|
+
import { Mastra } from '@mastra/core/mastra';
|
|
17
|
+
import { Observability } from '@mastra/observability';
|
|
26
18
|
import { PosthogExporter } from '@mastra/posthog';
|
|
27
19
|
|
|
28
|
-
const mastra = new Mastra({
|
|
29
|
-
|
|
30
|
-
observability: {
|
|
20
|
+
export const mastra = new Mastra({
|
|
21
|
+
observability: new Observability({
|
|
31
22
|
configs: {
|
|
32
23
|
posthog: {
|
|
33
24
|
serviceName: 'my-service',
|
|
34
25
|
exporters: [new PosthogExporter()],
|
|
35
26
|
},
|
|
36
27
|
},
|
|
37
|
-
},
|
|
28
|
+
}),
|
|
38
29
|
});
|
|
39
30
|
```
|
|
40
31
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
You can also pass credentials directly:
|
|
44
|
-
|
|
45
|
-
```typescript
|
|
46
|
-
import { PosthogExporter } from '@mastra/posthog';
|
|
47
|
-
|
|
48
|
-
const mastra = new Mastra({
|
|
49
|
-
...,
|
|
50
|
-
observability: {
|
|
51
|
-
configs: {
|
|
52
|
-
posthog: {
|
|
53
|
-
serviceName: 'my-service',
|
|
54
|
-
exporters: [
|
|
55
|
-
new PosthogExporter({
|
|
56
|
-
apiKey: 'phc_...',
|
|
57
|
-
host: 'https://us.i.posthog.com', // optional, defaults to US region
|
|
58
|
-
}),
|
|
59
|
-
],
|
|
60
|
-
},
|
|
61
|
-
},
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
## Features
|
|
67
|
-
|
|
68
|
-
### AI Tracing
|
|
69
|
-
|
|
70
|
-
- **Event-based architecture**: Captures LLM calls and operations as PostHog events
|
|
71
|
-
- **LLM analytics**: Automatic tracking of token usage, latency, and costs
|
|
72
|
-
- **Streaming support**: Handles streaming LLM responses with MODEL_CHUNK events
|
|
73
|
-
- **Privacy mode**: Optional exclusion of input/output data for sensitive applications
|
|
74
|
-
- **Serverless optimized**: Auto-configures batching for serverless environments
|
|
75
|
-
|
|
76
|
-
### Supported Event Types
|
|
77
|
-
|
|
78
|
-
- `$ai_generation`: LLM model calls (MODEL_GENERATION, MODEL_STEP)
|
|
79
|
-
- `$ai_span`: Operations like tool calls, workflows, and streaming chunks
|
|
80
|
-
- Hierarchical traces with parent-child relationships via `$ai_parent_id`
|
|
81
|
-
- Session grouping with `$ai_session_id`
|
|
82
|
-
|
|
83
|
-
## Configuration
|
|
84
|
-
|
|
85
|
-
### Basic Configuration
|
|
86
|
-
|
|
87
|
-
```typescript
|
|
88
|
-
new PosthogExporter({
|
|
89
|
-
apiKey: process.env.POSTHOG_API_KEY,
|
|
90
|
-
});
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
### Advanced Configuration
|
|
94
|
-
|
|
95
|
-
```typescript
|
|
96
|
-
new PosthogExporter({
|
|
97
|
-
// Required
|
|
98
|
-
apiKey: process.env.POSTHOG_API_KEY,
|
|
99
|
-
|
|
100
|
-
// Optional: Region/Host
|
|
101
|
-
host: 'https://eu.i.posthog.com', // EU region
|
|
102
|
-
// or
|
|
103
|
-
host: 'https://your-instance.com', // Self-hosted
|
|
104
|
-
|
|
105
|
-
// Optional: Batching (defaults: flushAt=20, flushInterval=10000)
|
|
106
|
-
flushAt: 20, // Batch size before auto-flush
|
|
107
|
-
flushInterval: 10000, // Flush interval in milliseconds
|
|
32
|
+
## Documentation
|
|
108
33
|
|
|
109
|
-
|
|
110
|
-
serverless: true, // Sets flushAt=10, flushInterval=2000
|
|
34
|
+
- [PostHog](https://mastra.ai/integrations/observability/posthog)
|
|
111
35
|
|
|
112
|
-
|
|
113
|
-
defaultDistinctId: 'anonymous', // Fallback if no userId in metadata
|
|
36
|
+
## Changelog
|
|
114
37
|
|
|
115
|
-
|
|
116
|
-
enablePrivacyMode: false, // Set to true to exclude input/output from LLM events
|
|
117
|
-
});
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
### Serverless Environments
|
|
121
|
-
|
|
122
|
-
When deploying to serverless environments (Lambda, Vercel Functions, etc.), enable serverless mode:
|
|
123
|
-
|
|
124
|
-
```typescript
|
|
125
|
-
new PosthogExporter({
|
|
126
|
-
apiKey: process.env.POSTHOG_API_KEY,
|
|
127
|
-
serverless: true, // Auto-configures for serverless
|
|
128
|
-
});
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
**Important**: Always call `await mastra.shutdown()` before your serverless function exits to flush remaining events.
|
|
132
|
-
|
|
133
|
-
### Privacy Mode
|
|
134
|
-
|
|
135
|
-
To exclude sensitive input/output data while still tracking token usage and latency:
|
|
136
|
-
|
|
137
|
-
```typescript
|
|
138
|
-
new PosthogExporter({
|
|
139
|
-
apiKey: process.env.POSTHOG_API_KEY,
|
|
140
|
-
enablePrivacyMode: true, // Excludes $ai_input and $ai_output_choices
|
|
141
|
-
});
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
Note: Privacy mode only applies to `$ai_generation` events. Span events (tool calls, etc.) still include input/output state.
|
|
145
|
-
|
|
146
|
-
## Metadata
|
|
147
|
-
|
|
148
|
-
Include metadata in your Mastra spans to enrich PostHog events:
|
|
149
|
-
|
|
150
|
-
```typescript
|
|
151
|
-
// User identification
|
|
152
|
-
{
|
|
153
|
-
metadata: {
|
|
154
|
-
userId: 'user-123', // → distinctId in PostHog
|
|
155
|
-
sessionId: 'session-abc', // → $ai_session_id for grouping
|
|
156
|
-
|
|
157
|
-
// Custom properties (passed through to PostHog)
|
|
158
|
-
environment: 'production',
|
|
159
|
-
version: '1.0.0',
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
## Cost Tracking
|
|
165
|
-
|
|
166
|
-
PostHog automatically calculates costs from:
|
|
167
|
-
|
|
168
|
-
- Model name + token counts (uses OpenRouter pricing data)
|
|
169
|
-
- Or you can send pre-calculated costs in span attributes
|
|
170
|
-
|
|
171
|
-
## Viewing Data in PostHog
|
|
172
|
-
|
|
173
|
-
1. Navigate to **Product Analytics** → **Events**
|
|
174
|
-
2. Filter for events starting with `$ai_`
|
|
175
|
-
3. Use the **AI Observability** dashboard (if available in your PostHog plan)
|
|
176
|
-
4. Query events by:
|
|
177
|
-
- `$ai_trace_id`: Group all events in a trace
|
|
178
|
-
- `$ai_session_id`: Group traces in a session
|
|
179
|
-
- `$ai_model`: Filter by model (e.g., "gpt-4o")
|
|
180
|
-
- `$ai_provider`: Filter by provider (e.g., "openai")
|
|
181
|
-
|
|
182
|
-
## Environment Variables
|
|
183
|
-
|
|
184
|
-
```bash
|
|
185
|
-
# Required
|
|
186
|
-
POSTHOG_API_KEY=phc_...
|
|
187
|
-
|
|
188
|
-
# Optional
|
|
189
|
-
POSTHOG_HOST=https://us.i.posthog.com # or eu.i.posthog.com
|
|
190
|
-
```
|
|
38
|
+
See the [package changelog](https://github.com/mastra-ai/mastra/blob/main/observability/posthog/CHANGELOG.md) for version history and release notes.
|
|
191
39
|
|
|
192
|
-
##
|
|
40
|
+
## Support
|
|
193
41
|
|
|
194
|
-
|
|
195
|
-
- [Mastra Observability Documentation](https://mastra.ai/docs/observability)
|
|
42
|
+
We have an [open community Discord](https://discord.gg/mastra-ai). Come and say hello and let us know if you have any questions or need any help getting things running.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/posthog",
|
|
3
|
-
"version": "1.3.7-alpha.
|
|
3
|
+
"version": "1.3.7-alpha.2",
|
|
4
4
|
"description": "PostHog observability provider for Mastra",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"license": "Apache-2.0",
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"posthog-node": "^5.46.1",
|
|
27
|
-
"@mastra/observability": "1.17.5-alpha.
|
|
27
|
+
"@mastra/observability": "1.17.5-alpha.2"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/node": "22.20.1",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"vitest": "4.1.10",
|
|
35
35
|
"@internal/lint": "0.0.129",
|
|
36
36
|
"@internal/types-builder": "0.0.104",
|
|
37
|
-
"@
|
|
38
|
-
"@
|
|
37
|
+
"@mastra/core": "1.64.0-alpha.8",
|
|
38
|
+
"@observability/test-utils": "0.1.0"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"@mastra/core": ">=1.16.0-0 <2.0.0-0"
|