var s=Object.defineProperty;var l=(r)=>r;function n(r,o){this[r]=l.bind(null,o)}var d=(r,o)=>{for(var a in o)s(r,a,{get:o[a],enumerable:!0,configurable:!0,set:n.bind(o,a)})};var m=(r,o)=>()=>(r&&(o=r(r=0)),o);import i from"@contractspec/lib.ui-link";import{ChevronRight as c}from"lucide-react";import{jsx as e,jsxs as t}from"react/jsx-runtime";function y(){return t("div",{className:"space-y-8",children:[t("div",{className:"space-y-4",children:[e("h1",{className:"font-bold text-4xl",children:"Telemetry"}),t("p",{className:"text-muted-foreground",children:["A ",e("strong",{children:"TelemetrySpec"})," defines what metrics, logs, and traces to collect for observability. ContractSpec automatically instruments your application based on these specs, ensuring you have the visibility you need to monitor, debug, and optimize your system."]})]}),t("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Why telemetry matters"}),e("p",{className:"text-muted-foreground",children:"You can't fix what you can't see. Telemetry provides visibility into how your application is performing, where errors are occurring, and how users are interacting with your system. Without proper instrumentation, you're flying blind in production."}),e("p",{className:"text-muted-foreground",children:"ContractSpec takes a spec-first approach to telemetry: you declare what you want to observe, and runtime adapters instrument operations automatically. This ensures consistent, comprehensive coverage without manual effort."})]}),t("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Three pillars of observability"}),t("div",{className:"space-y-3",children:[t("div",{children:[e("h3",{className:"font-semibold text-lg",children:"Metrics"}),e("p",{className:"text-muted-foreground",children:"Numerical measurements collected over time. Examples: request count, error rate, latency percentiles, active users, queue depth. Metrics are cheap to collect and store, making them ideal for high-level monitoring and alerting."})]}),t("div",{children:[e("h3",{className:"font-semibold text-lg",children:"Logs"}),e("p",{className:"text-muted-foreground",children:'Timestamped text records of events. Examples: "User 123 logged in", "Payment failed for order 456", "Database connection pool exhausted". Logs provide detailed context for debugging specific issues.'})]}),t("div",{children:[e("h3",{className:"font-semibold text-lg",children:"Traces"}),e("p",{className:"text-muted-foreground",children:"Records of requests as they flow through your system. A trace shows the complete path of a request—which services it touched, how long each step took, and where errors occurred. Traces are essential for debugging distributed systems."})]})]})]}),t("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Example TelemetrySpec"}),e("p",{className:"text-muted-foreground",children:"Here's how telemetry is configured in TypeScript:"}),e("div",{className:"overflow-x-auto rounded-lg border border-border bg-background/50 p-4 font-mono text-muted-foreground text-sm",children:e("pre",{children:`import { defineTelemetry } from '@contractspec/lib.contracts-spec/telemetry';

export const OrderProcessingTelemetry = defineTelemetry({
  meta: {
    key: 'order.processing.observability',
    version: '1.0.0',
  },
  metrics: [
    {
      name: 'orders_created_total',
      type: 'counter',
      description: 'Total number of orders created',
      labels: ['status', 'payment_method'],
    },
    {
      name: 'order_processing_duration_seconds',
      type: 'histogram',
      description: 'Time to process an order',
      buckets: [0.1, 0.5, 1.0, 2.0, 5.0, 10.0],
      labels: ['status'],
    },
  ],
  traces: [
    {
      operation: 'createOrder',
      sampleRate: 1.0, // Trace 100% of requests
      includeInputs: true,
      includeOutputs: true,
      redactFields: ['creditCard', 'ssn'],
    },
    {
      operation: 'processPayment',
      sampleRate: 0.1, // Trace 10% of requests
      includeInputs: false, // Don't log payment details
    },
  ],
  alerts: [
    {
      name: 'high-error-rate',
      condition: 'error_rate > 0.05',
      duration: '5m',
      severity: 'critical',
      notify: ['pagerduty', 'slack'],
    },
  ],
});`})})]}),t("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Automatic instrumentation"}),e("p",{className:"text-muted-foreground",children:"ContractSpec automatically instruments:"}),t("ul",{className:"list-inside list-disc space-y-2 text-muted-foreground",children:[t("li",{children:[e("strong",{children:"All operations"})," – Request count, latency, error rate per Command/Query"]}),t("li",{children:[e("strong",{children:"All workflows"})," – Step execution time, retry counts, compensation events"]}),t("li",{children:[e("strong",{children:"All data views"})," – Query execution time, result set size"]}),t("li",{children:[e("strong",{children:"All policy decisions"})," – Decision time, permit/deny ratio"]}),t("li",{children:[e("strong",{children:"System resources"})," – CPU, memory, disk, network usage"]})]}),e("p",{className:"text-muted-foreground",children:"You don't need to add instrumentation code manually—the runtime handles it based on your specs."})]}),t("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Integration with observability platforms"}),e("p",{className:"text-muted-foreground",children:"ContractSpec supports multiple observability backends:"}),t("ul",{className:"list-inside list-disc space-y-2 text-muted-foreground",children:[t("li",{children:[e("strong",{children:"Prometheus"})," – For metrics collection and alerting"]}),t("li",{children:[e("strong",{children:"Grafana"})," – For dashboards and visualization"]}),t("li",{children:[e("strong",{children:"Jaeger / Tempo"})," – For distributed tracing"]}),t("li",{children:[e("strong",{children:"Loki"})," – For log aggregation"]}),t("li",{children:[e("strong",{children:"Datadog"})," – All-in-one observability platform"]}),t("li",{children:[e("strong",{children:"New Relic"})," – Application performance monitoring"]}),t("li",{children:[e("strong",{children:"Honeycomb"})," – Observability for complex systems"]})]}),e("p",{className:"text-muted-foreground",children:"You can configure multiple backends and send telemetry to all of them simultaneously."})]}),t("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Sampling and performance"}),e("p",{className:"text-muted-foreground",children:"Collecting telemetry has a cost—CPU, memory, network bandwidth, and storage. ContractSpec provides several mechanisms to control overhead:"}),t("ul",{className:"list-inside list-disc space-y-2 text-muted-foreground",children:[t("li",{children:[e("strong",{children:"Sampling"})," – Trace only a percentage of requests (e.g., 10%)"]}),t("li",{children:[e("strong",{children:"Adaptive sampling"})," – Automatically reduce sampling rate under high load"]}),t("li",{children:[e("strong",{children:"Tail-based sampling"})," – Keep traces for failed requests, sample successful ones"]}),t("li",{children:[e("strong",{children:"Field redaction"})," – Remove sensitive data from traces and logs"]}),t("li",{children:[e("strong",{children:"Aggregation"})," – Pre-aggregate metrics before sending to reduce network traffic"]})]})]}),t("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Best practices"}),t("ul",{className:"list-inside list-disc space-y-2 text-muted-foreground",children:[e("li",{children:"Start with high-level metrics (request rate, error rate, latency) and add more detailed instrumentation as needed."}),e("li",{children:"Use structured logging—log events with structured fields, not free-form text."}),e("li",{children:"Set up alerts for critical metrics so you're notified when things go wrong."}),e("li",{children:"Use traces to debug complex issues—they show the complete picture of a request."}),e("li",{children:"Redact sensitive data from logs and traces to comply with privacy regulations."}),e("li",{children:"Review dashboards regularly to understand normal behavior—this makes anomalies easier to spot."}),e("li",{children:"Use sampling to control costs, but always trace errors and slow requests."})]})]}),t("div",{className:"flex items-center gap-4 pt-4",children:[e(i,{href:"/docs/advanced/mcp",className:"btn-ghost",children:"Previous: MCP Adapters"}),t(i,{href:"/docs/comparison",className:"btn-primary",children:["Next: Comparison ",e(c,{size:16})]})]})]})}export{y as AdvancedTelemetryPage};
