Skip to content

Commit

Permalink
use monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
day253 committed Oct 26, 2024
1 parent 43ecf99 commit 4f2422e
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 73 deletions.
4 changes: 2 additions & 2 deletions boilerplate/pkg/models/predictor_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/ishumei/krpc/boilerplate/pkg/conf"
"github.com/ishumei/krpc/kserver"
prometheus "github.com/ishumei/krpc/monitor-prometheus"
"github.com/ishumei/krpc/monitor"
"github.com/ishumei/krpc/protocols/arbiter/kitex_gen/com/shumei/service/predictor"
"github.com/kitex-contrib/obs-opentelemetry/tracing"
"github.com/samber/do"
Expand Down Expand Up @@ -51,7 +51,7 @@ func (m *PredictorOption) apply(p *Predictor) {
//nolint:staticcheck // SA1019 ignore the deprecation warnings
client.WithSuite(tracing.NewFramedClientSuite()),
client.WithClientBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: m.NodePath}),
client.WithTracer(prometheus.NewClientTracerWithoutExport()),
client.WithTracer(monitor.NewClientTracerWithoutExport()),
)
return c
}()
Expand Down
4 changes: 2 additions & 2 deletions kclient/client_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/cloudwego/kitex/pkg/retry"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/transport"
monitor_prometheus "github.com/ishumei/krpc/monitor-prometheus"
"github.com/ishumei/krpc/monitor"
"github.com/kitex-contrib/obs-opentelemetry/tracing"
)

Expand Down Expand Up @@ -35,7 +35,7 @@ func MustNewClientOptionsWithoutResolver(c *SingleClientConf, opts ...client.Opt
}
return DefaultClientName
}()}),
client.WithTracer(monitor_prometheus.NewClientTracerWithoutExport()),
client.WithTracer(monitor.NewClientTracerWithoutExport()),
}

options = append(options, opts...)
Expand Down
4 changes: 2 additions & 2 deletions kserver/server_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/server"
_ "github.com/ishumei/krpc/autolimit"
monitor_prometheus "github.com/ishumei/krpc/monitor-prometheus"
"github.com/ishumei/krpc/monitor"
"github.com/kitex-contrib/obs-opentelemetry/tracing"
"github.com/samber/do"
"github.com/samber/lo"
Expand All @@ -32,7 +32,7 @@ func NewServerOptions(i *do.Injector) (*ServerOptions, error) {
options := []server.Option{
server.WithServiceAddr(addr),
server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: c.ServiceName}),
server.WithTracer(monitor_prometheus.NewServerTracerWithoutExport()),
server.WithTracer(monitor.NewServerTracerWithoutExport()),
server.WithExitSignal(DefaultUserExitSignal),
}

Expand Down
27 changes: 0 additions & 27 deletions monitor-prometheus/metrics.go

This file was deleted.

12 changes: 6 additions & 6 deletions monitor-prometheus/export.go → monitor/export.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package monitor_prometheus
package monitor

import (
"strconv"
"time"

prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus"
)

type ArchClientLabel struct {
Expand All @@ -20,7 +20,7 @@ func ArchClientThroughput(labels *ArchClientLabel) {
if labels == nil {
return
}
var l = make(prom.Labels)
var l = make(prometheus.Labels)
l[labelKeyCaller] = defaultValIfEmpty(labels.Caller, unknownLabelValue)
l[labelKeyCallee] = defaultValIfEmpty(labels.Callee, unknownLabelValue)
l[labelKeyMethod] = defaultValIfEmpty(labels.Method, unknownLabelValue)
Expand All @@ -34,7 +34,7 @@ func ArchClientLatencyUs(value time.Duration, labels *ArchClientLabel) {
if labels == nil {
return
}
var l = make(prom.Labels)
var l = make(prometheus.Labels)
l[labelKeyCaller] = defaultValIfEmpty(labels.Caller, unknownLabelValue)
l[labelKeyCallee] = defaultValIfEmpty(labels.Callee, unknownLabelValue)
l[labelKeyMethod] = defaultValIfEmpty(labels.Method, unknownLabelValue)
Expand All @@ -57,7 +57,7 @@ func ArchServerThroughput(labels *ArchServerLabel) {
if labels == nil {
return
}
var l = make(prom.Labels)
var l = make(prometheus.Labels)
l[labelKeyCaller] = defaultValIfEmpty(labels.Caller, unknownLabelValue)
l[labelKeyCallee] = defaultValIfEmpty(labels.Callee, unknownLabelValue)
l[labelKeyMethod] = defaultValIfEmpty(labels.Method, unknownLabelValue)
Expand All @@ -71,7 +71,7 @@ func ArchServerLatencyUs(value time.Duration, labels *ArchServerLabel) {
if labels == nil {
return
}
var l = make(prom.Labels)
var l = make(prometheus.Labels)
l[labelKeyCaller] = defaultValIfEmpty(labels.Caller, unknownLabelValue)
l[labelKeyCallee] = defaultValIfEmpty(labels.Callee, unknownLabelValue)
l[labelKeyMethod] = defaultValIfEmpty(labels.Method, unknownLabelValue)
Expand Down
27 changes: 27 additions & 0 deletions monitor/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package monitor

import (
"time"

"github.com/prometheus/client_golang/prometheus"
)

// counterAdd wraps Add of prometheus.Counter.
func counterAdd(counterVec *prometheus.CounterVec, value int, labels prometheus.Labels) error {
counter, err := counterVec.GetMetricWith(labels)
if err != nil {
return err
}
counter.Add(float64(value))
return nil
}

// histogramObserve wraps Observe of prometheus.Observer.
func histogramObserve(histogramVec *prometheus.HistogramVec, value time.Duration, labels prometheus.Labels) error {
histogram, err := histogramVec.GetMetricWith(labels)
if err != nil {
return err
}
histogram.Observe(float64(value.Microseconds()))
return nil
}
44 changes: 22 additions & 22 deletions monitor-prometheus/tracer.go → monitor/tracer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package monitor_prometheus
package monitor

import (
"context"
Expand All @@ -10,7 +10,7 @@ import (
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/pkg/stats"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

Expand All @@ -31,30 +31,30 @@ const (
)

var (
globalClientHandledCounter = prom.NewCounterVec(
prom.CounterOpts{
globalClientHandledCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "arch_client_throughput",
Help: "Total number of RPCs completed by the client, regardless of success or failure.",
},
[]string{labelKeyCaller, labelKeyCallee, labelKeyDetail, labelKeyMethod, labelKeyStatus, labelKeyRetry},
)
globalClientHandledHistogram = prom.NewHistogramVec(
prom.HistogramOpts{
globalClientHandledHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "arch_client_latency_us",
Help: "Latency (microseconds) of the RPC until it is finished.",
Buckets: []float64{5000, 10000, 25000, 50000, 100000, 250000, 500000, 1000000},
},
[]string{labelKeyCaller, labelKeyCallee, labelKeyDetail, labelKeyMethod, labelKeyStatus, labelKeyRetry},
)
globalServerHandledCounter = prom.NewCounterVec(
prom.CounterOpts{
globalServerHandledCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "arch_server_throughput",
Help: "Total number of RPCs completed by the server, regardless of success or failure.",
},
[]string{labelKeyCaller, labelKeyCallee, labelKeyDetail, labelKeyMethod, labelKeyStatus, labelKeyRetry},
)
globalServerHandledHistogram = prom.NewHistogramVec(
prom.HistogramOpts{
globalServerHandledHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "arch_server_latency_us",
Help: "Latency (microseconds) of RPC that had been application-level handled by the server.",
Buckets: []float64{5000, 10000, 25000, 50000, 100000, 250000, 500000, 1000000},
Expand Down Expand Up @@ -92,9 +92,9 @@ func (e errDetail) Detail() string {
}

// genLabels make labels values.
func genLabels(ri rpcinfo.RPCInfo) prom.Labels {
func genLabels(ri rpcinfo.RPCInfo) prometheus.Labels {
var (
labels = make(prom.Labels)
labels = make(prometheus.Labels)

caller = ri.From()
callee = ri.To()
Expand All @@ -118,8 +118,8 @@ func genLabels(ri rpcinfo.RPCInfo) prom.Labels {
}

type clientTracer struct {
clientHandledCounter *prom.CounterVec
clientHandledHistogram *prom.HistogramVec
clientHandledCounter *prometheus.CounterVec
clientHandledHistogram *prometheus.HistogramVec
}

// Start record the beginning of an RPC invocation.
Expand All @@ -137,7 +137,7 @@ func (c *clientTracer) Finish(ctx context.Context) {
rpcFinish := ri.Stats().GetEvent(stats.RPCFinish)
cost := rpcFinish.Time().Sub(rpcStart.Time())

extraLabels := make(prom.Labels)
extraLabels := make(prometheus.Labels)
extraLabels[labelKeyStatus] = statusSucceed
if ri.Stats().Error() != nil {
extraLabels[labelKeyStatus] = statusError
Expand Down Expand Up @@ -173,8 +173,8 @@ func NewClientTracerWithoutExport() stats.Tracer {
}

type serverTracer struct {
serverHandledCounter *prom.CounterVec
serverHandledHistogram *prom.HistogramVec
serverHandledCounter *prometheus.CounterVec
serverHandledHistogram *prometheus.HistogramVec
}

// Start record the beginning of server handling request from client.
Expand All @@ -193,7 +193,7 @@ func (c *serverTracer) Finish(ctx context.Context) {
rpcFinish := ri.Stats().GetEvent(stats.RPCFinish)
cost := rpcFinish.Time().Sub(rpcStart.Time())

extraLabels := make(prom.Labels)
extraLabels := make(prometheus.Labels)
extraLabels[labelKeyStatus] = statusSucceed
if ri.Stats().Error() != nil {
extraLabels[labelKeyStatus] = statusError
Expand Down Expand Up @@ -249,8 +249,8 @@ func removeDynamicDetail(val, def string) string {
}

func init() {
prom.MustRegister(globalClientHandledCounter)
prom.MustRegister(globalClientHandledHistogram)
prom.MustRegister(globalServerHandledCounter)
prom.MustRegister(globalServerHandledHistogram)
prometheus.MustRegister(globalClientHandledCounter)
prometheus.MustRegister(globalClientHandledHistogram)
prometheus.MustRegister(globalServerHandledCounter)
prometheus.MustRegister(globalServerHandledHistogram)
}
22 changes: 11 additions & 11 deletions monitor-prometheus/tracer_test.go → monitor/tracer_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package monitor_prometheus
package monitor

import (
"io"
Expand All @@ -7,13 +7,13 @@ import (
"testing"
"time"

prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/stretchr/testify/assert"
)

func TestPrometheusReporter(t *testing.T) {
registry := prom.NewRegistry()
registry := prometheus.NewRegistry()
http.Handle("/prometheus", promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}))
go func() {
if err := http.ListenAndServe(":9090", nil); err != nil {
Expand All @@ -22,26 +22,26 @@ func TestPrometheusReporter(t *testing.T) {
}
}()

counter := prom.NewCounterVec(
prom.CounterOpts{
counter := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "test_counter",
ConstLabels: prom.Labels{"service": "prometheus-test"},
ConstLabels: prometheus.Labels{"service": "prometheus-test"},
},
[]string{"test1", "test2"},
)
registry.MustRegister(counter)

histogram := prom.NewHistogramVec(
prom.HistogramOpts{
histogram := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "test_histogram",
ConstLabels: prom.Labels{"service": "prometheus-test"},
Buckets: prom.DefBuckets,
ConstLabels: prometheus.Labels{"service": "prometheus-test"},
Buckets: prometheus.DefBuckets,
},
[]string{"test1", "test2"},
)
registry.MustRegister(histogram)

labels := prom.Labels{
labels := prometheus.Labels{
"test1": "abc",
"test2": "def",
}
Expand Down
1 change: 0 additions & 1 deletion registry-zookeeper/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

json "github.com/bytedance/sonic"

"github.com/cloudwego/kitex/pkg/discovery"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/go-zookeeper/zk"
Expand Down

0 comments on commit 4f2422e

Please sign in to comment.