|
1 | | -import { describe, expect, it } from 'vitest'; |
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
2 | 2 | import { Scope } from '../../../src'; |
3 | 3 | import { _INTERNAL_getMetricBuffer } from '../../../src/metrics/internal'; |
4 | 4 | import { count, distribution, gauge } from '../../../src/metrics/public-api'; |
@@ -119,6 +119,123 @@ describe('Metrics Public API', () => { |
119 | 119 |
|
120 | 120 | expect(_INTERNAL_getMetricBuffer(client)).toBeUndefined(); |
121 | 121 | }); |
| 122 | + |
| 123 | + it('captures a counter metric with sample_rate', () => { |
| 124 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableMetrics: true } }); |
| 125 | + const client = new TestClient(options); |
| 126 | + const scope = new Scope(); |
| 127 | + scope.setClient(client); |
| 128 | + |
| 129 | + count('api.requests', 1, { |
| 130 | + scope, |
| 131 | + sample_rate: 0.5, |
| 132 | + }); |
| 133 | + |
| 134 | + const metricBuffer = _INTERNAL_getMetricBuffer(client); |
| 135 | + expect(metricBuffer).toHaveLength(1); |
| 136 | + expect(metricBuffer?.[0]).toEqual( |
| 137 | + expect.objectContaining({ |
| 138 | + name: 'api.requests', |
| 139 | + type: 'counter', |
| 140 | + value: 1, |
| 141 | + attributes: { |
| 142 | + 'sentry.client_sample_rate': { |
| 143 | + value: 0.5, |
| 144 | + type: 'double', |
| 145 | + }, |
| 146 | + }, |
| 147 | + }), |
| 148 | + ); |
| 149 | + }); |
| 150 | + |
| 151 | + it('captures a counter metric with sample_rate and existing attributes', () => { |
| 152 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableMetrics: true } }); |
| 153 | + const client = new TestClient(options); |
| 154 | + const scope = new Scope(); |
| 155 | + scope.setClient(client); |
| 156 | + |
| 157 | + count('api.requests', 1, { |
| 158 | + scope, |
| 159 | + sample_rate: 0.25, |
| 160 | + attributes: { |
| 161 | + endpoint: '/api/users', |
| 162 | + method: 'GET', |
| 163 | + }, |
| 164 | + }); |
| 165 | + |
| 166 | + const metricBuffer = _INTERNAL_getMetricBuffer(client); |
| 167 | + expect(metricBuffer).toHaveLength(1); |
| 168 | + expect(metricBuffer?.[0]).toEqual( |
| 169 | + expect.objectContaining({ |
| 170 | + name: 'api.requests', |
| 171 | + type: 'counter', |
| 172 | + value: 1, |
| 173 | + attributes: { |
| 174 | + endpoint: { |
| 175 | + value: '/api/users', |
| 176 | + type: 'string', |
| 177 | + }, |
| 178 | + method: { |
| 179 | + value: 'GET', |
| 180 | + type: 'string', |
| 181 | + }, |
| 182 | + 'sentry.client_sample_rate': { |
| 183 | + value: 0.25, |
| 184 | + type: 'double', |
| 185 | + }, |
| 186 | + }, |
| 187 | + }), |
| 188 | + ); |
| 189 | + }); |
| 190 | + |
| 191 | + it('drops metrics with sample_rate above 1', () => { |
| 192 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableMetrics: true } }); |
| 193 | + const client = new TestClient(options); |
| 194 | + const scope = new Scope(); |
| 195 | + scope.setClient(client); |
| 196 | + |
| 197 | + count('api.requests', 1, { |
| 198 | + scope, |
| 199 | + sample_rate: 1.5, |
| 200 | + }); |
| 201 | + |
| 202 | + const metricBuffer = _INTERNAL_getMetricBuffer(client); |
| 203 | + expect(metricBuffer).toBeUndefined(); |
| 204 | + }); |
| 205 | + |
| 206 | + it('drops metrics with sample_rate at or below 0', () => { |
| 207 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableMetrics: true } }); |
| 208 | + const client = new TestClient(options); |
| 209 | + const scope = new Scope(); |
| 210 | + scope.setClient(client); |
| 211 | + |
| 212 | + count('api.requests', 1, { |
| 213 | + scope, |
| 214 | + sample_rate: 0, |
| 215 | + }); |
| 216 | + |
| 217 | + const metricBuffer = _INTERNAL_getMetricBuffer(client); |
| 218 | + expect(metricBuffer).toBeUndefined(); |
| 219 | + }); |
| 220 | + |
| 221 | + it('records dropped event for invalid sample_rate values', () => { |
| 222 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableMetrics: true } }); |
| 223 | + const client = new TestClient(options); |
| 224 | + const scope = new Scope(); |
| 225 | + scope.setClient(client); |
| 226 | + |
| 227 | + const recordDroppedEventSpy = vi.spyOn(client, 'recordDroppedEvent'); |
| 228 | + |
| 229 | + count('api.requests', 1, { scope, sample_rate: 0 }); |
| 230 | + count('api.requests', 1, { scope, sample_rate: -0.5 }); |
| 231 | + count('api.requests', 1, { scope, sample_rate: 1.5 }); |
| 232 | + |
| 233 | + expect(recordDroppedEventSpy).toHaveBeenCalledTimes(3); |
| 234 | + expect(recordDroppedEventSpy).toHaveBeenCalledWith('invalid_sample_rate', 'metric'); |
| 235 | + |
| 236 | + const metricBuffer = _INTERNAL_getMetricBuffer(client); |
| 237 | + expect(metricBuffer).toBeUndefined(); |
| 238 | + }); |
122 | 239 | }); |
123 | 240 |
|
124 | 241 | describe('gauge', () => { |
@@ -209,6 +326,34 @@ describe('Metrics Public API', () => { |
209 | 326 |
|
210 | 327 | expect(_INTERNAL_getMetricBuffer(client)).toBeUndefined(); |
211 | 328 | }); |
| 329 | + |
| 330 | + it('captures a gauge metric with sample_rate', () => { |
| 331 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableMetrics: true } }); |
| 332 | + const client = new TestClient(options); |
| 333 | + const scope = new Scope(); |
| 334 | + scope.setClient(client); |
| 335 | + |
| 336 | + gauge('memory.usage', 1024, { |
| 337 | + scope, |
| 338 | + sample_rate: 0.75, |
| 339 | + }); |
| 340 | + |
| 341 | + const metricBuffer = _INTERNAL_getMetricBuffer(client); |
| 342 | + expect(metricBuffer).toHaveLength(1); |
| 343 | + expect(metricBuffer?.[0]).toEqual( |
| 344 | + expect.objectContaining({ |
| 345 | + name: 'memory.usage', |
| 346 | + type: 'gauge', |
| 347 | + value: 1024, |
| 348 | + attributes: { |
| 349 | + 'sentry.client_sample_rate': { |
| 350 | + value: 0.75, |
| 351 | + type: 'double', |
| 352 | + }, |
| 353 | + }, |
| 354 | + }), |
| 355 | + ); |
| 356 | + }); |
212 | 357 | }); |
213 | 358 |
|
214 | 359 | describe('distribution', () => { |
@@ -299,6 +444,34 @@ describe('Metrics Public API', () => { |
299 | 444 |
|
300 | 445 | expect(_INTERNAL_getMetricBuffer(client)).toBeUndefined(); |
301 | 446 | }); |
| 447 | + |
| 448 | + it('captures a distribution metric with sample_rate', () => { |
| 449 | + const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN, _experiments: { enableMetrics: true } }); |
| 450 | + const client = new TestClient(options); |
| 451 | + const scope = new Scope(); |
| 452 | + scope.setClient(client); |
| 453 | + |
| 454 | + distribution('task.duration', 500, { |
| 455 | + scope, |
| 456 | + sample_rate: 0.1, |
| 457 | + }); |
| 458 | + |
| 459 | + const metricBuffer = _INTERNAL_getMetricBuffer(client); |
| 460 | + expect(metricBuffer).toHaveLength(1); |
| 461 | + expect(metricBuffer?.[0]).toEqual( |
| 462 | + expect.objectContaining({ |
| 463 | + name: 'task.duration', |
| 464 | + type: 'distribution', |
| 465 | + value: 500, |
| 466 | + attributes: { |
| 467 | + 'sentry.client_sample_rate': { |
| 468 | + value: 0.1, |
| 469 | + type: 'double', |
| 470 | + }, |
| 471 | + }, |
| 472 | + }), |
| 473 | + ); |
| 474 | + }); |
302 | 475 | }); |
303 | 476 |
|
304 | 477 | describe('mixed metric types', () => { |
|
0 commit comments