Skip to content

Commit

Permalink
Activity Log Filtering Limit Parameter (#16000)
Browse files Browse the repository at this point in the history
* adding changes from ent branch

* adding fmt changes

* adding changelog
  • Loading branch information
akshya96 authored Jun 15, 2022
1 parent 4762806 commit c88c73e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelog/16000.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
core: Limit activity log client count usage by namespaces
```
16 changes: 15 additions & 1 deletion vault/activity_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -1505,7 +1505,7 @@ func (a *ActivityLog) DefaultStartTime(endTime time.Time) time.Time {
return monthStart.AddDate(0, -a.defaultReportMonths+1, 0)
}

func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.Time) (map[string]interface{}, error) {
func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.Time, limitNamespaces int) (map[string]interface{}, error) {
queryNS, err := namespace.FromContext(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1557,6 +1557,7 @@ func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.T
} else {
displayPath = ns.Path
}

byNamespace = append(byNamespace, &ResponseNamespace{
NamespaceID: nsRecord.NamespaceID,
NamespacePath: displayPath,
Expand All @@ -1577,6 +1578,19 @@ func (a *ActivityLog) handleQuery(ctx context.Context, startTime, endTime time.T
sort.Slice(byNamespace, func(i, j int) bool {
return byNamespace[i].Counts.Clients > byNamespace[j].Counts.Clients
})
if limitNamespaces > 0 {
if limitNamespaces > len(byNamespace) {
limitNamespaces = len(byNamespace)
}
byNamespace = byNamespace[:limitNamespaces]
// recalculate total entities and tokens
totalEntities = 0
totalTokens = 0
for _, namespaceData := range byNamespace {
totalEntities += namespaceData.Counts.DistinctEntities
totalTokens += namespaceData.Counts.NonEntityTokens
}
}

responseData["by_namespace"] = byNamespace
responseData["total"] = &ResponseCounts{
Expand Down
12 changes: 11 additions & 1 deletion vault/logical_system_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func (b *SystemBackend) activityQueryPath() *framework.Path {
Type: framework.TypeTime,
Description: "End of query interval",
},
"limit_namespaces": {
Type: framework.TypeInt,
Default: 0,
Description: "Limit query output by namespaces",
},
},
HelpSynopsis: strings.TrimSpace(sysHelp["activity-query"][0]),
HelpDescription: strings.TrimSpace(sysHelp["activity-query"][1]),
Expand Down Expand Up @@ -198,7 +203,12 @@ func (b *SystemBackend) handleClientMetricQuery(ctx context.Context, req *logica
return logical.ErrorResponse(err.Error()), nil
}

results, err := a.handleQuery(ctx, startTime, endTime)
var limitNamespaces int
if limitNamespacesRaw, ok := d.GetOk("limit_namespaces"); ok {
limitNamespaces = limitNamespacesRaw.(int)
}

results, err := a.handleQuery(ctx, startTime, endTime, limitNamespaces)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit c88c73e

Please sign in to comment.