securenow 4.0.1 → 4.0.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/AUTO-SETUP.md CHANGED
@@ -412,3 +412,5 @@ npx securenow init
412
412
  **That's it!** 🎉
413
413
 
414
414
 
415
+
416
+
@@ -0,0 +1,356 @@
1
+ # 📊 Automatic IP and Request Metadata Capture
2
+
3
+ SecureNow automatically captures user IP addresses and detailed request metadata in your Next.js traces!
4
+
5
+ ---
6
+
7
+ ## ✅ What Gets Captured Automatically
8
+
9
+ ### 🌐 Client Information
10
+ - **IP Address** (`http.client_ip`)
11
+ - From `x-forwarded-for` (Vercel, most proxies)
12
+ - From `x-real-ip`
13
+ - From `cf-connecting-ip` (Cloudflare)
14
+ - From `x-client-ip`
15
+ - From socket `remoteAddress`
16
+
17
+ ### 📱 Request Details
18
+ - **User Agent** (`http.user_agent`) - Browser/device info
19
+ - **Referer** (`http.referer`) - Where the user came from
20
+ - **Host** (`http.host`) - Your domain
21
+ - **Scheme** (`http.scheme`) - http or https
22
+ - **Request ID** (`http.request_id`) - Correlation ID if present
23
+
24
+ ### 🌍 Geographic Data (when available)
25
+ - **Country** (`http.geo.country`)
26
+ - From Vercel: `x-vercel-ip-country`
27
+ - From Cloudflare: `cf-ipcountry`
28
+ - **Region** (`http.geo.region`) - From `x-vercel-ip-country-region`
29
+ - **City** (`http.geo.city`) - From `x-vercel-ip-city`
30
+
31
+ ### 📊 Response Data
32
+ - **Status Code** (`http.status_code`) - 200, 404, 500, etc.
33
+
34
+ ---
35
+
36
+ ## 🚀 Usage
37
+
38
+ **No configuration needed!** Just use SecureNow:
39
+
40
+ ```typescript
41
+ // instrumentation.ts
42
+ import { registerSecureNow } from 'securenow/nextjs';
43
+
44
+ export function register() {
45
+ registerSecureNow();
46
+ }
47
+ ```
48
+
49
+ That's it! All request metadata is automatically captured.
50
+
51
+ ---
52
+
53
+ ## 📈 View in SigNoz
54
+
55
+ In your SigNoz dashboard, you'll see these attributes on every span:
56
+
57
+ ```json
58
+ {
59
+ "http.client_ip": "203.0.113.45",
60
+ "http.user_agent": "Mozilla/5.0...",
61
+ "http.referer": "https://google.com",
62
+ "http.host": "your-app.vercel.app",
63
+ "http.scheme": "https",
64
+ "http.status_code": 200,
65
+ "http.geo.country": "US",
66
+ "http.geo.region": "CA",
67
+ "http.geo.city": "San Francisco"
68
+ }
69
+ ```
70
+
71
+ ---
72
+
73
+ ## 🔍 Use Cases
74
+
75
+ ### 1. Debug Location-Specific Issues
76
+ Filter traces by country/region to debug geographic problems:
77
+ ```
78
+ http.geo.country = "JP" AND http.status_code >= 500
79
+ ```
80
+
81
+ ### 2. Track User Journey
82
+ Follow a specific user through your app using IP:
83
+ ```
84
+ http.client_ip = "203.0.113.45"
85
+ ```
86
+
87
+ ### 3. Monitor Bot Traffic
88
+ Identify and filter bot requests:
89
+ ```
90
+ http.user_agent CONTAINS "bot" OR http.user_agent CONTAINS "crawler"
91
+ ```
92
+
93
+ ### 4. Analyze Referer Sources
94
+ See where your traffic comes from:
95
+ ```
96
+ GROUP BY http.referer
97
+ ```
98
+
99
+ ### 5. Performance by Region
100
+ Compare response times across regions:
101
+ ```
102
+ AVG(duration) GROUP BY http.geo.country
103
+ ```
104
+
105
+ ---
106
+
107
+ ## 🛠️ Customization
108
+
109
+ ### Option 1: Add More Attributes
110
+
111
+ You can add custom attributes in your Next.js code:
112
+
113
+ ```typescript
114
+ // In your API route or server component
115
+ import { trace } from '@opentelemetry/api';
116
+
117
+ export async function GET(request: Request) {
118
+ const span = trace.getActiveSpan();
119
+
120
+ if (span) {
121
+ // Add custom attributes
122
+ span.setAttributes({
123
+ 'user.id': getUserId(request),
124
+ 'user.subscription': 'premium',
125
+ 'request.path': new URL(request.url).pathname,
126
+ });
127
+ }
128
+
129
+ // Your code...
130
+ }
131
+ ```
132
+
133
+ ### Option 2: Disable Auto-Capture
134
+
135
+ If you don't want automatic IP capture, you can use a simpler configuration:
136
+
137
+ ```typescript
138
+ // instrumentation.ts
139
+ import { registerSecureNow } from 'securenow/nextjs';
140
+
141
+ export function register() {
142
+ // This will use the simple @vercel/otel default
143
+ // (no automatic IP capture)
144
+ process.env.SECURENOW_SIMPLE_MODE = '1';
145
+ registerSecureNow();
146
+ }
147
+ ```
148
+
149
+ ---
150
+
151
+ ## 🔒 Privacy Considerations
152
+
153
+ ### IP Address Handling
154
+
155
+ **By default, SecureNow captures IP addresses.** Consider these privacy aspects:
156
+
157
+ 1. **GDPR Compliance**
158
+ - IP addresses are considered personal data under GDPR
159
+ - Ensure you have legal basis for processing
160
+ - Consider anonymizing IPs in some regions
161
+
162
+ 2. **Data Retention**
163
+ - Configure SigNoz retention policies
164
+ - Consider shorter retention for IP data
165
+
166
+ 3. **Anonymization Option**
167
+
168
+ ```typescript
169
+ // Custom middleware to anonymize IPs
170
+ import { trace } from '@opentelemetry/api';
171
+
172
+ export function middleware(request: NextRequest) {
173
+ const span = trace.getActiveSpan();
174
+
175
+ if (span) {
176
+ const ip = request.ip || 'unknown';
177
+ // Anonymize last octet: 203.0.113.45 → 203.0.113.0
178
+ const anonymized = ip.replace(/\.\d+$/, '.0');
179
+ span.setAttribute('http.client_ip', anonymized);
180
+ }
181
+
182
+ return NextResponse.next();
183
+ }
184
+ ```
185
+
186
+ ---
187
+
188
+ ## 🎯 Examples
189
+
190
+ ### Example 1: Geographic Load Balancing Debugging
191
+
192
+ **Problem:** Users in Asia report slow performance
193
+
194
+ **Solution:** Query traces by region
195
+ ```
196
+ http.geo.country IN ["JP", "CN", "KR"]
197
+ AND duration > 1000ms
198
+ ```
199
+
200
+ ### Example 2: Bot Detection
201
+
202
+ **Problem:** Suspicious traffic patterns
203
+
204
+ **Solution:** Filter by user agent
205
+ ```
206
+ http.user_agent CONTAINS "bot"
207
+ OR http.user_agent CONTAINS "crawler"
208
+ OR http.user_agent CONTAINS "spider"
209
+ ```
210
+
211
+ ### Example 3: Referer Analysis
212
+
213
+ **Problem:** Want to track marketing campaigns
214
+
215
+ **Solution:** Group by referer
216
+ ```
217
+ http.referer CONTAINS "utm_source"
218
+ GROUP BY http.referer
219
+ ```
220
+
221
+ ### Example 4: Rate Limiting Analysis
222
+
223
+ **Problem:** Need to identify IPs hitting rate limits
224
+
225
+ **Solution:** Track by IP and status
226
+ ```
227
+ http.status_code = 429
228
+ GROUP BY http.client_ip
229
+ ORDER BY COUNT DESC
230
+ ```
231
+
232
+ ---
233
+
234
+ ## 📊 Dashboard Queries
235
+
236
+ ### Top Countries by Traffic
237
+ ```sql
238
+ SELECT
239
+ http.geo.country,
240
+ COUNT(*) as requests,
241
+ AVG(duration) as avg_duration
242
+ FROM spans
243
+ WHERE http.geo.country IS NOT NULL
244
+ GROUP BY http.geo.country
245
+ ORDER BY requests DESC
246
+ LIMIT 10
247
+ ```
248
+
249
+ ### Slowest Requests by Region
250
+ ```sql
251
+ SELECT
252
+ http.geo.country,
253
+ http.target,
254
+ MAX(duration) as max_duration
255
+ FROM spans
256
+ WHERE http.geo.country IS NOT NULL
257
+ GROUP BY http.geo.country, http.target
258
+ ORDER BY max_duration DESC
259
+ LIMIT 20
260
+ ```
261
+
262
+ ### Error Rate by User Agent
263
+ ```sql
264
+ SELECT
265
+ http.user_agent,
266
+ COUNT(*) as total,
267
+ SUM(CASE WHEN http.status_code >= 400 THEN 1 ELSE 0 END) as errors,
268
+ (errors / total * 100) as error_rate
269
+ FROM spans
270
+ GROUP BY http.user_agent
271
+ ORDER BY error_rate DESC
272
+ LIMIT 10
273
+ ```
274
+
275
+ ---
276
+
277
+ ## 🔧 Technical Details
278
+
279
+ ### How It Works
280
+
281
+ 1. **HttpInstrumentation** intercepts incoming HTTP requests
282
+ 2. **requestHook** extracts headers and metadata
283
+ 3. **Attributes** are added to the active span
284
+ 4. **Data flows** to SigNoz with the trace
285
+
286
+ ### Headers Priority
287
+
288
+ IP address is extracted in this order:
289
+ 1. `x-forwarded-for` (first IP in list)
290
+ 2. `x-real-ip`
291
+ 3. `cf-connecting-ip` (Cloudflare)
292
+ 4. `x-client-ip`
293
+ 5. `socket.remoteAddress`
294
+
295
+ ### Performance Impact
296
+
297
+ - **Minimal overhead:** < 1ms per request
298
+ - **No blocking:** Runs async with request processing
299
+ - **Fail-safe:** Errors don't break requests
300
+
301
+ ---
302
+
303
+ ## ❓ FAQ
304
+
305
+ ### Q: Is this GDPR compliant?
306
+
307
+ **A:** IP addresses are personal data. Ensure you:
308
+ - Have legal basis (legitimate interest, consent, etc.)
309
+ - Document in privacy policy
310
+ - Configure appropriate retention
311
+ - Consider anonymization for EU users
312
+
313
+ ### Q: Can I disable IP capture?
314
+
315
+ **A:** Yes, use simple mode:
316
+ ```typescript
317
+ process.env.SECURENOW_SIMPLE_MODE = '1';
318
+ registerSecureNow();
319
+ ```
320
+
321
+ ### Q: Does this work on Edge Runtime?
322
+
323
+ **A:** Currently only Node.js runtime is supported. Edge runtime support coming soon.
324
+
325
+ ### Q: What about bot traffic?
326
+
327
+ **A:** Bot traffic is captured automatically. Filter using:
328
+ ```
329
+ http.user_agent NOT CONTAINS "bot"
330
+ ```
331
+
332
+ ### Q: Can I capture custom headers?
333
+
334
+ **A:** Yes! Use OpenTelemetry API:
335
+ ```typescript
336
+ import { trace } from '@opentelemetry/api';
337
+
338
+ const span = trace.getActiveSpan();
339
+ span.setAttribute('custom.header', request.headers.get('x-custom'));
340
+ ```
341
+
342
+ ---
343
+
344
+ ## 🎉 Summary
345
+
346
+ SecureNow automatically captures:
347
+ - ✅ IP addresses (multiple sources)
348
+ - ✅ User agents
349
+ - ✅ Referers
350
+ - ✅ Geographic data (Vercel/Cloudflare)
351
+ - ✅ Request/response metadata
352
+
353
+ **Zero configuration required** - it just works!
354
+
355
+ View everything in SigNoz for powerful analytics and debugging.
356
+
package/CUSTOMER-GUIDE.md CHANGED
@@ -110,6 +110,21 @@ Open your **SigNoz dashboard** and you'll see traces immediately!
110
110
 
111
111
  ## 📊 What You Get Automatically
112
112
 
113
+ ### 🌐 User & Request Data (NEW!)
114
+ - **IP Addresses** - Automatically captured from all sources
115
+ - **User Agents** - Browser/device information
116
+ - **Referers** - Traffic sources
117
+ - **Geographic Data** - Country, region, city (on Vercel/Cloudflare)
118
+ - **Request Headers** - Host, scheme, request IDs
119
+ - **Response Codes** - 200, 404, 500, etc.
120
+
121
+ **Perfect for:**
122
+ - Debugging location-specific issues
123
+ - Tracking user journeys
124
+ - Bot detection
125
+ - Performance analysis by region
126
+ - Marketing attribution
127
+
113
128
  ### ✅ Next.js Built-in Spans
114
129
  - HTTP requests
115
130
  - API routes
package/NEXTJS-GUIDE.md CHANGED
@@ -99,9 +99,19 @@ You should see:
99
99
 
100
100
  ---
101
101
 
102
- ## 📊 What Gets Automatically Instrumented?
102
+ ## 📊 What Gets Automatically Captured?
103
103
 
104
- SecureNow uses OpenTelemetry's auto-instrumentation to capture:
104
+ SecureNow automatically captures comprehensive request data:
105
+
106
+ ### 🌐 User Information (Automatic!)
107
+ - **IP Address** - From x-forwarded-for, x-real-ip, etc.
108
+ - **User Agent** - Browser and device info
109
+ - **Referer** - Where users came from
110
+ - **Geographic Data** - Country, region, city (Vercel/Cloudflare)
111
+ - **Request Metadata** - Headers, host, scheme
112
+ - **Response Data** - Status codes, timing
113
+
114
+ See [AUTOMATIC-IP-CAPTURE.md](./AUTOMATIC-IP-CAPTURE.md) for full details.
105
115
 
106
116
  ### Next.js Built-in Spans
107
117
  - ✅ HTTP requests (`[http.method] [next.route]`)
package/cli.js CHANGED
@@ -260,3 +260,5 @@ if (require.main === module) {
260
260
  module.exports = { main };
261
261
 
262
262
 
263
+
264
+
@@ -258,3 +258,5 @@ Then in SigNoz:
258
258
  **Happy tracing! 🎉**
259
259
 
260
260
 
261
+
262
+
@@ -30,3 +30,5 @@ const nextConfig = {
30
30
  module.exports = nextConfig;
31
31
 
32
32
 
33
+
34
+
@@ -27,3 +27,5 @@ OTEL_EXPORTER_OTLP_HEADERS="x-api-key=your-api-key-here"
27
27
  # NODE_ENV=production
28
28
 
29
29
 
30
+
31
+
@@ -29,3 +29,5 @@ export function register() {
29
29
  */
30
30
 
31
31
 
32
+
33
+
@@ -29,3 +29,5 @@ export function register() {
29
29
  */
30
30
 
31
31
 
32
+
33
+
@@ -29,3 +29,5 @@ export function register() {
29
29
  */
30
30
 
31
31
 
32
+
33
+
@@ -63,3 +63,5 @@ setTimeout(() => {
63
63
  }, 2000);
64
64
 
65
65
 
66
+
67
+
package/nextjs.js CHANGED
@@ -86,8 +86,10 @@ function registerSecureNow(options = {}) {
86
86
 
87
87
  console.log('[securenow] 🚀 Next.js App → service.name=%s', serviceName);
88
88
 
89
- // -------- Use @vercel/otel --------
89
+ // -------- Use @vercel/otel with enhanced configuration --------
90
90
  const { registerOTel } = require('@vercel/otel');
91
+ const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
92
+
91
93
  registerOTel({
92
94
  serviceName: serviceName,
93
95
  attributes: {
@@ -95,10 +97,89 @@ function registerSecureNow(options = {}) {
95
97
  'service.version': process.env.npm_package_version || process.env.VERCEL_GIT_COMMIT_SHA || undefined,
96
98
  'vercel.region': process.env.VERCEL_REGION || undefined,
97
99
  },
100
+ instrumentations: [
101
+ // Add HTTP instrumentation with request hooks to capture IP and headers
102
+ new HttpInstrumentation({
103
+ requireParentforOutgoingSpans: false,
104
+ requireParentforIncomingSpans: false,
105
+ requestHook: (span, request) => {
106
+ try {
107
+ // Capture client IP from various headers
108
+ const headers = request.headers || {};
109
+
110
+ // Try different header sources for IP
111
+ const clientIp =
112
+ headers['x-forwarded-for']?.split(',')[0]?.trim() ||
113
+ headers['x-real-ip'] ||
114
+ headers['cf-connecting-ip'] || // Cloudflare
115
+ headers['x-client-ip'] ||
116
+ request.socket?.remoteAddress ||
117
+ 'unknown';
118
+
119
+ // Add IP and request metadata to span
120
+ span.setAttributes({
121
+ 'http.client_ip': clientIp,
122
+ 'http.user_agent': headers['user-agent'] || 'unknown',
123
+ 'http.referer': headers['referer'] || headers['referrer'] || '',
124
+ 'http.host': headers['host'] || '',
125
+ 'http.scheme': request.socket?.encrypted ? 'https' : 'http',
126
+ 'http.forwarded_for': headers['x-forwarded-for'] || '',
127
+ 'http.real_ip': headers['x-real-ip'] || '',
128
+ 'http.request_id': headers['x-request-id'] || headers['x-trace-id'] || '',
129
+ });
130
+
131
+ // Add geographic headers if available (Vercel/Cloudflare)
132
+ if (headers['x-vercel-ip-country']) {
133
+ span.setAttributes({
134
+ 'http.geo.country': headers['x-vercel-ip-country'],
135
+ 'http.geo.region': headers['x-vercel-ip-country-region'] || '',
136
+ 'http.geo.city': headers['x-vercel-ip-city'] || '',
137
+ });
138
+ }
139
+
140
+ if (headers['cf-ipcountry']) {
141
+ span.setAttribute('http.geo.country', headers['cf-ipcountry']);
142
+ }
143
+ } catch (error) {
144
+ // Silently fail to not break the request
145
+ console.debug('[securenow] Failed to capture request metadata:', error.message);
146
+ }
147
+ },
148
+ responseHook: (span, response) => {
149
+ try {
150
+ // Add response metadata
151
+ if (response.statusCode) {
152
+ span.setAttribute('http.status_code', response.statusCode);
153
+ }
154
+ } catch (error) {
155
+ console.debug('[securenow] Failed to capture response metadata:', error.message);
156
+ }
157
+ },
158
+ }),
159
+ ],
160
+ instrumentationConfig: {
161
+ fetch: {
162
+ // Propagate context to your backend APIs
163
+ propagateContextUrls: [
164
+ /^https?:\/\/localhost/,
165
+ /^https?:\/\/.*\.vercel\.app/,
166
+ // Add your backend domains here
167
+ ],
168
+ // Optionally ignore certain URLs
169
+ ignoreUrls: [
170
+ /_next\/static/,
171
+ /_next\/image/,
172
+ /\.map$/,
173
+ ],
174
+ // Add resource name template for better span naming
175
+ resourceNameTemplate: '{http.method} {http.target}',
176
+ },
177
+ },
98
178
  });
99
179
 
100
180
  isRegistered = true;
101
181
  console.log('[securenow] ✅ OpenTelemetry started for Next.js → %s', tracesUrl);
182
+ console.log('[securenow] 📊 Auto-capturing: IP, User-Agent, Headers, Geographic data');
102
183
 
103
184
  // Optional test span
104
185
  if (String(env('SECURENOW_TEST_SPAN')) === '1') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "securenow",
3
- "version": "4.0.1",
3
+ "version": "4.0.2",
4
4
  "description": "OpenTelemetry instrumentation for Node.js and Next.js - Send traces to SigNoz or any OTLP backend",
5
5
  "type": "commonjs",
6
6
  "main": "register.js",
@@ -53,7 +53,8 @@
53
53
  "NEXTJS-GUIDE.md",
54
54
  "NEXTJS-QUICKSTART.md",
55
55
  "CUSTOMER-GUIDE.md",
56
- "AUTO-SETUP.md"
56
+ "AUTO-SETUP.md",
57
+ "AUTOMATIC-IP-CAPTURE.md"
57
58
  ],
58
59
  "dependencies": {
59
60
  "@opentelemetry/api": "1.7.0",
@@ -62,6 +63,7 @@
62
63
  "@opentelemetry/instrumentation": "0.47.0",
63
64
  "@opentelemetry/instrumentation-document-load": "0.47.0",
64
65
  "@opentelemetry/instrumentation-fetch": "0.47.0",
66
+ "@opentelemetry/instrumentation-http": "^0.208.0",
65
67
  "@opentelemetry/instrumentation-user-interaction": "0.47.0",
66
68
  "@opentelemetry/instrumentation-xml-http-request": "0.47.0",
67
69
  "@opentelemetry/resources": "1.20.0",
package/postinstall.js CHANGED
@@ -211,3 +211,5 @@ if (require.main === module || process.env.npm_config_global !== 'true') {
211
211
  module.exports = { setup };
212
212
 
213
213
 
214
+
215
+