首页 > 其他分享 >Go 工程化 - 使用 expvar 监控接口状态

Go 工程化 - 使用 expvar 监控接口状态

时间:2023-02-08 17:55:05浏览次数:52  
标签:code http 计数器 404 Go 工程化 expvar 500

expvar 包为程序中的公共变量提供了一个标准化的接口,如服务中的访问计数器。包提供了 HTTP 接口并以 JSON 格式输出这些变量,接口的 URL 路由是 /debug/vars 。

最重要的是,操作这些公共变量是原子的,这意味着我们不需要考虑并发带来的问题。

例子

接口状态码计数器

package main

import (
    "expvar"
    "fmt"
    "log"
    "math/rand"
    "net/http"
    "time"
)

var (
    ok          = expvar.NewInt("200") // 200 计算器
    notFound    = expvar.NewInt("404") // 404 计数器
    serverError = expvar.NewInt("500") // 500 计数器
)

func helloWorld(w http.ResponseWriter, r *http.Request) {
    _, err := fmt.Fprintf(w, "hello world")
    if err != nil {
        log.Fatal(err)
    }

    ok.Add(1) // 增加 200 计数器
}

// 为了模拟 404, 500 错误
// 随机返回 Http Code [200, 404, 500]
func random(w http.ResponseWriter, r *http.Request) {
    rand.Seed(time.Now().UnixNano())
    n := rand.Intn(3)
    var code int

    switch n {
    case 0:
        code = http.StatusOK
        ok.Add(1) // 增加 200 计数器
    case 1:
        code = http.StatusNotFound
        notFound.Add(1) // 增加 404 计数器
    case 2:
        code = http.StatusInternalServerError
        serverError.Add(1) // 增加 500 计数器
    }

    w.WriteHeader(code)
}

func main() {
    http.HandleFunc("/", helloWorld)   // 默认返回地址
    http.HandleFunc("/random", random) // 随机返回状态码地址

    log.Fatal(http.ListenAndServe(":8080", nil))
}

标签:code,http,计数器,404,Go,工程化,expvar,500
From: https://www.cnblogs.com/cheyunhua/p/17102790.html

相关文章

  • BIGO 如何做到夜间同时运行 2.4K 个工作流实例?
    点亮⭐️Star·照亮开源之路GitHub:https://github.com/apache/dolphinscheduler ​精彩回顾近期,BIGO的大数据研发工程师许名勇在社区线上Meetup上给大家......
  • Linux 安装 go 环境
    目录安装运行go程序编译go程序安装wgethttps://dl.google.com/go/go1.19.5.linux-amd64.tar.gztarzxvfgo1.19.5.linux-amd64.tar.gz-C/usr/local/#vim/etc/pr......
  • Codeforces Round #720 (Div. 2) A. Nastia and Nearly Good Num
    problemA.NastiaandNearlyGoodNumberstimelimitpertest1secondmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputNastiahas2p......
  • CF Round #722 (Div. 2) C. Parsa‘s Humongous Tree(树形dp)
    problemC.Parsa’sHumongousTreetimelimitpertest1secondmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputParsahasahumongous......
  • Codeforces Round #720 (Div. 2), B. Nastia and a Good Array
    problemB.NastiaandaGoodArraytimelimitpertest2secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputNastiahasreceived......
  • django中有关登陆验证的LoginRequiredMixin类
      通常情况判断一个用户是否登陆可以使用request.user.is_is_authenticate(),就可以完成,返回结果TRUE或者FALSE classUserCenterInfoView(View):defget(self......
  • golang 内存泄漏分析案例
    1.前言关于内存泄漏的情形已经在之前文章总结过了,本文将讨论如何发现内存泄漏。2.怎么发现内存泄露在Go中发现内存泄露有2种方法,一个是通用的监控工具,另一个是goppro......
  • 【go-zero】demo尝试
    准备工作官网:https://go-zero.dev/cn/docs/introduction环境安装:Go:https://go-zero.dev/cn/docs/prepare/golang-installGoModule:https://go-zero.dev/cn/docs/prepa......
  • golang 时间time
    1.格式化返回当前时间:两种形式funcmain(){//格式化输出日期第一种方法:now.Year()等now:=time.Now()fmt.Printf("当前时间为:%d-%d-%d%d:%d:%d\n",no......
  • Go Gorm 使用
    简介官方文档:https://gorm.io/zh_CN/docs/index.htmlgithub:https://github.com/go-gorm/gorm同其他语言的ORM框架一样,这是Go版本的本文全部以mysql为例sql脚本为......