parent
e3ee24df31
commit
0a9b53bbe3
@ -1,57 +0,0 @@
|
||||
package prommetrics
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
apiCallCounter1 = prometheus.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Name: "api_calls_total",
|
||||
Help: "Total number of API calls",
|
||||
},
|
||||
[]string{"endpoint", "status", "code", "error"},
|
||||
)
|
||||
registerer *prometheus.Registry
|
||||
)
|
||||
|
||||
func init() {
|
||||
registerer = prometheus.NewRegistry()
|
||||
registerer.MustRegister(apiCallCounter1)
|
||||
}
|
||||
|
||||
func recordAPICall(endpoint string, status string) {
|
||||
apiCallCounter1.With(prometheus.Labels{"endpoint": endpoint, "status": status, "code": "200", "error": "ArgsError"}).Inc()
|
||||
}
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
go func() {
|
||||
for i := 0; ; i++ {
|
||||
recordAPICall("/api/test", "success")
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
for i := 0; ; i++ {
|
||||
recordAPICall("/api/test", "failed")
|
||||
time.Sleep(time.Second * 3)
|
||||
}
|
||||
}()
|
||||
promhttp.Handler()
|
||||
http.Handle("/metrics", promhttp.HandlerFor(registerer, promhttp.HandlerOpts{}))
|
||||
if err := http.ListenAndServe(":2112", nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestName2(t *testing.T) {
|
||||
var d time.Duration
|
||||
d = time.Second / 900
|
||||
fmt.Println(durationRange(d))
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
// 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 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"},
|
||||
// }
|
||||
//)
|
@ -0,0 +1,33 @@
|
||||
package prommetrics
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const RpcPath = "/metrics"
|
||||
|
||||
var (
|
||||
rpcRegistry = prometheus.NewRegistry()
|
||||
rpcCounter = prometheus.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Name: "rpc_count",
|
||||
Help: "Total number of RPC calls",
|
||||
},
|
||||
[]string{"name", "path", "code"},
|
||||
)
|
||||
)
|
||||
|
||||
func init() {
|
||||
rpcRegistry.MustRegister(rpcCounter)
|
||||
}
|
||||
|
||||
func RPCCall(name string, path string, code int) {
|
||||
rpcCounter.With(prometheus.Labels{"name": name, "path": path, "code": strconv.Itoa(code)}).Inc()
|
||||
}
|
||||
|
||||
func RPCHandler() http.Handler {
|
||||
return promhttp.HandlerFor(rpcRegistry, promhttp.HandlerOpts{})
|
||||
}
|
Loading…
Reference in new issue