Skip to content

Commit

Permalink
chore(usage): adopt new usage-client to include owner_uid in session (#…
Browse files Browse the repository at this point in the history
…410)

Because

- support more complex queries for usage metrics

This commit

- adopt new usage-client to include owner_uid in session
  • Loading branch information
heiruwu authored Aug 31, 2023
1 parent b33070b commit 10d2d72
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0
github.com/iancoleman/strcase v0.2.0
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20230829050804-7cbee906e52d
github.com/instill-ai/usage-client v0.2.4-alpha
github.com/instill-ai/usage-client v0.2.4-alpha.0.20230814155646-874e57a1e4b0
github.com/instill-ai/x v0.3.0-alpha
github.com/knadh/koanf v1.4.4
github.com/mennanov/fieldmask-utils v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,8 @@ github.com/instill-ai/protogen-go v0.3.3-alpha.0.20230829050804-7cbee906e52d h1:
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20230829050804-7cbee906e52d/go.mod h1:qsq5ecnA1xi2rLnVQFo/9xksA7I7wQu8c7rqM5xbIrQ=
github.com/instill-ai/usage-client v0.2.4-alpha h1:mYXd62eZsmGKBlzwMcdEgTBgn8zlbagYUHro6+p50c8=
github.com/instill-ai/usage-client v0.2.4-alpha/go.mod h1:BMxgyr02sqH6SeITXSV4M1ewwvfklzXIc5yzIqaN0c8=
github.com/instill-ai/usage-client v0.2.4-alpha.0.20230814155646-874e57a1e4b0 h1:9QoCxaktvqGJYGjN8KhkWsv1DVfwbt5G1d/Ycx1kJxo=
github.com/instill-ai/usage-client v0.2.4-alpha.0.20230814155646-874e57a1e4b0/go.mod h1:SELFgirs+28Wfnh0kGw02zttit4pUeKLKp17zGsTu6g=
github.com/instill-ai/x v0.3.0-alpha h1:z9fedROOG2dVHhswBfVwU/hzHuq8/JKSUON7inF+FH8=
github.com/instill-ai/x v0.3.0-alpha/go.mod h1:YVYjkbqc5FKatJ+jZa1S/8e4xHkQ8j9V3RYboqBpoR0=
github.com/intel/goresctrl v0.2.0/go.mod h1:+CZdzouYFn5EsxgqAQTEzMfwKwuc0fVdMrT9FCCAVRQ=
Expand Down
33 changes: 30 additions & 3 deletions pkg/usage/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/instill-ai/model-backend/config"
"github.com/instill-ai/model-backend/pkg/constant"
"github.com/instill-ai/model-backend/pkg/logger"
"github.com/instill-ai/model-backend/pkg/repository"
"github.com/instill-ai/model-backend/pkg/utils"
Expand Down Expand Up @@ -46,7 +47,14 @@ func NewUsage(ctx context.Context, r repository.Repository, u mgmtPB.MgmtPrivate
return nil
}

reporter, err := usageClient.InitReporter(ctx, usc, usagePB.Session_SERVICE_MODEL, config.Config.Server.Edition, version)
var defaultUserUID string
if resp, err := u.GetUserAdmin(ctx, &mgmtPB.GetUserAdminRequest{Name: constant.DefaultUserID}); err == nil {
defaultUserUID = resp.GetUser().GetUid()
} else {
logger.Error(err.Error())
}

reporter, err := usageClient.InitReporter(ctx, usc, usagePB.Session_SERVICE_MODEL, config.Config.Server.Edition, version, defaultUserUID)
if err != nil {
logger.Error(err.Error())
return nil
Expand Down Expand Up @@ -143,9 +151,18 @@ func (u *usage) StartReporter(ctx context.Context) {
}

logger, _ := logger.GetZapLogger(ctx)

var defaultUserUID string
if resp, err := u.mgmtPrivateServiceClient.GetUserAdmin(ctx, &mgmtPB.GetUserAdminRequest{Name: constant.DefaultUserID}); err == nil {
defaultUserUID = resp.GetUser().GetUid()
} else {
logger.Error(err.Error())
return
}

go func() {
time.Sleep(5 * time.Second)
err := usageClient.StartReporter(ctx, u.reporter, usagePB.Session_SERVICE_MODEL, config.Config.Server.Edition, u.version, u.RetrieveUsageData)
err := usageClient.StartReporter(ctx, u.reporter, usagePB.Session_SERVICE_MODEL, config.Config.Server.Edition, u.version, defaultUserUID, u.RetrieveUsageData)
if err != nil {
logger.Error(fmt.Sprintf("unable to start reporter: %v\n", err))
}
Expand All @@ -156,8 +173,18 @@ func (u *usage) TriggerSingleReporter(ctx context.Context) {
if u.reporter == nil {
return
}

logger, _ := logger.GetZapLogger(ctx)
err := usageClient.SingleReporter(ctx, u.reporter, usagePB.Session_SERVICE_MODEL, config.Config.Server.Edition, u.version, u.RetrieveUsageData())

var defaultUserUID string
if resp, err := u.mgmtPrivateServiceClient.GetUserAdmin(ctx, &mgmtPB.GetUserAdminRequest{Name: constant.DefaultUserID}); err == nil {
defaultUserUID = resp.GetUser().GetUid()
} else {
logger.Error(err.Error())
return
}

err := usageClient.SingleReporter(ctx, u.reporter, usagePB.Session_SERVICE_MODEL, config.Config.Server.Edition, u.version, defaultUserUID, u.RetrieveUsageData())
if err != nil {
logger.Fatal(err.Error())
}
Expand Down

0 comments on commit 10d2d72

Please sign in to comment.