Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/reference/qcloud_serverless_space_metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Metrics are reported per collection: request rates, search latency, point
counts, storage usage and the number of search workers. The summary command
gives a current overview, while the usage command reports time series over a
chosen period. Both also report the space's quota so that usage can be compared
against the limits.
against the limits. The inference command reports the tokens processed by
inference per collection and model.

### Examples

Expand All @@ -20,6 +21,9 @@ qcloud serverless space metrics summary 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60

# Show usage over the last 24 hours
qcloud serverless space metrics usage 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --since 24h

# Show inference token usage over the last 30 days
qcloud serverless space metrics inference 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --since 30d
```

### Options
Expand All @@ -44,6 +48,7 @@ qcloud serverless space metrics usage 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --sin
### SEE ALSO

* [qcloud serverless space](qcloud_serverless_space.md) - Manage serverless spaces
* [qcloud serverless space metrics inference](qcloud_serverless_space_metrics_inference.md) - Show inference token usage of a space's collections
* [qcloud serverless space metrics summary](qcloud_serverless_space_metrics_summary.md) - Show a metrics overview of a space's collections
* [qcloud serverless space metrics usage](qcloud_serverless_space_metrics_usage.md) - Show usage metrics of a space's collections over time

73 changes: 73 additions & 0 deletions docs/reference/qcloud_serverless_space_metrics_inference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## qcloud serverless space metrics inference

Show inference token usage of a space's collections

### Synopsis

Show the inference token usage of the collections in a serverless space over a period.

The server reports, per collection and inference model, the number of tokens
processed by inference, aggregated into buckets of one day, week or month. The
table shows one row per collection and model with the total number of tokens
over the period and the largest bucket. The total over all rows is shown above
the table. Use --json to get the individual buckets.

By default, the period covers the last 7 days and buckets are aggregated per
day. When only --until is given, the period covers the 7 days before it.

By default, all collections are fetched automatically across multiple pages.
Use --page-size and --page-token for manual pagination.

```
qcloud serverless space metrics inference <space-id> [flags]
```

### Examples

```
# Show inference token usage over the last 7 days
qcloud serverless space metrics inference 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60

# Show weekly token usage over the last 90 days
qcloud serverless space metrics inference 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --since 90d --interval week

# Show token usage of a single model in a single collection during October
qcloud serverless space metrics inference 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 \
--collection products --model sentence-transformers/all-minilm-l6-v2 \
--since 2026-10-01 --until 2026-10-31

# Export the daily buckets as JSON
qcloud serverless space metrics inference 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --json
```

### Options

```
--collection string Only include the collection with this exact name
--collection-contains string Only include collections whose name contains this substring
-h, --help[=true|false] help for inference
--interval string Size of the aggregation buckets (day, week, month; default day)
--model string Only include the inference model with this ID
--page-size int32 Maximum number of collections to return per page (manual pagination mode)
--page-token string Page token from a previous response to resume from (manual pagination mode)
-s, --since string Start of the time range (RFC3339, YYYY-MM-DD, or a duration ago such as 24h or 7d; default: 7 days before --until)
-u, --until string End of the time range (RFC3339, YYYY-MM-DD, or a duration ago such as 1h; default: now)
```

### Options inherited from parent commands

```
--account-id string Qdrant Cloud Account ID (env: QDRANT_CLOUD_ACCOUNT_ID)
--api-key string Management API Key (env: QDRANT_CLOUD_API_KEY)
-c, --config string Config file path (env: QDRANT_CLOUD_CONFIG, default ~/.config/qcloud/config.yaml)
--console-url string Qdrant Cloud web console base URL (env: QDRANT_CLOUD_CONSOLE_URL, default https://cloud.qdrant.io)
--context string Override the active context (env: QDRANT_CLOUD_CONTEXT)
--debug[=true|false] Enable debug logging to stderr
--endpoint string gRPC API endpoint (env: QDRANT_CLOUD_ENDPOINT, default grpc.cloud.qdrant.io:443)
--json[=true|false] Output as JSON
```

### SEE ALSO

* [qcloud serverless space metrics](qcloud_serverless_space_metrics.md) - Show metrics of a space's collections

9 changes: 7 additions & 2 deletions internal/cmd/serverless/space/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ Metrics are reported per collection: request rates, search latency, point
counts, storage usage and the number of search workers. The summary command
gives a current overview, while the usage command reports time series over a
chosen period. Both also report the space's quota so that usage can be compared
against the limits.`,
against the limits. The inference command reports the tokens processed by
inference per collection and model.`,
Example: `# Show a current overview of all collections in a space
qcloud serverless space metrics summary 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60

# Show usage over the last 24 hours
qcloud serverless space metrics usage 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --since 24h`,
qcloud serverless space metrics usage 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --since 24h

# Show inference token usage over the last 30 days
qcloud serverless space metrics inference 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --since 30d`,
Args: cobra.NoArgs,
}
cmd.AddCommand(
newMetricsSummaryCommand(s),
newMetricsUsageCommand(s),
newMetricsInferenceCommand(s),
)
return cmd
}
242 changes: 242 additions & 0 deletions internal/cmd/serverless/space/metrics_inference.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
package space

import (
"errors"
"fmt"
"io"
"strconv"
"strings"
"time"

"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/timestamppb"

serverlessmonitoringv1 "github.com/qdrant/qdrant-cloud-public-api/gen/go/qdrant/cloud/serverless/monitoring/v1"

"github.com/qdrant/qcloud-cli/internal/cmd/base"
"github.com/qdrant/qcloud-cli/internal/cmd/completion"
"github.com/qdrant/qcloud-cli/internal/cmd/output"
"github.com/qdrant/qcloud-cli/internal/cmd/util"
"github.com/qdrant/qcloud-cli/internal/state"
)

// defaultInferenceMetricsRange matches the server default of 7 days.
const defaultInferenceMetricsRange = 7 * 24 * time.Hour

var inferenceIntervals = []string{"day", "week", "month"}

func newMetricsInferenceCommand(s *state.State) *cobra.Command {
cmd := base.DescribeCmd[*serverlessmonitoringv1.GetSpaceInferenceMetricsResponse]{
Use: "inference <space-id>",
Short: "Show inference token usage of a space's collections",
Long: `Show the inference token usage of the collections in a serverless space over a period.

The server reports, per collection and inference model, the number of tokens
processed by inference, aggregated into buckets of one day, week or month. The
table shows one row per collection and model with the total number of tokens
over the period and the largest bucket. The total over all rows is shown above
the table. Use --json to get the individual buckets.

By default, the period covers the last 7 days and buckets are aggregated per
day. When only --until is given, the period covers the 7 days before it.

By default, all collections are fetched automatically across multiple pages.
Use --page-size and --page-token for manual pagination.`,
Example: `# Show inference token usage over the last 7 days
qcloud serverless space metrics inference 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60

# Show weekly token usage over the last 90 days
qcloud serverless space metrics inference 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --since 90d --interval week

# Show token usage of a single model in a single collection during October
qcloud serverless space metrics inference 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 \
--collection products --model sentence-transformers/all-minilm-l6-v2 \
--since 2026-10-01 --until 2026-10-31

# Export the daily buckets as JSON
qcloud serverless space metrics inference 0e7a3c1d-5f2b-4c8e-9a6d-1b2c3d4e5f60 --json`,
Args: util.ExactArgs(1, "a space ID"),
Fetch: func(s *state.State, cmd *cobra.Command, args []string) (*serverlessmonitoringv1.GetSpaceInferenceMetricsResponse, error) {
since, until, err := util.ReadPastTimeRange(cmd, time.Now(), defaultInferenceMetricsRange)
if err != nil {
return nil, err
}

filter, err := readCollectionFilter(cmd)
if err != nil {
return nil, err
}

var interval *serverlessmonitoringv1.InferenceMetricsInterval
if cmd.Flags().Changed("interval") {
v, _ := cmd.Flags().GetString("interval")
i, err := parseInferenceInterval(v)
if err != nil {
return nil, err
}

interval = &i
}

var model *string
if cmd.Flags().Changed("model") {
v, _ := cmd.Flags().GetString("model")
if v == "" {
return nil, errors.New("--model must not be empty")
}

model = &v
}

ctx := cmd.Context()
client, err := s.Client(ctx)
if err != nil {
return nil, err
}

accountID, err := s.AccountID()
if err != nil {
return nil, err
}

items, next, total, err := util.FetchPages(cmd, func(pageSize *int32, pageToken *string) ([]*serverlessmonitoringv1.SpaceCollectionInferenceMetrics, string, *int32, error) {
req := &serverlessmonitoringv1.GetSpaceInferenceMetricsRequest{
AccountId: accountID,
SpaceId: args[0],
Since: timestamppb.New(since),
Until: timestamppb.New(until),
Interval: interval,
InferenceModelId: model,
PageSize: pageSize,
PageToken: pageToken,
}
switch {
case filter.name != nil:
req.CollectionFilter = &serverlessmonitoringv1.GetSpaceInferenceMetricsRequest_CollectionName{CollectionName: *filter.name}
case filter.contains != nil:
req.CollectionFilter = &serverlessmonitoringv1.GetSpaceInferenceMetricsRequest_CollectionNameContains{CollectionNameContains: *filter.contains}
}

resp, err := client.ServerlessMonitoring().GetSpaceInferenceMetrics(ctx, req)
if err != nil {
return nil, "", nil, fmt.Errorf("failed to get space inference metrics: %w", err)
}

return resp.GetItems(), resp.GetNextPageToken(), resp.TotalSize, nil
})
if err != nil {
return nil, err
}

return &serverlessmonitoringv1.GetSpaceInferenceMetricsResponse{
Items: items,
TotalSize: total,
NextPageToken: next,
}, nil
},
PrintText: func(cmd *cobra.Command, w io.Writer, resp *serverlessmonitoringv1.GetSpaceInferenceMetricsResponse) error {
intervalName := "day"
if cmd.Flags().Changed("interval") {
intervalName, _ = cmd.Flags().GetString("interval")
}

type row struct {
collection string
model string
total float64
peak float64
ok bool
}

var rows []row
var grandTotal float64
var first, last time.Time
var hasPeriod bool
for _, item := range resp.GetItems() {
if len(item.GetModels()) == 0 {
rows = append(rows, row{collection: item.GetCollectionName(), model: "-"})
continue
}

for _, m := range item.GetModels() {
r := row{collection: item.GetCollectionName(), model: m.GetInferenceModelId()}
for _, v := range m.GetValues() {
r.ok = true
r.total += v.GetValue()
r.peak = max(r.peak, v.GetValue())
if v.GetTimestamp() == nil {
continue
}

ts := v.GetTimestamp().AsTime()
if !hasPeriod || ts.Before(first) {
first = ts
}

if !hasPeriod || ts.After(last) {
last = ts
}

hasPeriod = true
}

grandTotal += r.total
rows = append(rows, r)
}
}

if hasPeriod {
fmt.Fprintf(w, "Buckets: per %s, %s to %s\n", intervalName, output.FullDateTime(first), output.FullDateTime(last))
}

fmt.Fprintf(w, "Total Tokens: %s\n\n", tokens(grandTotal))

value := func(r row, v float64) string {
if !r.ok {
return "-"
}

return tokens(v)
}

t := output.NewTable[row](w)
t.AddField("COLLECTION", func(r row) string { return r.collection })
t.AddField("MODEL", func(r row) string { return r.model })
t.AddField("TOKENS", func(r row) string { return value(r, r.total) })
t.AddField("PEAK PER "+strings.ToUpper(intervalName), func(r row) string { return value(r, r.peak) })
t.SetItems(rows)
t.Render()

return nil
},
ValidArgsFunction: completion.SpaceIDCompletion(s),
}.CobraCommand(s)

util.AddTimeRangeFlags(cmd, defaultInferenceMetricsRange)
cmd.Flags().String("interval", "", "Size of the aggregation buckets (day, week, month; default day)")
_ = cmd.RegisterFlagCompletionFunc("interval", cobra.FixedCompletions(inferenceIntervals, cobra.ShellCompDirectiveNoFileComp))
cmd.Flags().String("model", "", "Only include the inference model with this ID")
addCollectionFilterFlags(cmd, false)
util.AddPaginationFlags(cmd, "collections")

return cmd
}

// parseInferenceInterval maps the --interval flag value to the proto enum.
func parseInferenceInterval(s string) (serverlessmonitoringv1.InferenceMetricsInterval, error) {
switch s {
case "day":
return serverlessmonitoringv1.InferenceMetricsInterval_INFERENCE_METRICS_INTERVAL_DAY, nil
case "week":
return serverlessmonitoringv1.InferenceMetricsInterval_INFERENCE_METRICS_INTERVAL_WEEK, nil
case "month":
return serverlessmonitoringv1.InferenceMetricsInterval_INFERENCE_METRICS_INTERVAL_MONTH, nil
default:
return 0, fmt.Errorf("invalid --interval %q: must be one of %s", s, strings.Join(inferenceIntervals, ", "))
}
}

// tokens formats a token count as a whole number.
func tokens(v float64) string {
return strconv.FormatUint(uint64(v+0.5), 10)
}
Loading