复制项目
This commit is contained in:
48
pkg/common/prommetrics/api.go
Normal file
48
pkg/common/prommetrics/api.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package prommetrics
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
var (
|
||||
apiCounter = prometheus.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Name: "api_count",
|
||||
Help: "Total number of API calls",
|
||||
},
|
||||
[]string{"path", "method", "code"},
|
||||
)
|
||||
httpCounter = prometheus.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Name: "http_count",
|
||||
Help: "Total number of HTTP calls",
|
||||
},
|
||||
[]string{"path", "method", "status"},
|
||||
)
|
||||
)
|
||||
|
||||
func RegistryApi() {
|
||||
registry.MustRegister(apiCounter, httpCounter)
|
||||
}
|
||||
|
||||
func ApiInit(listener net.Listener) error {
|
||||
apiRegistry := prometheus.NewRegistry()
|
||||
cs := append(
|
||||
baseCollector,
|
||||
apiCounter,
|
||||
httpCounter,
|
||||
)
|
||||
return Init(apiRegistry, listener, commonPath, promhttp.HandlerFor(apiRegistry, promhttp.HandlerOpts{}), cs...)
|
||||
}
|
||||
|
||||
func APICall(path string, method string, apiCode int) {
|
||||
apiCounter.With(prometheus.Labels{"path": path, "method": method, "code": strconv.Itoa(apiCode)}).Inc()
|
||||
}
|
||||
|
||||
func HttpCall(path string, method string, status int) {
|
||||
httpCounter.With(prometheus.Labels{"path": path, "method": method, "status": strconv.Itoa(status)}).Inc()
|
||||
}
|
||||
30
pkg/common/prommetrics/grpc_auth.go
Normal file
30
pkg/common/prommetrics/grpc_auth.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// 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 prommetrics
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var (
|
||||
UserLoginCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "user_login_total",
|
||||
Help: "The number of user login",
|
||||
})
|
||||
)
|
||||
|
||||
func RegistryAuth() {
|
||||
registry.MustRegister(UserLoginCounter)
|
||||
}
|
||||
47
pkg/common/prommetrics/grpc_msg.go
Normal file
47
pkg/common/prommetrics/grpc_msg.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// 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 prommetrics
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var (
|
||||
SingleChatMsgProcessSuccessCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "single_chat_msg_process_success_total",
|
||||
Help: "The number of single chat msg successful processed",
|
||||
})
|
||||
SingleChatMsgProcessFailedCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "single_chat_msg_process_failed_total",
|
||||
Help: "The number of single chat msg failed processed",
|
||||
})
|
||||
GroupChatMsgProcessSuccessCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "group_chat_msg_process_success_total",
|
||||
Help: "The number of group chat msg successful processed",
|
||||
})
|
||||
GroupChatMsgProcessFailedCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "group_chat_msg_process_failed_total",
|
||||
Help: "The number of group chat msg failed processed",
|
||||
})
|
||||
)
|
||||
|
||||
func RegistryMsg() {
|
||||
registry.MustRegister(
|
||||
SingleChatMsgProcessSuccessCounter,
|
||||
SingleChatMsgProcessFailedCounter,
|
||||
GroupChatMsgProcessSuccessCounter,
|
||||
GroupChatMsgProcessFailedCounter,
|
||||
)
|
||||
}
|
||||
30
pkg/common/prommetrics/grpc_msggateway.go
Normal file
30
pkg/common/prommetrics/grpc_msggateway.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// 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 prommetrics
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var (
|
||||
OnlineUserGauge = prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "online_user_num",
|
||||
Help: "The number of online user num",
|
||||
})
|
||||
)
|
||||
|
||||
func RegistryMsgGateway() {
|
||||
registry.MustRegister(OnlineUserGauge)
|
||||
}
|
||||
37
pkg/common/prommetrics/grpc_push.go
Normal file
37
pkg/common/prommetrics/grpc_push.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// 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 prommetrics
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var (
|
||||
MsgOfflinePushFailedCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "msg_offline_push_failed_total",
|
||||
Help: "The number of msg failed offline pushed",
|
||||
})
|
||||
MsgLoneTimePushCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "msg_long_time_push_total",
|
||||
Help: "The number of messages with a push time exceeding 10 seconds",
|
||||
})
|
||||
)
|
||||
|
||||
func RegistryPush() {
|
||||
registry.MustRegister(
|
||||
MsgOfflinePushFailedCounter,
|
||||
MsgLoneTimePushCounter,
|
||||
)
|
||||
}
|
||||
14
pkg/common/prommetrics/grpc_user.go
Normal file
14
pkg/common/prommetrics/grpc_user.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package prommetrics
|
||||
|
||||
import "github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
var (
|
||||
UserRegisterCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "user_register_total",
|
||||
Help: "The number of user login",
|
||||
})
|
||||
)
|
||||
|
||||
func RegistryUser() {
|
||||
registry.MustRegister(UserRegisterCounter)
|
||||
}
|
||||
117
pkg/common/prommetrics/prommetrics.go
Normal file
117
pkg/common/prommetrics/prommetrics.go
Normal file
@@ -0,0 +1,117 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// 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 prommetrics
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
const commonPath = "/metrics"
|
||||
|
||||
var registry = &prometheusRegistry{prometheus.NewRegistry()}
|
||||
|
||||
type prometheusRegistry struct {
|
||||
*prometheus.Registry
|
||||
}
|
||||
|
||||
func (x *prometheusRegistry) MustRegister(cs ...prometheus.Collector) {
|
||||
for _, c := range cs {
|
||||
if err := x.Registry.Register(c); err != nil {
|
||||
if errors.As(err, &prometheus.AlreadyRegisteredError{}) {
|
||||
continue
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
registry.MustRegister(
|
||||
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
|
||||
collectors.NewGoCollector(),
|
||||
)
|
||||
}
|
||||
|
||||
var (
|
||||
baseCollector = []prometheus.Collector{
|
||||
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
|
||||
collectors.NewGoCollector(),
|
||||
}
|
||||
)
|
||||
|
||||
func Init(registry *prometheus.Registry, listener net.Listener, path string, handler http.Handler, cs ...prometheus.Collector) error {
|
||||
registry.MustRegister(cs...)
|
||||
srv := http.NewServeMux()
|
||||
srv.Handle(path, handler)
|
||||
return http.Serve(listener, srv)
|
||||
}
|
||||
|
||||
func RegistryAll() {
|
||||
RegistryApi()
|
||||
RegistryAuth()
|
||||
RegistryMsg()
|
||||
RegistryMsgGateway()
|
||||
RegistryPush()
|
||||
RegistryUser()
|
||||
RegistryRpc()
|
||||
RegistryTransfer()
|
||||
}
|
||||
|
||||
func Start(listener net.Listener) error {
|
||||
srv := http.NewServeMux()
|
||||
srv.Handle(commonPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
|
||||
return http.Serve(listener, srv)
|
||||
}
|
||||
|
||||
const (
|
||||
APIKeyName = "api"
|
||||
MessageTransferKeyName = "message-transfer"
|
||||
|
||||
TTL = 300
|
||||
)
|
||||
|
||||
type Target struct {
|
||||
Target string `json:"target"`
|
||||
Labels map[string]string `json:"labels"`
|
||||
}
|
||||
|
||||
type RespTarget struct {
|
||||
Targets []string `json:"targets"`
|
||||
Labels map[string]string `json:"labels"`
|
||||
}
|
||||
|
||||
func BuildDiscoveryKeyPrefix(name string) string {
|
||||
return fmt.Sprintf("%s/%s/%s", "openim", "prometheus_discovery", name)
|
||||
}
|
||||
|
||||
func BuildDiscoveryKey(name string, index int) string {
|
||||
return fmt.Sprintf("%s/%s/%s/%d", "openim", "prometheus_discovery", name, index)
|
||||
}
|
||||
|
||||
func BuildDefaultTarget(host string, ip int) Target {
|
||||
return Target{
|
||||
Target: fmt.Sprintf("%s:%d", host, ip),
|
||||
Labels: map[string]string{
|
||||
"namespace": "default",
|
||||
},
|
||||
}
|
||||
}
|
||||
77
pkg/common/prommetrics/prommetrics_test.go
Normal file
77
pkg/common/prommetrics/prommetrics_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// 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 prommetrics
|
||||
|
||||
import "testing"
|
||||
|
||||
//func TestNewGrpcPromObj(t *testing.T) {
|
||||
// // Create a custom metric to pass into the NewGrpcPromObj function.
|
||||
// customMetric := prometheus.NewCounter(prometheus.CounterOpts{
|
||||
// Name: "test_metric",
|
||||
// Help: "This is a test metric.",
|
||||
// })
|
||||
// cusMetrics := []prometheus.Collector{customMetric}
|
||||
//
|
||||
// // Call NewGrpcPromObj with the custom metrics.
|
||||
// reg, grpcMetrics, err := NewGrpcPromObj(cusMetrics)
|
||||
//
|
||||
// // Assert no error was returned.
|
||||
// assert.NoError(t, err)
|
||||
//
|
||||
// // Assert the registry was correctly initialized.
|
||||
// assert.NotNil(t, reg)
|
||||
//
|
||||
// // Assert the grpcMetrics was correctly initialized.
|
||||
// assert.NotNil(t, grpcMetrics)
|
||||
//
|
||||
// // Assert that the custom metric is registered.
|
||||
// mfs, err := reg.Gather()
|
||||
// assert.NoError(t, err)
|
||||
// assert.NotEmpty(t, mfs) // Ensure some metrics are present.
|
||||
// found := false
|
||||
// for _, mf := range mfs {
|
||||
// if *mf.Name == "test_metric" {
|
||||
// found = true
|
||||
// break
|
||||
// }
|
||||
// }
|
||||
// assert.True(t, found, "Custom metric not found in registry")
|
||||
//}
|
||||
|
||||
//func TestGetGrpcCusMetrics(t *testing.T) {
|
||||
// conf := config2.NewGlobalConfig()
|
||||
//
|
||||
// config2.InitConfig(conf, "../../config")
|
||||
// // Test various cases based on the switch statement in the GetGrpcCusMetrics function.
|
||||
// testCases := []struct {
|
||||
// name string
|
||||
// expected int // The expected number of metrics for each case.
|
||||
// }{
|
||||
// {conf.RpcRegisterName.OpenImMessageGatewayName, 1},
|
||||
// }
|
||||
//
|
||||
// for _, tc := range testCases {
|
||||
// t.Run(tc.name, func(t *testing.T) {
|
||||
// metrics := GetGrpcCusMetrics(tc.name, &conf.RpcRegisterName)
|
||||
// assert.Len(t, metrics, tc.expected)
|
||||
// })
|
||||
// }
|
||||
//}
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
RegistryApi()
|
||||
RegistryApi()
|
||||
|
||||
}
|
||||
74
pkg/common/prommetrics/rpc.go
Normal file
74
pkg/common/prommetrics/rpc.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package prommetrics
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"git.imall.cloud/openim/open-im-server-deploy/pkg/common/config"
|
||||
gp "github.com/grpc-ecosystem/go-grpc-prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
const rpcPath = commonPath
|
||||
|
||||
var (
|
||||
grpcMetrics *gp.ServerMetrics
|
||||
rpcCounter = prometheus.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Name: "rpc_count",
|
||||
Help: "Total number of RPC calls",
|
||||
},
|
||||
[]string{"name", "path", "code"},
|
||||
)
|
||||
)
|
||||
|
||||
func RegistryRpc() {
|
||||
registry.MustRegister(rpcCounter)
|
||||
}
|
||||
|
||||
func RpcInit(cs []prometheus.Collector, listener net.Listener) error {
|
||||
reg := prometheus.NewRegistry()
|
||||
cs = append(append(
|
||||
baseCollector,
|
||||
rpcCounter,
|
||||
), cs...)
|
||||
return Init(reg, listener, rpcPath, promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}), cs...)
|
||||
}
|
||||
|
||||
func RPCCall(name string, path string, code int) {
|
||||
rpcCounter.With(prometheus.Labels{"name": name, "path": path, "code": strconv.Itoa(code)}).Inc()
|
||||
}
|
||||
|
||||
func GetGrpcServerMetrics() *gp.ServerMetrics {
|
||||
if grpcMetrics == nil {
|
||||
grpcMetrics = gp.NewServerMetrics()
|
||||
grpcMetrics.EnableHandlingTimeHistogram()
|
||||
}
|
||||
return grpcMetrics
|
||||
}
|
||||
|
||||
func GetGrpcCusMetrics(registerName string, discovery *config.Discovery) []prometheus.Collector {
|
||||
switch registerName {
|
||||
case discovery.RpcService.MessageGateway:
|
||||
return []prometheus.Collector{OnlineUserGauge}
|
||||
case discovery.RpcService.Msg:
|
||||
return []prometheus.Collector{
|
||||
SingleChatMsgProcessSuccessCounter,
|
||||
SingleChatMsgProcessFailedCounter,
|
||||
GroupChatMsgProcessSuccessCounter,
|
||||
GroupChatMsgProcessFailedCounter,
|
||||
}
|
||||
case discovery.RpcService.Push:
|
||||
return []prometheus.Collector{
|
||||
MsgOfflinePushFailedCounter,
|
||||
MsgLoneTimePushCounter,
|
||||
}
|
||||
case discovery.RpcService.Auth:
|
||||
return []prometheus.Collector{UserLoginCounter}
|
||||
case discovery.RpcService.User:
|
||||
return []prometheus.Collector{UserRegisterCounter}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
68
pkg/common/prommetrics/transfer.go
Normal file
68
pkg/common/prommetrics/transfer.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// 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 prommetrics
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
var (
|
||||
MsgInsertRedisSuccessCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "msg_insert_redis_success_total",
|
||||
Help: "The number of successful insert msg to redis",
|
||||
})
|
||||
MsgInsertRedisFailedCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "msg_insert_redis_failed_total",
|
||||
Help: "The number of failed insert msg to redis",
|
||||
})
|
||||
MsgInsertMongoSuccessCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "msg_insert_mongo_success_total",
|
||||
Help: "The number of successful insert msg to mongo",
|
||||
})
|
||||
MsgInsertMongoFailedCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "msg_insert_mongo_failed_total",
|
||||
Help: "The number of failed insert msg to mongo",
|
||||
})
|
||||
SeqSetFailedCounter = prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Name: "seq_set_failed_total",
|
||||
Help: "The number of failed set seq",
|
||||
})
|
||||
)
|
||||
|
||||
func RegistryTransfer() {
|
||||
registry.MustRegister(
|
||||
MsgInsertRedisSuccessCounter,
|
||||
MsgInsertRedisFailedCounter,
|
||||
MsgInsertMongoSuccessCounter,
|
||||
MsgInsertMongoFailedCounter,
|
||||
SeqSetFailedCounter,
|
||||
)
|
||||
}
|
||||
|
||||
func TransferInit(listener net.Listener) error {
|
||||
reg := prometheus.NewRegistry()
|
||||
cs := append(
|
||||
baseCollector,
|
||||
MsgInsertRedisSuccessCounter,
|
||||
MsgInsertRedisFailedCounter,
|
||||
MsgInsertMongoSuccessCounter,
|
||||
MsgInsertMongoFailedCounter,
|
||||
SeqSetFailedCounter,
|
||||
)
|
||||
return Init(reg, listener, commonPath, promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}), cs...)
|
||||
}
|
||||
Reference in New Issue
Block a user