-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
821 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// 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 http | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/openimsdk/open-im-server/v3/pkg/callbackstruct" | ||
"github.com/openimsdk/open-im-server/v3/pkg/common/config" | ||
) | ||
|
||
func TestGet(t *testing.T) { | ||
type args struct { | ||
url string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantResponse []byte | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotResponse, err := Get(tt.args.url) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(gotResponse, tt.wantResponse) { | ||
t.Errorf("Get() = %v, want %v", gotResponse, tt.wantResponse) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestPost(t *testing.T) { | ||
type args struct { | ||
ctx context.Context | ||
url string | ||
header map[string]string | ||
data interface{} | ||
timeout int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantContent []byte | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotContent, err := Post(tt.args.ctx, tt.args.url, tt.args.header, tt.args.data, tt.args.timeout) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Post() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(gotContent, tt.wantContent) { | ||
t.Errorf("Post() = %v, want %v", gotContent, tt.wantContent) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestPostReturn(t *testing.T) { | ||
type args struct { | ||
ctx context.Context | ||
url string | ||
header map[string]string | ||
input interface{} | ||
output interface{} | ||
timeOutSecond int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := PostReturn(tt.args.ctx, tt.args.url, tt.args.header, tt.args.input, tt.args.output, tt.args.timeOutSecond); (err != nil) != tt.wantErr { | ||
t.Errorf("PostReturn() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_callBackPostReturn(t *testing.T) { | ||
type args struct { | ||
ctx context.Context | ||
url string | ||
command string | ||
input interface{} | ||
output callbackstruct.CallbackResp | ||
callbackConfig config.CallBackConfig | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := callBackPostReturn(tt.args.ctx, tt.args.url, tt.args.command, tt.args.input, tt.args.output, tt.args.callbackConfig); (err != nil) != tt.wantErr { | ||
t.Errorf("callBackPostReturn() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCallBackPostReturn(t *testing.T) { | ||
type args struct { | ||
ctx context.Context | ||
url string | ||
req callbackstruct.CallbackReq | ||
resp callbackstruct.CallbackResp | ||
callbackConfig config.CallBackConfig | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := CallBackPostReturn(tt.args.ctx, tt.args.url, tt.args.req, tt.args.resp, tt.args.callbackConfig); (err != nil) != tt.wantErr { | ||
t.Errorf("CallBackPostReturn() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package prommetrics | ||
|
||
import ginProm "github.com/openimsdk/open-im-server/v3/pkg/common/ginprometheus" | ||
|
||
/* | ||
labels := prometheus.Labels{"label_one": "any", "label_two": "value"} | ||
ApiCustomCnt.MetricCollector.(*prometheus.CounterVec).With(labels).Inc() | ||
*/ | ||
var ( | ||
ApiCustomCnt = &ginProm.Metric{ | ||
Name: "custom_total", | ||
Description: "Custom counter events.", | ||
Type: "counter_vec", | ||
Args: []string{"label_one", "label_two"}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
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", | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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", | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
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", | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package prommetrics | ||
|
||
import ( | ||
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" | ||
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" | ||
"github.com/openimsdk/open-im-server/v3/pkg/common/ginprometheus" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/collectors" | ||
) | ||
|
||
func NewGrpcPromObj(cusMetrics []prometheus.Collector) (*prometheus.Registry, *grpc_prometheus.ServerMetrics, error) { | ||
//////////////////////////////////////////////////////// | ||
reg := prometheus.NewRegistry() | ||
grpcMetrics := grpc_prometheus.NewServerMetrics() | ||
grpcMetrics.EnableHandlingTimeHistogram() | ||
cusMetrics = append(cusMetrics, grpcMetrics, collectors.NewGoCollector()) | ||
reg.MustRegister(cusMetrics...) | ||
return reg, grpcMetrics, nil | ||
} | ||
|
||
func GetGrpcCusMetrics(registerName string) []prometheus.Collector { | ||
switch registerName { | ||
case config2.Config.RpcRegisterName.OpenImMessageGatewayName: | ||
return []prometheus.Collector{OnlineUserGauge} | ||
case config2.Config.RpcRegisterName.OpenImMsgName: | ||
return []prometheus.Collector{SingleChatMsgProcessSuccessCounter, SingleChatMsgProcessFailedCounter, GroupChatMsgProcessSuccessCounter, GroupChatMsgProcessFailedCounter} | ||
case "Transfer": | ||
return []prometheus.Collector{MsgInsertRedisSuccessCounter, MsgInsertRedisFailedCounter, MsgInsertMongoSuccessCounter, MsgInsertMongoFailedCounter, SeqSetFailedCounter} | ||
case config2.Config.RpcRegisterName.OpenImPushName: | ||
return []prometheus.Collector{MsgOfflinePushFailedCounter} | ||
case config2.Config.RpcRegisterName.OpenImAuthName: | ||
return []prometheus.Collector{UserLoginCounter} | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func GetGinCusMetrics(name string) []*ginprometheus.Metric { | ||
switch name { | ||
case "Api": | ||
return []*ginprometheus.Metric{ApiCustomCnt} | ||
default: | ||
return []*ginprometheus.Metric{ApiCustomCnt} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package prommetrics | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" | ||
"github.com/openimsdk/open-im-server/v3/pkg/common/ginprometheus" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
func TestNewGrpcPromObj(t *testing.T) { | ||
type args struct { | ||
cusMetrics []prometheus.Collector | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *prometheus.Registry | ||
want1 *grpc_prometheus.ServerMetrics | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, got1, err := NewGrpcPromObj(tt.args.cusMetrics) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("NewGrpcPromObj() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("NewGrpcPromObj() got = %v, want %v", got, tt.want) | ||
} | ||
if !reflect.DeepEqual(got1, tt.want1) { | ||
t.Errorf("NewGrpcPromObj() got1 = %v, want %v", got1, tt.want1) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetGrpcCusMetrics(t *testing.T) { | ||
type args struct { | ||
registerName string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []prometheus.Collector | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := GetGrpcCusMetrics(tt.args.registerName); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetGrpcCusMetrics() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetGinCusMetrics(t *testing.T) { | ||
type args struct { | ||
name string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []*ginprometheus.Metric | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := GetGinCusMetrics(tt.args.name); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetGinCusMetrics() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 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 startrpc | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/OpenIMSDK/tools/discoveryregistry" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func TestStart(t *testing.T) { | ||
type args struct { | ||
rpcPort int | ||
rpcRegisterName string | ||
prometheusPort int | ||
rpcFn func(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error | ||
options []grpc.ServerOption | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := Start(tt.args.rpcPort, tt.args.rpcRegisterName, tt.args.prometheusPort, tt.args.rpcFn, tt.args.options...); (err != nil) != tt.wantErr { | ||
t.Errorf("Start() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.