PlatformHttpInterceptor
An Angular HttpInterceptor that automatically measures latency and reports API metrics for every HttpClient request.
Import
import { PlatformHttpInterceptor } from '@mpratyush54/sdk-angular';
Registered automatically by PlatformModule.forRoot() — you typically do not import this class directly.
How It Works
- Skips the SDK's own Cairn calls (
{apiBase}/api/sdk/*) so telemetry is never metered. - Records
Date.now()at request start. - Wraps the
next.handle()observable. - On successful completion: sends a metric with
statusCode: 200and the measured duration. - On error: sends a metric with the actual HTTP error status code and the measured duration.
- POSTs to
POST /api/sdk/api-metrics(withAuthorization: Bearer <token>) with:projectId,environmentroute— the request URL (with params; credential-like values redacted)methodstatusCodedurationMs
Registration
import { HttpClientModule } from '@angular/common/http';
import { PlatformModule } from '@mpratyush54/sdk-angular';
@NgModule({
imports: [
HttpClientModule,
PlatformModule.forRoot({
apiBase: 'https://platform.example.com',
token: 'sdk_live_xxxx',
projectId: 'my-app',
}),
],
})
export class AppModule {}
Behaviour
- Intercepts all
HttpClientcalls (GET, POST, PUT, DELETE, etc.). - Does not modify the request or response — it only observes and reports. The SDK token is never added to your app's own requests.
- The metric POST is sent through
HttpBackend, bypassingHTTP_INTERCEPTORS, so it cannot re-enter this (or any other) interceptor. - The metric POST uses a fire-and-forget
.subscribe()— errors are silently swallowed.
Error Handling
- Interceptor failures are silent — they never propagate to the calling code.
- If the metric POST itself fails, it is ignored (
.subscribe({ error: () => {} })).