Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/metrics #13

Merged
merged 6 commits into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
services/data/
services/data/
.vscode/
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

5 changes: 5 additions & 0 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/klintcheng/kim/tcp"
"github.com/klintcheng/kim/wire"
"github.com/klintcheng/kim/wire/pkt"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

const (
Expand Down Expand Up @@ -94,6 +95,8 @@ func EnableMonitor(listen string) {
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("ok"))
})
// add prometheus metrics
http.Handle("/metrics", promhttp.Handler())
_ = http.ListenAndServe(listen, nil)
}()
})
Expand All @@ -103,6 +106,7 @@ func EnableMonitor(listen string) {
func SetSelector(selector Selector) {
c.selector = selector
}

// SetServiceNaming
func SetServiceNaming(nm naming.Naming) {
c.Naming = nm
Expand Down Expand Up @@ -364,6 +368,7 @@ func pushMessage(packet *pkt.LogicPkt) error {
log.Debugf("Push to %v %v", channelIds, packet)

for _, channel := range channelIds {
messageOutFlowBytes.WithLabelValues(packet.Command).Add(float64(len(payload)))
err := c.Srv.Push(channel, payload)
if err != nil {
log.Debug(err)
Expand Down
12 changes: 12 additions & 0 deletions container/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package container

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var messageOutFlowBytes = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "kim",
Name: "message_out_flow_bytes",
Help: "网关下发的消息字节数",
}, []string{"command"})
5 changes: 4 additions & 1 deletion default_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,12 @@ func (s *DefaultServer) connHandler(rawconn net.Conn, gpool *ants.Pool) {
channel := NewChannel(id, meta, conn, gpool)
channel.SetReadWait(s.options.Readwait)
channel.SetWriteWait(s.options.Writewait)

s.Add(channel)

gaugeWithLabel := channelTotalGauge.WithLabelValues(s.ServiceID(), s.ServiceName())
gaugeWithLabel.Inc()
defer gaugeWithLabel.Dec()

logger.Infof("accept channel - ID: %s RemoteAddr: %s", channel.ID(), channel.RemoteAddr())
err = channel.Readloop(s.MessageListener)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions examples/kimbench/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewBenchmarkCmd(ctx context.Context) *cobra.Command {
Short: "kim benchmark tools",
}
var opts = &Options{}
cmd.PersistentFlags().StringVarP(&opts.Addr, "address", "a", "ws://124.71.204.19:8000", "server address")
cmd.PersistentFlags().StringVarP(&opts.Addr, "address", "a", "ws://localhost:8000", "server address")
cmd.PersistentFlags().StringVarP(&opts.AppSecret, "appSecret", "s", token.DefaultSecret, "app secret")
cmd.PersistentFlags().IntVarP(&opts.Count, "count", "c", 100, "request count")
cmd.PersistentFlags().IntVarP(&opts.Threads, "thread", "t", 10, "thread count")
Expand Down Expand Up @@ -72,7 +72,7 @@ func NewLoginCmd(opts *Options) *cobra.Command {
return nil
},
}
cmd.PersistentFlags().DurationVarP(&options.keep, "keep", "k", time.Second*5, "the duration of keeping the client connection")
cmd.PersistentFlags().DurationVarP(&options.keep, "keep", "k", time.Millisecond*10, "the duration of keeping the client connection")
return cmd
}

Expand Down
2 changes: 2 additions & 0 deletions examples/kimbench/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func login(wsurl, appSecret string, threads int, count int, keep time.Duration)
r.Finalize(time.Since(t1))

logger.Infof("keep login for %v", keep)
time.Sleep(keep)

for _, cli := range clis {
cli.Close()
}
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.4.3
github.com/hashicorp/consul/api v1.8.1
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/kataras/iris/v12 v12.2.0-alpha2.0.20210705170737-afb15b860124
github.com/kelseyhightower/envconfig v1.4.0
github.com/kr/text v0.2.0 // indirect
Expand All @@ -23,19 +22,18 @@ require (
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/panjf2000/ants/v2 v2.4.6
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.11.0
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
github.com/segmentio/ksuid v1.0.3
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
github.com/yudai/pp v2.0.1+incompatible // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
golang.org/x/sys v0.0.0-20211020174200-9d6173849985 // indirect
google.golang.org/grpc v1.33.2
google.golang.org/protobuf v1.25.0
google.golang.org/protobuf v1.26.0-rc.1
gorm.io/driver/mysql v1.1.1
gorm.io/driver/sqlite v1.1.5
gorm.io/gorm v1.21.15
Expand Down
Loading