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
25 changes: 24 additions & 1 deletion internal/api/endpoint_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import (

"github.com/VKCOM/statshouse-go"

"github.com/VKCOM/tl/pkg/rpc"

"github.com/VKCOM/statshouse/internal/format"
"github.com/VKCOM/statshouse/internal/vkgo/srvfunc"
"github.com/VKCOM/tl/pkg/rpc"
)

const (
Expand Down Expand Up @@ -62,6 +63,7 @@ type endpointStat struct {
lane string
laneMutex sync.Mutex // we access lane from main and badges query
metric string
dashboard string
tokenName string
tokenSource string
user string
Expand All @@ -79,6 +81,19 @@ func (es *endpointStat) reportServiceTime(code int, err error) {
3: es.metric,
}, 1)
}
if len(es.dashboard) != 0 {
statshouse.Count(
format.BuiltinMetricMetaAPIDashboardUsage.Name,
statshouse.Tags{
1: strconv.FormatInt(int64(es.protocol), 10),
2: es.method,
3: srvfunc.Hostname(),
4: es.tokenName,
5: es.user,
6: es.dashboard,
7: es.tokenSource,
}, 1)
}
if es.protocol == format.TagValueIDRPC && code == 0 {
switch e := err.(type) {
case *rpc.Error:
Expand All @@ -103,6 +118,14 @@ func (es *endpointStat) setMetricMeta(metricMeta *format.MetricMetaValue) {
}
}

func (es *endpointStat) setDashboard(name string, id int32) {
if name != "" {
es.dashboard = name
} else if id != 0 {
es.dashboard = strconv.Itoa(int(id))
}
}

func (es *endpointStat) report(code int, metric string) {
v := time.Since(es.timestamp).Seconds()
es.laneMutex.Lock()
Expand Down
9 changes: 8 additions & 1 deletion internal/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2364,7 +2364,13 @@ func HandleGetEntity[T any](r *httpRequestHandler, handle func(ctx context.Conte
}

func HandleGetDashboard(h *httpRequestHandler) {
HandleGetEntity(h, h.handleGetDashboard)
HandleGetEntity(h, func(ctx context.Context, ai accessInfo, id int32, version int64) (*DashboardInfo, time.Duration, error) {
resp, cache, err := h.handleGetDashboard(ctx, ai, id, version)
if resp != nil {
h.endpointStat.setDashboard(resp.Dashboard.Name, resp.Dashboard.DashboardID)
}
return resp, cache, err
})
}

func HandleGetGroup(h *httpRequestHandler) {
Expand Down Expand Up @@ -2404,6 +2410,7 @@ func HandlePutPostDashboard(h *httpRequestHandler) {
if err != nil {
return nil, 0, err
}
h.endpointStat.setDashboard(response.Dashboard.Name, response.Dashboard.DashboardID)
return response, response.Dashboard.Version, nil
})
}
Expand Down
1 change: 1 addition & 0 deletions internal/format/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ var (
-152: BuiltinMetricMetaAggSendSrcBudget,
-153: BuiltinMetricMetaMappingUsage,
-156: BuiltinMetricMetaAggTagMapperInfo,
BuiltinMetricIDAPIDashboardUsage: BuiltinMetricMetaAPIDashboardUsage,
}

// BuiltInGroupDefault can be overridden by journal, don't use directly
Expand Down
33 changes: 33 additions & 0 deletions internal/format/builtin_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3162,3 +3162,36 @@ var BuiltinMetricMetaAggTagMapperInfo = &MetricMetaValue{
}),
}},
}

const BuiltinMetricIDAPIDashboardUsage = -157

var BuiltinMetricMetaAPIDashboardUsage = &MetricMetaValue{
Name: "__api_dashboard_usage",
Kind: MetricKindCounter,
Description: "Dashboard usage",
NoSampleAgent: false,
BuiltinAllowedToReceive: true,
WithAgentEnvRouteArch: false,
WithAggregatorID: false,
Tags: []MetricMetaTag{{
Description: "protocol",
ValueComments: convertToValueComments(map[int32]string{
TagValueIDRPC: "RPC",
TagValueIDHTTP: "http",
}),
}, {
Description: "method",
}, {
Description: "host",
}, {
Description: "token-short",
}, {
Description: "token-long",
}, {
Description: "dashboard",
}, {
Description: "token_source",
RawKind: "int",
ValueComments: convertToValueComments(tokenSourceToValue),
}},
}
Loading