Skip to main content

Observability

Callback hooks for error tracking and performance metrics, compatible with tools like Sentry or Datadog. Pass an ObservabilityHook instance to GustoProvider via config.observability.

ObservabilityHook

Hooks for routing SDK errors and performance metrics into an external monitoring tool.

Remarks

Pass an instance to GustoProvider via config.observability to forward errors to services like Sentry or Datadog and to capture performance metrics for form submissions and component loading. Sanitization is applied before the hooks are invoked; see SanitizationConfig.

Example

import * as Sentry from '@sentry/react'
import { GustoProvider, type ObservabilityHook } from '@gusto/embedded-react-sdk'

const observability: ObservabilityHook = {
onError: error => {
Sentry.captureException(error.raw ?? new Error(error.message), {
level: error.category === 'validation_error' ? 'warning' : 'error',
tags: {
error_category: error.category,
component: error.componentName ?? 'unknown',
http_status: String(error.httpStatus ?? ''),
},
})
},
onMetric: metric => {
console.log(`[Metric] ${metric.name}: ${metric.value}${metric.unit ?? ''}`)
},
}

<GustoProvider config={{ baseUrl: '/api/', observability }}>
<YourApp />
</GustoProvider>

Properties

PropertyTypeDescription
onError?(error: ObservabilityError) => voidCalled when an error is caught by error boundaries or form submission fails. Receives an ObservabilityError — an SDKError enriched with componentName and (for boundary errors) componentStack.
onMetric?(metric: ObservabilityMetric) => voidCalled to track performance metrics for component operations.
sanitization?SanitizationConfigConfiguration for sanitizing data before sending to observability tools. Default: { enabled: true, includeRawError: false }

Utility types

ObservabilityError

An SDKError enriched with component context for observability telemetry.

Remarks

Delivered to ObservabilityHook.onError. Extends SDKError with timestamp, componentName, and componentStack so error-tracking tools (e.g. Sentry) can correlate and group errors. The base SDKError (without these fields) is the shape exposed through form hooks.

Extends

Properties

PropertyTypeDescription
categorySDKErrorCategoryHigh-level error classification
fieldErrorsSDKFieldError[]Flattened field-level errors from API responses. Empty array for non-field errors.
messagestringHuman-readable error summary
timestampnumberWhen the error occurred (Unix timestamp in milliseconds)
componentName?stringSDK component where the error occurred (e.g. "Employee.Profile")
componentStack?stringReact component stack trace (present only for errors caught by ErrorBoundary)
httpStatus?numberHTTP status code (undefined for non-HTTP errors like network or validation)
raw?unknownThe original error object for advanced use cases. May be stripped by sanitization (controlled by sanitization.includeRawError).

ObservabilityMetric

A performance metric emitted by the SDK to ObservabilityHook.onMetric.

Remarks

Built-in metric names include sdk.form.submit_duration (form submission time) and sdk.component.loading_duration (time spent in loading/suspense state). Tags may include status (success or error) and component when known.

Properties

PropertyTypeDescription
namestringMetric name (e.g., 'sdk.form.submit_duration', 'sdk.component.loading_duration')
timestampnumberWhen the metric was recorded (Unix timestamp in milliseconds)
valuenumberMetric value
tags?Record<string, string>Tags for filtering/grouping
unit?ObservabilityMetricUnitMetric unit

ObservabilityMetricUnit

ObservabilityMetricUnit = "ms" | "count" | "bytes" | "percent"

Unit of measure for an ObservabilityMetric.


SanitizationConfig

Configuration for sanitizing error and metric data before it reaches observability hooks.

Remarks

Sanitization runs by default to prevent PII leakage. When enabled, the SDK pattern-redacts SSNs, emails, phone numbers, credit card numbers, and API tokens from messages and tags, and removes values for fields with sensitive names (password, ssn, bankAccount, etc.) from metadata. The raw error object is excluded unless includeRawError is set to true.

Properties

PropertyTypeDescription
additionalSensitiveFields?string[]Additional field names to treat as sensitive (case-insensitive)
customErrorSanitizer?(error: ObservabilityError) => ObservabilityErrorCustom sanitization function for errors
customMetricSanitizer?(metric: ObservabilityMetric) => ObservabilityMetricCustom sanitization function for metrics
enabled?booleanWhether to sanitize error data. Default: true
includeRawError?booleanWhether to include the raw error object on SDKError. Default: false WARNING: Raw errors may contain sensitive data from form inputs or API responses