From dc8d2af2d858b986a0cfbd90adaf1b5d232daaeb Mon Sep 17 00:00:00 2001 From: Mike Palmiotto Date: Mon, 22 May 2023 09:22:45 -0400 Subject: [PATCH] Add current_billing_period activity endpoint param (#20694) * Add current_billing_period activity endpoint param This commit introduces a new parameter: `current_billing_period`, which can be used in lieu of `start_time` and `end_time` options. GET ... /sys/internal/counters/activity?current_billing_period=true now results in a response which contains the full billing period information. * changelog * Update internal counters docs --- changelog/20694.txt | 4 ++++ vault/logical_system_activity.go | 17 ++++++++++++++--- .../api-docs/system/internal-counters.mdx | 4 ++++ 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 changelog/20694.txt diff --git a/changelog/20694.txt b/changelog/20694.txt new file mode 100644 index 000000000..07f790a66 --- /dev/null +++ b/changelog/20694.txt @@ -0,0 +1,4 @@ +```release-note:improvement +api: GET ... /sys/internal/counters/activity?current_billing_period=true now +results in a response which contains the full billing period +``` diff --git a/vault/logical_system_activity.go b/vault/logical_system_activity.go index 607df3014..9ad930b5d 100644 --- a/vault/logical_system_activity.go +++ b/vault/logical_system_activity.go @@ -29,6 +29,10 @@ func (b *SystemBackend) activityQueryPath() *framework.Path { }, Fields: map[string]*framework.FieldSchema{ + "current_billing_period": { + Type: framework.TypeBool, + Description: "Query utilization for configured billing period", + }, "start_time": { Type: framework.TypeTime, Description: "Start of query interval", @@ -236,6 +240,7 @@ func (b *SystemBackend) handleClientExport(ctx context.Context, req *logical.Req } func (b *SystemBackend) handleClientMetricQuery(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) { + var startTime, endTime time.Time b.Core.activityLogLock.RLock() a := b.Core.activityLog b.Core.activityLogLock.RUnlock() @@ -243,9 +248,15 @@ func (b *SystemBackend) handleClientMetricQuery(ctx context.Context, req *logica return logical.ErrorResponse("no activity log present"), nil } - startTime, endTime, err := parseStartEndTimes(a, d) - if err != nil { - return logical.ErrorResponse(err.Error()), nil + if d.Get("current_billing_period").(bool) { + startTime = b.Core.BillingStart() + endTime = time.Now().UTC() + } else { + var err error + startTime, endTime, err = parseStartEndTimes(a, d) + if err != nil { + return logical.ErrorResponse(err.Error()), nil + } } var limitNamespaces int diff --git a/website/content/api-docs/system/internal-counters.mdx b/website/content/api-docs/system/internal-counters.mdx index 6c1077c05..8c3ac0079 100644 --- a/website/content/api-docs/system/internal-counters.mdx +++ b/website/content/api-docs/system/internal-counters.mdx @@ -346,6 +346,10 @@ This endpoint was added in Vault 1.6. - `limit_namespaces` `(int, optional)` - Controls the total number of by_namespace data returned. This can be used to return the client counts for the specified number of namespaces having highest activity. If no `limit_namespaces` parameter is specified, client counts for all namespaces in specified usage period is returned. +- `current_billing_period` `(bool, optional)` - Uses the builtin billing start + timestamp as `start_time` and the current time as the `end_time`, returning a + response with the current billing period information without having to + explicitly provide a start and end time. ### Sample Request