Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to visualize real-time runtime statistics for system integration in one step of Go third-party library

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "Go third-party library of how to visualize real-time runtime statistics for system integration", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Go third-party library of how to visualize real-time runtime statistics for system integration" bar.

Take a look at the real-time dynamic picture of Heap:

The above is done by running the following code:

Package main import ("math/rand"net/http"strconv"time"github.com/arl/statsviz") func main () {/ / Force the GC to work to make the plots "move". Go work () / / Register statsviz handlers on the default serve mux. Statsviz.RegisterDefault () http.ListenAndServe (": 8080", nil)} func work () {/ / Generate some allocations m: = map [string] [] byte {} for {b: = make ([] byte) 512+rand.Intn (16x 1024)) m [strconv.Itoa (len (m)% (10x 100))] = b if len (m)% (10x 100) = 0 {m = make (map [string] [] byte)} time.Sleep (10 * time.Millisecond)}}

1. How to use

It's all Go1.15.x, please use go module.

Statsviz is easy to use. Suppose it is based on net/http use.

Import "github.com/arl/statsviz"

Register for statsviz HTTP handlers

Startup program

Open a browser to access: http://host:port/debug/statsviz

Enjoy it.

Take a specific look at how it is integrated into the project in practice and introduce it separately according to the possible situation.

Based on net/http

If your project does not use a framework, it is directly based on net/http; or your project does not provide HTTP services. Integrating statsviz can be done in the following way.

1) A Web project using net/http

Just after the import statsviz package is needed, add the following code to the place where the route is registered:

Statsviz.RegisterDefault ()

This is using the default http.DefaultServeMux. If you are using a custom Mux, add code similar to the following:

Mux: = http.NewServeMux () statsviz.Register (mux)

Among them, mux uses what you defined.

In fact, statsviz.RegisterDefault () is implemented internally as follows:

Func RegisterDefault () {Register (http.DefaultServeMux)}

2) projects that do not provide HTTP services

The easiest way to do this is to use statsviz.RegisterDefault (), and you need to start a HTTP service:

Go func () {statsviz.RegisterDefault () log.Println (http.ListenAndServe ("localhost:8080", nil))} ()

Framework based on compatible net/http

Because it is compatible with the net/http framework, you can integrate statsviz's Handler directly. For example, for the gorilla/mux library, you can do this:

R: = mux.NewRouter () r.Methods ("GET"). Path ("/ debug/statsviz/ws"). Name ("GET / debug/statsviz/ws") .HandlerFunc (statsviz.Ws) r.Methods ("GET"). PathPrefix ("/ debug/statsviz/"). Name ("GET / debug/statsviz/"). Handler (statsviz.Index) mux: = http.NewServeMux () mux.Handle ("/", r) http.ListenAndServe (": 8080", mux)

Here are mainly statsviz.Ws and statsviz.Index, which can be done because the gorilla/mux library is compatible with net/http.

Frameworks that are not compatible with net/http

Such as integration into the Gin framework. The way to do this is to open another HTTP port, just like "projects that do not provide HTTP services".

Go func () {statsviz.RegisterDefault () log.Println (http.ListenAndServe ("localhost:8080", nil))} ()

So this actually has nothing to do with the specific framework.

Best practic

Because runtime information belongs to the internal information of the system, it is not suitable to be exposed to public network users. So, in practice, the best practice is to open a separate HTTP port in any case, which is inaccessible to the public network, as shown below.

Go func () {statsviz.RegisterDefault () log.Println (http.ListenAndServe ("localhost:8080", nil))} ()

A simple explanation of the principle

The library uses the WebSocket protocol to regularly send the runtime data of the system to the browser. After receiving it, the browser uses JS to draw a chart and display it. The core code of Go is as follows:

/ / sendStats indefinitely send runtime statistics on the websocket connection. Func sendStats (conn * websocket.Conn) error {tick: = time.NewTicker (defaultSendPeriod) defer tick.Stop () var stats stats for {select {case

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report