<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                &nbsp;&nbsp;&nbsp; 監控數據庫go程序 一、編寫一段go程序代碼 package main import ( "github.com/prometheus/client_golang/prometheus/promhttp" "log" "net/http" "runtime" "sync" "time" "github.com/prometheus/client_golang/prometheus" ) var ( goGoroutines = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_goroutines", Help: "Number of current goroutines.", }) goGCDurationSeconds = prometheus.NewHistogram(prometheus.HistogramOpts{ Name: "go_gc_duration_seconds", Help: "A histogram of the GC duration in seconds.", Buckets: []float64{0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10}, }) goMemstatsAllocBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_memstats_alloc_bytes", Help: "Number of bytes allocated and still in use.", }) goMemstatsHeapAllocBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_memstats_heap_alloc_bytes", Help: "Number of heap bytes allocated and still in use.", }) goMemstatsAllocBytesTotal = prometheus.NewCounter(prometheus.CounterOpts{ Name: "go_memstats_alloc_bytes_total", Help: "Total number of bytes allocated, even if freed.", }) goMemstatsSysBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_memstats_sys_bytes", Help: "Total number of bytes obtained by system. Sum of all system allocations.", }) goMemstatsHeapSysBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_memstats_heap_sys_bytes", Help: "Number of heap bytes obtained from system.", }) goMemstatsHeapIdleBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_memstats_heap_idle_bytes", Help: "Number of heap bytes waiting to be used.", }) goMemstatsHeapInuseBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_memstats_heap_inuse_bytes", Help: "Number of heap bytes that are in use.", }) goMemstatsStackInuseBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_memstats_stack_inuse_bytes", Help: "Number of bytes in use by the stack allocator.", }) goMemstatsStackSysBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_memstats_stack_sys_bytes", Help: "Total number of bytes obtained from system for stack allocator.", }) goMemstatsMspanInuseBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_memstats_mspan_inuse_bytes", Help: "Number of bytes in use by mspan structures.", }) goMemstatsMspanSysBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_memstats_mspan_sys_bytes", Help: "Number of bytes obtained from system for mspan structures.", }) goMemstatsMcacheInuseBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_memstats_mcache_inuse_bytes", Help: "Number of bytes in use by mcache structures.", }) goMemstatsMcacheSysBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_memstats_mcache_sys_bytes", Help: "Number of bytes obtained from system for mcache structures.", }) goMemstatsBuckHashSysBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_memstats_buck_hash_sys_bytes", Help: "Number of bytes used by the profiling bucket hash table.", }) goMemstatsGCSysBytes = prometheus.NewGauge(prometheus.GaugeOpts{ Name: "go_memstats_gc_sys_bytes", Help: "Number of bytes used for garbage collection system metadata.", }) ) func updateMetrics() { var stats runtime.MemStats runtime.ReadMemStats(&stats) goGoroutines.Set(float64(runtime.NumGoroutine())) goMemstatsAllocBytes.Set(float64(stats.Alloc)) goMemstatsHeapAllocBytes.Set(float64(stats.HeapAlloc)) goMemstatsAllocBytesTotal.Add(float64(stats.TotalAlloc)) goMemstatsSysBytes.Set(float64(stats.Sys)) goMemstatsHeapSysBytes.Set(float64(stats.HeapSys)) goMemstatsHeapIdleBytes.Set(float64(stats.HeapIdle)) goMemstatsHeapInuseBytes.Set(float64(stats.HeapInuse)) goMemstatsStackInuseBytes.Set(float64(stats.StackInuse)) goMemstatsStackSysBytes.Set(float64(stats.StackSys)) goMemstatsMspanInuseBytes.Set(float64(stats.MSpanInuse)) goMemstatsMspanSysBytes.Set(float64(stats.MSpanSys)) goMemstatsMcacheInuseBytes.Set(float64(stats.MCacheInuse)) goMemstatsMcacheSysBytes.Set(float64(stats.MCacheSys)) goMemstatsBuckHashSysBytes.Set(float64(stats.BuckHashSys)) goMemstatsGCSysBytes.Set(float64(stats.GCSys)) // For GC duration, we'll need to track GC events separately // and update the histogram accordingly. // Here's a simplified example of how to do it. // Note: This is not the most efficient way, but serves as an illustration. var mu sync.Mutex var lastGCTime time.Time mu.Lock() if lastGCTime.IsZero() { lastGCTime = time.Now() } else { duration := time.Since(lastGCTime).Seconds() mu.Unlock() goGCDurationSeconds.Observe(duration) // Update lastGCTime after unlocking to avoid race conditions mu.Lock() lastGCTime = time.Now() } } func main() { http.Handle("/metrics", promhttp.Handler()) // 定期更新指標 go func() { for { updateMetrics() // 每秒更新一次指標 time.Sleep(1 \* time.Second) } }() log.Fatal(http.ListenAndServe(":8182", nil)) } 運行,訪問 http://192.168.100.58:8182/metrics ![](https://img.kancloud.cn/a6/76/a6764024b22a53e5aa81df267a32b780_1477x928.png) <hr> 二、 prometheus服務器添加go程序的地址 &nbsp;&nbsp;&nbsp;2.4 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;192.168.100.85的centos上,修改prometheus的配置文件 #進入docker-prometheus目錄 cd /data/docker-prometheus #修改prometheus.yml vi prometheus/prometheus.yml &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;添加monogodb_exporter配置如下圖: ![](https://img.kancloud.cn/da/96/da967ce0355d3cfe20496d8936deb0dd_525x260.png) - job_name: "golang-exporter" static_configs: - targets: ["192.168.100.58:8182"] labels: istance: "centos2服務器go程序監聽" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;保存后輸入命令更新: curl -XPOST http://localhost:9090/-/reload &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;成功監聽 ![](https://img.kancloud.cn/8e/c1/8ec19bdaa9f7bd54f53f7eab10b4f433_1581x706.png) <hr> &nbsp;&nbsp;&nbsp;2.6 grafana中對go程序進行監控(因為程序參數是對著模板參數寫的,所以適合,自己隨便寫的得自己配面板) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy id to clipboard->grafana的dashboards中lmport dashboard https://grafana.com/grafana/dashboards/10826-go-metrics/ ![](https://img.kancloud.cn/e3/e1/e3e197b276b811cbbb5d581c89a6f3ba_1663x844.png) ![](https://img.kancloud.cn/6e/10/6e104658c167e77624421d4c6b2e365f_1881x906.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看