Skip to content

Commit

Permalink
refactor:statis log add traffic direction info (#1232)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun authored Sep 15, 2023
1 parent 4df963a commit e7f2bfc
Show file tree
Hide file tree
Showing 128 changed files with 5,029 additions and 5,069 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: "1.20"
go-version:
# Checkout latest code
- name: Checkout repo
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ README:

**功能**

- 服务管理:服务发现、服务发现、健康检查
- 服务管理:服务注册、服务发现、健康检查
- 流量控制:可自定义的流量路由、负载均衡、限频限流、访问控制
- 故障容错:服务和接口熔断和降级、实例熔断和切换
- 配置管理:版本管理、灰度发布、动态更新
Expand Down
7 changes: 4 additions & 3 deletions apiserver/grpcserver/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,17 @@ func TestGetClientOpenMethod(t *testing.T) {
wantErr: false,
},
{
name: "case=1",
name: "case=2",
args: args{
include: []string{
apiserver.DiscoverAccess,
},
protocol: "grpc",
},
want: map[string]bool{
"/v1.PolarisGRPC/Discover": true,
"/v1.PolarisGRPC/ReportClient": true,
"/v1.PolarisGRPC/Discover": true,
"/v1.PolarisGRPC/ReportClient": true,
"/v1.PolarisServiceContractGRPC/ReportServiceContract": true,
},
wantErr: false,
},
Expand Down
33 changes: 31 additions & 2 deletions apiserver/grpcserver/config/client_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,34 @@ package config

import (
"context"
"fmt"

apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"

"github.com/polarismesh/polaris/apiserver/grpcserver"
"github.com/polarismesh/polaris/common/metrics"
commontime "github.com/polarismesh/polaris/common/time"
"github.com/polarismesh/polaris/common/utils"
"github.com/polarismesh/polaris/plugin"
)

// GetConfigFile 拉取配置
func (g *ConfigGRPCServer) GetConfigFile(ctx context.Context,
configFile *apiconfig.ClientConfigFileInfo) (*apiconfig.ConfigClientResponse, error) {
req *apiconfig.ClientConfigFileInfo) (*apiconfig.ConfigClientResponse, error) {
ctx = grpcserver.ConvertContext(ctx)
response := g.configServer.GetConfigFileForClient(ctx, configFile)

startTime := commontime.CurrentMillisecond()
defer func() {
plugin.GetStatis().ReportDiscoverCall(metrics.ClientDiscoverMetric{
ClientIP: utils.ParseClientAddress(ctx),
Namespace: req.GetNamespace().GetValue(),
Resource: fmt.Sprintf("CONFIG_FILE:%s|%s|%d", req.GetGroup().GetValue(),
req.GetFileName().GetValue(), req.GetVersion().GetValue()),
Timestamp: startTime,
CostTime: commontime.CurrentMillisecond() - startTime,
})
}()
response := g.configServer.GetConfigFileForClient(ctx, req)
return response, nil
}

Expand Down Expand Up @@ -73,6 +90,18 @@ func (g *ConfigGRPCServer) WatchConfigFiles(ctx context.Context,
func (g *ConfigGRPCServer) GetConfigFileMetadataList(ctx context.Context,
req *apiconfig.ConfigFileGroupRequest) (*apiconfig.ConfigClientListResponse, error) {

startTime := commontime.CurrentMillisecond()
defer func() {
plugin.GetStatis().ReportDiscoverCall(metrics.ClientDiscoverMetric{
ClientIP: utils.ParseClientAddress(ctx),
Namespace: req.GetConfigFileGroup().GetNamespace().GetValue(),
Resource: fmt.Sprintf("CONFIG_FILE_LIST:%s|%s", req.GetConfigFileGroup().GetName().GetValue(),
req.GetRevision().GetValue()),
Timestamp: startTime,
CostTime: commontime.CurrentMillisecond() - startTime,
})
}()

ctx = grpcserver.ConvertContext(ctx)
return g.configServer.GetConfigFileNamesWithCache(ctx, req), nil
}
20 changes: 17 additions & 3 deletions apiserver/grpcserver/discover/v1/client_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ import (
"github.com/polarismesh/polaris/apiserver/grpcserver"
api "github.com/polarismesh/polaris/common/api/v1"
commonlog "github.com/polarismesh/polaris/common/log"
"github.com/polarismesh/polaris/common/metrics"
commontime "github.com/polarismesh/polaris/common/time"
"github.com/polarismesh/polaris/common/utils"
"github.com/polarismesh/polaris/plugin"
)

var (
namingLog = commonlog.GetScopeOrDefaultByName(commonlog.NamingLoggerName)
accesslog = commonlog.GetScopeOrDefaultByName(commonlog.APIServerLoggerName)
)

// ReportClient 客户端上报
Expand Down Expand Up @@ -101,7 +104,7 @@ func (g *DiscoverServer) Discover(server apiservice.PolarisGRPC_DiscoverServer)
}

msg := fmt.Sprintf("receive grpc discover request: %s", in.Service.String())
namingLog.Info(msg,
accesslog.Info(msg,
zap.String("type", apiservice.DiscoverRequest_DiscoverRequestType_name[int32(in.Type)]),
zap.String("client-address", clientAddress),
zap.String("user-agent", userAgent),
Expand All @@ -126,10 +129,21 @@ func (g *DiscoverServer) Discover(server apiservice.PolarisGRPC_DiscoverServer)
continue
}

startTime := commontime.CurrentMillisecond()
defer func() {
plugin.GetStatis().ReportDiscoverCall(metrics.ClientDiscoverMetric{
ClientIP: utils.ParseClientAddress(ctx),
Namespace: in.GetService().GetNamespace().GetValue(),
Resource: in.Type.String() + ":" + in.GetService().GetName().GetValue(),
Timestamp: startTime,
CostTime: commontime.CurrentMillisecond() - startTime,
})
}()

var out *apiservice.DiscoverResponse
switch in.Type {
case apiservice.DiscoverRequest_INSTANCE:
out = g.namingServer.ServiceInstancesCache(ctx, in.Service)
out = g.namingServer.ServiceInstancesCache(ctx, &apiservice.DiscoverFilter{}, in.Service)
case apiservice.DiscoverRequest_ROUTING:
out = g.namingServer.GetRoutingConfigWithCache(ctx, in.Service)
case apiservice.DiscoverRequest_RATE_LIMIT:
Expand Down
5 changes: 4 additions & 1 deletion apiserver/grpcserver/utils/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// GetClientOpenMethod 获取客户端openMethod
func GetClientOpenMethod(include []string, protocol string) (map[string]bool, error) {
clientAccess := make(map[string][]string)
clientAccess[apiserver.DiscoverAccess] = []string{"Discover", "ReportClient"}
clientAccess[apiserver.DiscoverAccess] = []string{"Discover", "ReportClient", "ReportServiceContract"}
clientAccess[apiserver.RegisterAccess] = []string{"RegisterInstance", "DeregisterInstance"}
clientAccess[apiserver.HealthcheckAccess] = []string{"Heartbeat", "BatchHeartbeat", "BatchGetHeartbeat", "BatchDelHeartbeat"}

Expand All @@ -49,6 +49,9 @@ func GetClientOpenMethod(include []string, protocol string) (map[string]bool, er
if item == apiserver.HealthcheckAccess && method != "Heartbeat" {
recordMethod = "/v1.PolarisHeartbeat" + strings.ToUpper(protocol) + "/" + method
}
if method == "ReportServiceContract" {
recordMethod = "/v1.PolarisServiceContract" + strings.ToUpper(protocol) + "/" + method
}
openMethod[recordMethod] = true
}
} else {
Expand Down
33 changes: 32 additions & 1 deletion apiserver/httpserver/config/client_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package config

import (
"fmt"
"strconv"

"github.com/emicklei/go-restful/v3"
Expand All @@ -27,6 +28,10 @@ import (

httpcommon "github.com/polarismesh/polaris/apiserver/httpserver/utils"
api "github.com/polarismesh/polaris/common/api/v1"
"github.com/polarismesh/polaris/common/metrics"
commontime "github.com/polarismesh/polaris/common/time"
"github.com/polarismesh/polaris/common/utils"
"github.com/polarismesh/polaris/plugin"
)

func (h *HTTPServer) ClientGetConfigFile(req *restful.Request, rsp *restful.Response) {
Expand All @@ -43,7 +48,20 @@ func (h *HTTPServer) ClientGetConfigFile(req *restful.Request, rsp *restful.Resp
Version: &wrapperspb.UInt64Value{Value: version},
}

response := h.configServer.GetConfigFileForClient(handler.ParseHeaderContext(), configFile)
ctx := handler.ParseHeaderContext()
startTime := commontime.CurrentMillisecond()
defer func() {
plugin.GetStatis().ReportDiscoverCall(metrics.ClientDiscoverMetric{
ClientIP: utils.ParseClientAddress(ctx),
Namespace: configFile.GetNamespace().GetValue(),
Resource: fmt.Sprintf("CONFIG_FILE:%s|%s|%d", configFile.GetGroup().GetValue(),
configFile.GetFileName().GetValue(), version),
Timestamp: startTime,
CostTime: commontime.CurrentMillisecond() - startTime,
})
}()

response := h.configServer.GetConfigFileForClient(ctx, configFile)
handler.WriteHeaderAndProto(response)
}

Expand Down Expand Up @@ -82,6 +100,19 @@ func (h *HTTPServer) GetConfigFileMetadataList(req *restful.Request, rsp *restfu
handler.WriteHeaderAndProto(api.NewResponseWithMsg(apimodel.Code_ParseException, err.Error()))
return
}

startTime := commontime.CurrentMillisecond()
defer func() {
plugin.GetStatis().ReportDiscoverCall(metrics.ClientDiscoverMetric{
ClientIP: utils.ParseClientAddress(ctx),
Namespace: in.GetConfigFileGroup().GetNamespace().GetValue(),
Resource: fmt.Sprintf("CONFIG_FILE_LIST:%s|%s", in.GetConfigFileGroup().GetName().GetValue(),
in.GetRevision().GetValue()),
Timestamp: startTime,
CostTime: commontime.CurrentMillisecond() - startTime,
})
}()

out := h.configServer.GetConfigFileNamesWithCache(ctx, in)
handler.WriteHeaderAndProto(out)
}
24 changes: 14 additions & 10 deletions apiserver/httpserver/discover/v1/client_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ package v1

import (
"context"
"fmt"

"github.com/emicklei/go-restful/v3"
apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
"go.uber.org/zap"

httpcommon "github.com/polarismesh/polaris/apiserver/httpserver/utils"
api "github.com/polarismesh/polaris/common/api/v1"
"github.com/polarismesh/polaris/common/metrics"
commontime "github.com/polarismesh/polaris/common/time"
"github.com/polarismesh/polaris/common/utils"
"github.com/polarismesh/polaris/plugin"
)

// ReportClient 客户端上报信息
Expand Down Expand Up @@ -102,18 +103,21 @@ func (h *HTTPServerV1) Discover(req *restful.Request, rsp *restful.Response) {
return
}

msg := fmt.Sprintf("receive http discover request: %s", discoverRequest.Service.String())
namingLog.Info(msg,
zap.String("type", apiservice.DiscoverRequest_DiscoverRequestType_name[int32(discoverRequest.Type)]),
zap.String("client-address", req.Request.RemoteAddr),
zap.String("user-agent", req.HeaderParameter("User-Agent")),
utils.ZapRequestID(req.HeaderParameter("Request-Id")),
)
startTime := commontime.CurrentMillisecond()
defer func() {
plugin.GetStatis().ReportDiscoverCall(metrics.ClientDiscoverMetric{
ClientIP: utils.ParseClientAddress(ctx),
Namespace: discoverRequest.GetService().GetNamespace().GetValue(),
Resource: discoverRequest.Type.String() + ":" + discoverRequest.GetService().GetName().GetValue(),
Timestamp: startTime,
CostTime: commontime.CurrentMillisecond() - startTime,
})
}()

var ret *apiservice.DiscoverResponse
switch discoverRequest.Type {
case apiservice.DiscoverRequest_INSTANCE:
ret = h.namingServer.ServiceInstancesCache(ctx, discoverRequest.Service)
ret = h.namingServer.ServiceInstancesCache(ctx, discoverRequest.Filter, discoverRequest.Service)
case apiservice.DiscoverRequest_ROUTING:
ret = h.namingServer.GetRoutingConfigWithCache(ctx, discoverRequest.Service)
case apiservice.DiscoverRequest_RATE_LIMIT:
Expand Down
Loading

0 comments on commit e7f2bfc

Please sign in to comment.