38

golang 程序如何解析 prometheus 的 api 数据

 2 years ago
source link: https://studygolang.com/articles/35346
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

golang 程序如何解析 prometheus 的 api 数据

wmd_nb · 大约19小时之前 · 93 次点击 · 预计阅读时间 1 分钟 · 大约8小时之前 开始浏览    
package main
import (
    "encoding/json"
    "fmt"
)

type Person struct {
    Age  string `json:"status"`
    Data Data   `json:"data"`
    Name string `json:"name"`
}

type Data struct {
    Values []map[string]interface{} `json:"result"`
}

func main() {
    // 假数据
    b := []byte(`{"status":"success","wmd":{"result":"wmd"},"name":"wmd","data":{"resultType":"matrix","result":[{"metric":{"__name__":"namedprocess_namegroup_memory_bytes","groupname":"root","instance":"localhost:9256","job":"process_export_9256","memtype":"resident"},"values":[[1635991200,"300257280"],[1635993000,"302428160"],[1635994800,"301727744"]]}]}}`)
    var p Person
    err := json.Unmarshal(b, &p)
    if err != nil {
        fmt.Println(err)
    }

    wmd := p.Data.Values[0]["values"]

    fmt.Println(wmd)
}

上面是代码,最后解析出来的是 interface 类型数据,但是我没办法给拿出来变成字符串或者数组类型的,没办法进行求和、平均值啊这类的。求大佬们指教


有疑问加站长微信联系(非本文作者))

280

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:701969077

3 回复  |  直到 2021-11-26 15:14:43

YYYGH · #1 · 大约3小时之前

package main import ( "encoding/json" "fmt" )

type Person struct { Age string json:"status" Data Data json:"data" Name string json:"name" }

type Data struct { Values []map[string]interface{} json:"result" }

func main() { // 假数据 b := []byte({"status":"success","wmd":{"result":"wmd"},"name":"wmd","data":{"resultType":"matrix","result":[{"metric":{"__name__":"namedprocess_namegroup_memory_bytes","groupname":"root","instance":"localhost:9256","job":"process_export_9256","memtype":"resident"},"values":[[1635991200,"300257280"],[1635993000,"302428160"],[1635994800,"301727744"]]}]}}) var p Person err := json.Unmarshal(b, &p) if err != nil { fmt.Println(err) }

wmd := p.Data.Values[0]["values"]
/*
    下面这种方法可行,建议最好根据api返回的数据格式定义struct 去解决
*/
list := wmd.([]interface{})
sum := float64(0)
for _, v := range list {
    value := v.([]interface{})
    for _, data := range value {
        switch data := data.(type) {
        case string:
            i, _ := strconv.ParseFloat(data, 64)
            sum += i
        case float64:
            sum += data
        }
    }
}
fmt.Printf("%f\n", sum)

YYYGH · #2 · 大约3小时之前


type Person struct {
    Age  string `json:"status"`
    Data Data   `json:"data"`
    Name string `json:"name"`
}

type Data struct {
    Values []map[string]interface{} `json:"result"`
}

func main() {
    // 假数据
    b := []byte(`{"status":"success","wmd":{"result":"wmd"},"name":"wmd","data":{"resultType":"matrix","result":[{"metric":{"__name__":"namedprocess_namegroup_memory_bytes","groupname":"root","instance":"localhost:9256","job":"process_export_9256","memtype":"resident"},"values":[[1635991200,"300257280"],[1635993000,"302428160"],[1635994800,"301727744"]]}]}}`)
    var p Person
    err := json.Unmarshal(b, &p)
    if err != nil {
        fmt.Println(err)
    }

    wmd := p.Data.Values[0]["values"]
    /*
        下面这种方法可行,建议最好根据api返回的数据格式定义struct 去解决
    */
    list := wmd.([]interface{})
    sum := float64(0)
    for _, v := range list {
        value := v.([]interface{})
        for _, data := range value {
            switch data := data.(type) {
            case string:
                i, _ := strconv.ParseFloat(data, 64)
                sum += i
            case float64:
                sum += data
            }
        }
    }
    fmt.Printf("%f\n", sum)
}

wmd_nb · #3 · 大约1小时之前

大佬牛逼啊,空接口还能这样用,学习了。感谢

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK