summaryrefslogtreecommitdiff
path: root/test/netperf-go/puppeteer/promMetricFetcher.go
blob: cd268d0db12bd29a122da321035dc8141256ef96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"reflect"
	"strconv"
)

type promMetricFetcher struct {
	promHost string
}

type promValueResult struct {
	timestamp float64
	value     float64
}

func makePromMetricFetcher(host string) *promMetricFetcher {

	return &promMetricFetcher{
		promHost: host,
	}
}

func (r *promMetricFetcher) getMetric(query string) (results []promValueResult, err error) {
	queryURL := fmt.Sprintf("http://%s:9090/api/v1/query", r.promHost)
	req, err := http.NewRequest("GET", queryURL, nil)
	if err != nil {
		return
	}
	q := req.URL.Query()
	q.Add("query", query)
	req.URL.RawQuery = q.Encode()

	httpClient := http.Client{}
	resp, err := httpClient.Do(req /*req.WithContext(ctx)*/)
	if err != nil {
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return nil, fmt.Errorf("http error code received %v", resp.StatusCode)
	}

	bytes, err := io.ReadAll(resp.Body)
	if err != nil {
		return
	}

	var resultsMap map[string]interface{}
	pathMap := make(map[string]interface{})
	err = json.Unmarshal(bytes, &resultsMap)
	if err != nil {
		return
	}

	r.parseMap("", resultsMap, pathMap)
	for i := 0; ; i++ {
		// ensure that we have /data/result[X]/value[0] and /data/result[X]/value[1]
		tsName := fmt.Sprintf("/data/result[%d]/value[0]", i)
		valName := fmt.Sprintf("/data/result[%d]/value[1]", i)
		_, hasTs := pathMap[tsName]
		_, hasVal := pathMap[valName]
		if !hasTs || !hasVal {
			break
		}

		var tsFloat, valFloat float64
		if flt, ok := pathMap[tsName].(float64); !ok {
			break
		} else {
			tsFloat = flt
		}

		switch concreteVal := pathMap[valName].(type) {
		case float64:
			valFloat = concreteVal
		case uint64:
			valFloat = float64(concreteVal)
		case int64:
			valFloat = float64(concreteVal)
		case string:
			valFloat, _ = strconv.ParseFloat(concreteVal, 64)
		default:
			fmt.Printf("failed : %v %v\n", pathMap[valName], reflect.TypeOf(pathMap[valName]))
			return
		}

		results = append(results, promValueResult{
			timestamp: tsFloat,
			value:     valFloat,
		})
	}

	return results, nil
}

func (r *promMetricFetcher) parseMap(path string, aMap map[string]interface{}, pathMap map[string]interface{}) {
	for key, val := range aMap {
		switch concreteVal := val.(type) {
		case map[string]interface{}:
			r.parseMap(path+"/"+key, val.(map[string]interface{}), pathMap)
		case []interface{}:
			r.parseArray(path+"/"+key, val.([]interface{}), pathMap)
		default:
			//fmt.Println(path+"/"+key, ":", concreteVal)
			pathMap[path+"/"+key] = concreteVal
		}
	}
}

func (r *promMetricFetcher) parseArray(path string, anArray []interface{}, pathMap map[string]interface{}) {
	for i, val := range anArray {
		switch concreteVal := val.(type) {
		case map[string]interface{}:
			r.parseMap(fmt.Sprintf("%s[%d]", path, i), val.(map[string]interface{}), pathMap)
		case []interface{}:
			r.parseArray(fmt.Sprintf("%s[%d]", path, i), val.([]interface{}), pathMap)
		default:
			//fmt.Println(fmt.Sprintf("%s[%d]", path, i), concreteVal)
			pathMap[fmt.Sprintf("%s[%d]", path, i)] = concreteVal
		}
	}
}

func (r *promMetricFetcher) getSingleValue(results []promValueResult) (result float64, err error) {
	if len(results) > 1 {
		return 0.0, fmt.Errorf("unexpected number of results %v", results)
	}
	if len(results) == 0 {
		return 0.0, nil
	}
	return results[0].value, nil
}

//curl http://telemetry.algodev.network:9090/api/v1/query\?query\=max\(algod_ledger_round\)