> ## Documentation Index
> Fetch the complete documentation index at: https://s2.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Metrics

> Query account, basin, and stream metrics with S2 SDKs.

Metrics are available at three levels: account, basin, and stream. Each returns time-series or scalar data for the specified metric set.

<Tabs>
  <Tab title="TypeScript">
    ```typescript theme={null}
    const now = new Date();
    const thirtyDaysAgo = new Date(Date.now() - 30 * 24 * 3600 * 1000);
    const sixHoursAgo = new Date(Date.now() - 6 * 3600 * 1000);
    const hourAgo = new Date(Date.now() - 3600 * 1000);

    // Account-level: active basins over the last 30 days
    const accountMetrics = await client.metrics.account({
    	set: "active-basins",
    	start: thirtyDaysAgo,
    	end: now,
    });

    // Basin-level: storage usage with hourly resolution
    const basinMetrics = await client.metrics.basin({
    	basin: "events",
    	set: "storage",
    	start: sixHoursAgo,
    	end: now,
    	interval: "hour",
    });

    // Stream-level: storage for a specific stream
    const streamMetrics = await client.metrics.stream({
    	basin: "events",
    	stream: "user-actions",
    	set: "storage",
    	start: hourAgo,
    	end: now,
    	interval: "minute",
    });
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    now = int(time.time())
    thirty_days_ago = now - 30 * 24 * 3600
    six_hours_ago = now - 6 * 3600
    hour_ago = now - 3600

    # Account-level: active basins over the last 30 days
    account_metrics = await client.account_metrics(
        set=AccountMetricSet.ACTIVE_BASINS,
        start=thirty_days_ago,
        end=now,
    )

    # Basin-level: storage usage with hourly resolution
    basin_metrics = await client.basin_metrics(
        "events",
        set=BasinMetricSet.STORAGE,
        start=six_hours_ago,
        end=now,
        interval=TimeseriesInterval.HOUR,
    )

    # Stream-level: storage for a specific stream
    stream_metrics = await client.stream_metrics(
        "events",
        "user-actions",
        set=StreamMetricSet.STORAGE,
        start=hour_ago,
        end=now,
        interval=TimeseriesInterval.MINUTE,
    )
    ```
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    now := time.Now().Unix()
    thirtyDaysAgo := now - 30*24*3600
    sixHoursAgo := now - 6*3600
    hourAgo := now - 3600

    // Account-level: active basins over the last 30 days
    accountMetrics, _ := client.Metrics.Account(ctx, &s2.AccountMetricsArgs{
    	Set:   s2.AccountMetricSetActiveBasins,
    	Start: &thirtyDaysAgo,
    	End:   &now,
    })

    // Basin-level: storage usage with hourly resolution
    hourInterval := s2.TimeseriesIntervalHour
    basinMetrics, _ := client.Metrics.Basin(ctx, &s2.BasinMetricsArgs{
    	Basin:    "events",
    	Set:      s2.BasinMetricSetStorage,
    	Start:    &sixHoursAgo,
    	End:      &now,
    	Interval: &hourInterval,
    })

    // Stream-level: storage for a specific stream
    minuteInterval := s2.TimeseriesIntervalMinute
    streamMetrics, _ := client.Metrics.Stream(ctx, &s2.StreamMetricsArgs{
    	Basin:    "events",
    	Stream:   "user-actions",
    	Set:      s2.StreamMetricSetStorage,
    	Start:    &hourAgo,
    	End:      &now,
    	Interval: &minuteInterval,
    })
    ```
  </Tab>

  <Tab title="Rust">
    ```rust theme={null}
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)?
        .as_secs() as u32;
    let thirty_days_ago = now - 30 * 24 * 3600;
    let six_hours_ago = now - 6 * 3600;
    let hour_ago = now - 3600;

    // Account-level: active basins over the last 30 days
    let account_metrics = client
        .get_account_metrics(GetAccountMetricsInput::new(AccountMetricSet::ActiveBasins(
            TimeRange::new(thirty_days_ago, now),
        )))
        .await?;

    // Basin-level: storage usage with hourly resolution
    let basin_metrics = client
        .get_basin_metrics(GetBasinMetricsInput::new(
            "events".parse()?,
            BasinMetricSet::Storage(TimeRange::new(six_hours_ago, now)),
        ))
        .await?;

    // Stream-level: storage for a specific stream
    let stream_metrics = client
        .get_stream_metrics(GetStreamMetricsInput::new(
            "events".parse()?,
            "user-actions".parse()?,
            StreamMetricSet::Storage(TimeRange::new(hour_ago, now)),
        ))
        .await?;
    ```
  </Tab>
</Tabs>

See the [metrics guide](/concepts/metrics) for available metric sets and response formats.
