Skip to content

Commit

Permalink
Merge pull request #69 from Mutezebra/prometheus
Browse files Browse the repository at this point in the history
Feature:Add prometheus to monitor the gateway and grpc server
  • Loading branch information
CocaineCong committed Feb 27, 2024
2 parents c179e10 + da97b8b commit 17afc7e
Show file tree
Hide file tree
Showing 29 changed files with 3,919 additions and 74 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ weights/
index/
venv/
.ruff_cache/
__pycache__
__pycache__
pkg/prometheus/config/*.json
10 changes: 8 additions & 2 deletions app/favorite/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"context"
"net"

"github.com/CocaineCong/tangseng/pkg/prometheus"
"github.com/CocaineCong/tangseng/pkg/tracing"

"github.com/pkg/errors"

"github.com/CocaineCong/tangseng/app/favorite/internal/service"
Expand Down Expand Up @@ -60,10 +60,16 @@ func main() {
}
}()
handler := otelgrpc.NewServerHandler()
server := grpc.NewServer(grpc.StatsHandler(handler))
server := grpc.NewServer(
grpc.StatsHandler(handler),
grpc.UnaryInterceptor(prometheus.UnaryServerInterceptor),
grpc.StreamInterceptor(prometheus.StreamServerInterceptor),
)

defer server.Stop()
// 绑定service
favoritePb.RegisterFavoritesServiceServer(server, service.GetFavoriteSrv())
prometheus.RegisterServer(server, config.Conf.Services[consts.FavoriteServiceName].Metrics[0], consts.FavoriteServiceName)
lis, err := net.Listen("tcp", grpcAddress)
if err != nil {
panic(err)
Expand Down
2 changes: 2 additions & 0 deletions app/gateway/routes/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/CocaineCong/tangseng/app/gateway/http"
"github.com/CocaineCong/tangseng/app/gateway/middleware"
"github.com/CocaineCong/tangseng/consts"
"github.com/CocaineCong/tangseng/pkg/prometheus"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
)

Expand All @@ -34,6 +35,7 @@ func NewRouter() *gin.Engine {
r.Use(middleware.Cors(), middleware.ErrorMiddleware(), otelgin.Middleware(consts.ServiceName))
store := cookie.NewStore([]byte("something-very-secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/metrics", prometheus.GatewayHandler())
v1 := r.Group("/api/v1")
{
v1.GET("ping", func(context *gin.Context) {
Expand Down
3 changes: 3 additions & 0 deletions app/gateway/rpc/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"log"
"time"

"github.com/CocaineCong/tangseng/pkg/prometheus"

"github.com/pkg/errors"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -68,6 +70,7 @@ func Init() {

// initClient 初始化所有的rpc客户端
func initClient(serviceName string, client interface{}) {
prometheus.EnableHandlingTimeHistogram()
conn, err := connectServer(serviceName)

if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion app/index_platform/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"net"

"github.com/CocaineCong/tangseng/pkg/prometheus"
"github.com/CocaineCong/tangseng/pkg/tracing"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"

Expand Down Expand Up @@ -69,10 +70,15 @@ func main() {
}
}()
handler := otelgrpc.NewServerHandler()
server := grpc.NewServer(grpc.StatsHandler(handler))
server := grpc.NewServer(
grpc.StatsHandler(handler),
grpc.UnaryInterceptor(prometheus.UnaryServerInterceptor),
grpc.StreamInterceptor(prometheus.StreamServerInterceptor),
)
defer server.Stop()

index_platform.RegisterIndexPlatformServiceServer(server, service.GetIndexPlatformSrv())
prometheus.RegisterServer(server, config.Conf.Services[consts.IndexPlatformName].Metrics[0], consts.IndexPlatformName)
lis, err := net.Listen("tcp", grpcAddress)
if err != nil {
panic(err)
Expand Down
7 changes: 6 additions & 1 deletion app/mapreduce/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/CocaineCong/tangseng/loading"
"github.com/CocaineCong/tangseng/pkg/discovery"
logs "github.com/CocaineCong/tangseng/pkg/logger"
"github.com/CocaineCong/tangseng/pkg/prometheus"
)

const (
Expand All @@ -51,10 +52,14 @@ func main() {
Name: config.Conf.Domain[MapreduceServerName].Name,
Addr: grpcAddress,
}
server := grpc.NewServer()
server := grpc.NewServer(
grpc.UnaryInterceptor(prometheus.UnaryServerInterceptor),
grpc.StreamInterceptor(prometheus.StreamServerInterceptor),
)
defer server.Stop()

mapreduce.RegisterMapReduceServiceServer(server, master.GetMapReduceSrv())
prometheus.RegisterServer(server, config.Conf.Services[MapreduceServerName].Metrics[0], MapreduceServerName)
lis, err := net.Listen("tcp", grpcAddress)
if err != nil {
panic(err)
Expand Down
8 changes: 7 additions & 1 deletion app/search_engine/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"net"

"github.com/CocaineCong/tangseng/pkg/prometheus"
"github.com/CocaineCong/tangseng/pkg/tracing"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"

Expand Down Expand Up @@ -70,10 +71,15 @@ func main() {
}
}()
handler := otelgrpc.NewServerHandler()
server := grpc.NewServer(grpc.StatsHandler(handler))
server := grpc.NewServer(
grpc.StatsHandler(handler),
grpc.UnaryInterceptor(prometheus.UnaryServerInterceptor),
grpc.StreamInterceptor(prometheus.StreamServerInterceptor),
)
defer server.Stop()
// 绑定service
pb.RegisterSearchEngineServiceServer(server, service.GetSearchEngineSrv())
prometheus.RegisterServer(server, config.Conf.Services[consts.SearchServiceName].Metrics[0], consts.SearchServiceName)
lis, err := net.Listen("tcp", grpcAddress)
if err != nil {
panic(err)
Expand Down
51 changes: 51 additions & 0 deletions app/search_vector/lshash/lshash.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,57 @@
# specific language governing permissions and limitations
# under the License.

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

from __future__ import print_function, unicode_literals, division, absolute_import
from builtins import int, round, str, object # noqa
# from future import standard_library
Expand Down
8 changes: 7 additions & 1 deletion app/user/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"net"

"github.com/CocaineCong/tangseng/pkg/prometheus"
"github.com/CocaineCong/tangseng/pkg/tracing"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"

Expand Down Expand Up @@ -61,10 +62,15 @@ func main() {
}
}()
handler := otelgrpc.NewServerHandler()
server := grpc.NewServer(grpc.StatsHandler(handler))
server := grpc.NewServer(
grpc.StatsHandler(handler),
grpc.UnaryInterceptor(prometheus.UnaryServerInterceptor),
grpc.StreamInterceptor(prometheus.StreamServerInterceptor),
)
defer server.Stop()
// 绑定service
pb.RegisterUserServiceServer(server, service.GetUserSrv())
prometheus.RegisterServer(server, config.Conf.Services[consts.UserServiceName].Metrics[0], consts.UserServiceName)
lis, err := net.Listen("tcp", grpcAddress)
if err != nil {
panic(err)
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type Server struct {
Port string `yaml:"port"`
Version string `yaml:"version"`
JwtSecret string `yaml:"jwtSecret"`
Metrics string `yaml:"metrics"`
}

type MySQL struct {
Expand Down Expand Up @@ -116,6 +117,7 @@ type Service struct {
Name string `yaml:"name"`
LoadBalance bool `yaml:"loadBalance"`
Addr []string `yaml:"addr"`
Metrics []string `yaml:"metrics"`
}

type Kafka struct {
Expand Down
15 changes: 15 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ server:
port: :4000
version: 1.0
jwtSecret: "38324-search-engine"
metrics: 127.0.0.1:4000

mysql:
driverName: mysql
Expand Down Expand Up @@ -58,42 +59,56 @@ services:
loadBalance: true
addr:
- 127.0.0.1:20001
metrics:
- 127.0.0.1:30001

user:
name: user
loadBalance: false
addr:
- 127.0.0.1:20002
metrics:
- 127.0.0.1:30002

favorite:
name: favorite
loadBalance: false
addr:
- 127.0.0.1:20003
metrics:
- 127.0.0.1:30003

search_engine:
name: search_engine
loadBalance: false
addr:
- 127.0.0.1:20004
metrics:
- 127.0.0.1:30004

index_platform:
name: index_platform
loadBalance: false
addr:
- 127.0.0.1:20005
metrics:
- 127.0.0.1:30005

mapreduce:
name: mapreduce
loadBalance: false
addr:
- 127.0.0.1:20006
metrics:
- 127.0.0.1:30006

search_vector:
name: search_vector
loadBalance: false
addr:
- 127.0.0.1:20007
metrics:
- 127.0.0.1:30007

starrocks:
username: root
Expand Down
17 changes: 15 additions & 2 deletions config/config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ server:
port: :4000
version: 1.0
jwtSecret: "38324-search-engine"
metrics: 127.0.0.1:4000

mysql:
driverName: mysql
Expand Down Expand Up @@ -41,42 +42,56 @@ services:
loadBalance: true
addr:
- 127.0.0.1:20001
metrics:
- 127.0.0.1:30001

user:
name: user
loadBalance: false
addr:
- 127.0.0.1:20002
metrics:
- 127.0.0.1:30002

favorite:
name: favorite
loadBalance: false
addr:
- 127.0.0.1:20003
metrics:
- 127.0.0.1:30003

search_engine:
name: search_engine
loadBalance: false
addr:
- 127.0.0.1:20004
metrics:
- 127.0.0.1:30004

index_platform:
name: index_platform
loadBalance: false
addr:
- 127.0.0.1:20005
metrics:
- 127.0.0.1:30005

mapreduce:
name: mapreduce
loadBalance: false
addr:
- 127.0.0.1:20006
metrics:
- 127.0.0.1:30006

search_vector:
name: search_vector
loadBalance: false
addr:
- 127.0.0.1:20007
metrics:
- 127.0.0.1:30007

starrocks:
username: root
Expand Down Expand Up @@ -118,7 +133,5 @@ milvus:
default_milvus_table_name: milvus_table_name
metric_type: L2
timeout: 3

jaeger:
addr: 127.0.0.1:4317

Loading

0 comments on commit 17afc7e

Please sign in to comment.