Skip to content

Commit

Permalink
Add matchmaker stats api (#1251)
Browse files Browse the repository at this point in the history
  • Loading branch information
sesposito authored Jul 27, 2024
1 parent 501f9a0 commit f822def
Show file tree
Hide file tree
Showing 21 changed files with 2,142 additions and 1,481 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
### Added
- New runtime functions to get and delete notifications by id.
- Add runtime function to disable ranks for an active leaderboard.
- Add new get matchmaker stats API.

### Changed
- Add leaderboard and tournament create param to enable or disable ranks.
Expand All @@ -16,6 +17,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
- Correctly wire Go runtime shutdown function context.
- Fix friends of friends api error when user has no friends.
- Fix group listing pagination if name filter is used.
- Correctly register friends of friends API before/after hooks.

## [3.22.0] - 2024-06-09
### Added
Expand Down
1,187 changes: 598 additions & 589 deletions apigrpc/apigrpc.pb.go

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions apigrpc/apigrpc.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions apigrpc/apigrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ service Nakama {
option (google.api.http).get = "/v2/iap/subscription/{product_id}";
}

// Get matchmaker stats.
rpc GetMatchmakerStats (google.protobuf.Empty) returns (api.MatchmakerStats) {
option (google.api.http).get = "/v2/matchmaker/stats";
}

// A healthcheck which load balancers can use to check the service.
rpc Healthcheck (google.protobuf.Empty) returns (google.protobuf.Empty) {
option (google.api.http).get = "/healthcheck";
Expand Down
58 changes: 58 additions & 0 deletions apigrpc/apigrpc.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2764,6 +2764,29 @@
]
}
},
"/v2/matchmaker/stats": {
"get": {
"summary": "Get matchmaker stats.",
"operationId": "Nakama_GetMatchmakerStats",
"responses": {
"200": {
"description": "A successful response.",
"schema": {
"$ref": "#/definitions/apiMatchmakerStats"
}
},
"default": {
"description": "An unexpected error response.",
"schema": {
"$ref": "#/definitions/rpcStatus"
}
}
},
"tags": [
"Nakama"
]
}
},
"/v2/notification": {
"get": {
"summary": "Fetch list of notifications.",
Expand Down Expand Up @@ -4452,6 +4475,41 @@
},
"description": "A list of realtime matches."
},
"apiMatchmakerCompletionStats": {
"type": "object",
"properties": {
"createTime": {
"type": "string",
"format": "date-time"
},
"completeTime": {
"type": "string",
"format": "date-time"
}
},
"title": "Matchmaker ticket completion stats"
},
"apiMatchmakerStats": {
"type": "object",
"properties": {
"ticketCount": {
"type": "integer",
"format": "int32"
},
"oldestTicketCreateTime": {
"type": "string",
"format": "date-time"
},
"completions": {
"type": "array",
"items": {
"type": "object",
"$ref": "#/definitions/apiMatchmakerCompletionStats"
}
}
},
"title": "Matchmaker stats"
},
"apiNotification": {
"type": "object",
"properties": {
Expand Down
39 changes: 39 additions & 0 deletions apigrpc/apigrpc_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/gorilla/mux v1.8.1
github.com/gorilla/websocket v1.5.1
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0
github.com/heroiclabs/nakama-common v1.32.1-0.20240723124100-b530f4e89fd6
github.com/heroiclabs/nakama-common v1.32.1-0.20240726184732-0e481a55364b
github.com/heroiclabs/sql-migrate v0.0.0-20240528102547-233afc8cf05a
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
github.com/jackc/pgx/v5 v5.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZH
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/heroiclabs/nakama-common v1.32.1-0.20240723124100-b530f4e89fd6 h1:K543q0jEBcAvfiA0wJ/mEGD+jaGCNNARu/V0aG9kKKY=
github.com/heroiclabs/nakama-common v1.32.1-0.20240723124100-b530f4e89fd6/go.mod h1:lPG64MVCs0/tEkh311Cd6oHX9NLx2vAPx7WW7QCJHQ0=
github.com/heroiclabs/nakama-common v1.32.1-0.20240726184732-0e481a55364b h1:qjX3G/dYYVBkCLBCebJ9tHZkRjp1qaSx9dRe7TB2aAc=
github.com/heroiclabs/nakama-common v1.32.1-0.20240726184732-0e481a55364b/go.mod h1:lPG64MVCs0/tEkh311Cd6oHX9NLx2vAPx7WW7QCJHQ0=
github.com/heroiclabs/sql-migrate v0.0.0-20240528102547-233afc8cf05a h1:tuL2ZPaeCbNw8rXmV9ywd00nXRv95V4/FmbIGKLQJAE=
github.com/heroiclabs/sql-migrate v0.0.0-20240528102547-233afc8cf05a/go.mod h1:hzCTPoEi/oml2BllVydJcNP63S7b56e5DzeQeLGvw1U=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down
2 changes: 2 additions & 0 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type ApiServer struct {
router MessageRouter
streamManager StreamManager
metrics Metrics
matchmaker Matchmaker
runtime *Runtime
grpcServer *grpc.Server
grpcGatewayServer *http.Server
Expand Down Expand Up @@ -140,6 +141,7 @@ func StartApiServer(logger *zap.Logger, startupLogger *zap.Logger, db *sql.DB, p
router: router,
streamManager: streamManager,
metrics: metrics,
matchmaker: matchmaker,
runtime: runtime,
grpcServer: grpcServer,
}
Expand Down
59 changes: 59 additions & 0 deletions server/api_matchmaker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2024 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package server

import (
"context"
"github.com/gofrs/uuid/v5"
"github.com/heroiclabs/nakama-common/api"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)

func (s *ApiServer) GetMatchmakerStats(ctx context.Context, in *emptypb.Empty) (*api.MatchmakerStats, error) {
userID := ctx.Value(ctxUserIDKey{}).(uuid.UUID)

// Before hook.
if fn := s.runtime.BeforeGetMatchmakerStats(); fn != nil {
beforeFn := func(clientIP, clientPort string) error {
err, code := fn(ctx, s.logger, userID.String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxVarsKey{}).(map[string]string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort)
if err != nil {
return status.Error(code, err.Error())
}
// Empty input never overridden.
return nil
}

// Execute the before function lambda wrapped in a trace for stats measurement.
err := traceApiBefore(ctx, s.logger, s.metrics, ctx.Value(ctxFullMethodKey{}).(string), beforeFn)
if err != nil {
return nil, err
}
}

stats := s.matchmaker.GetStats()

// After hook.
if fn := s.runtime.AfterGetMatchmakerStats(); fn != nil {
afterFn := func(clientIP, clientPort string) error {
return fn(ctx, s.logger, userID.String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxVarsKey{}).(map[string]string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, stats)
}

// Execute the after function lambda wrapped in a trace for stats measurement.
traceApiAfter(ctx, s.logger, s.metrics, ctx.Value(ctxFullMethodKey{}).(string), afterFn)
}

return stats, nil
}
Loading

0 comments on commit f822def

Please sign in to comment.