Skip to content

Commit 7e1842e

Browse files
Fix interface usage
1 parent 98aa3db commit 7e1842e

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

cmd/prometheus.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"io/ioutil"
67
"net/http"
78
"os/exec"
@@ -12,15 +13,15 @@ import (
1213
type Prometheus interface {
1314
FetchPrometheusData(url string) (int, map[string]interface{})
1415
ImportPrometheusData(file, targetDir string) error
15-
ExecutePromtoolCommand(args ...string) (string, error)
16+
ExecutePromtoolCommand(sourceDir, targetDir string) (string, error)
1617
}
1718

18-
type PromClient struct {
19+
type PromHandler struct {
1920
}
2021

21-
func (promClient *PromClient) FetchPrometheusData(url string) (int, map[string]interface{}) {
22+
func (promHandler *PromHandler) FetchPrometheusData(url string) (int, map[string]interface{}) {
2223
var metric map[string]interface{}
23-
24+
fmt.Println(url)
2425
resp, err := http.Get(url)
2526
if err != nil {
2627
log.Err(err).Msg("Failed to fetch data")
@@ -42,17 +43,17 @@ func (promClient *PromClient) FetchPrometheusData(url string) (int, map[string]i
4243
return resp.StatusCode, metric
4344
}
4445

45-
func (promClient *PromClient) ImportPrometheusData(file, targetDir string) error {
46-
result, err := promClient.ExecutePromtoolCommand(file, targetDir)
46+
func (promHandler *PromHandler) ImportPrometheusData(file, targetDir string) error {
47+
result, err := promHandler.ExecutePromtoolCommand(file, targetDir)
4748
if err != nil {
48-
log.Err(err).Msg("Error on promClient.ExecutePromtoolCommand")
49+
log.Err(err).Msg("Error on PromHandler.ExecutePromtoolCommand")
4950
return err
5051
}
5152
log.Info().Msgf("Promtool command output is %s", result)
5253
return nil
5354
}
5455

55-
func (promClient *PromClient) ExecutePromtoolCommand(sourceDir, targetDir string) (string, error) {
56+
func (promHandler *PromHandler) ExecutePromtoolCommand(sourceDir, targetDir string) (string, error) {
5657
cmd := exec.Command("promtool", "tsdb", "create-blocks-from", "openmetrics", sourceDir, targetDir)
5758
output, err := cmd.CombinedOutput()
5859
if err != nil {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module prom-migrator
22

3-
go 1.24.1
3+
go 1.24
44

55
replace github.com/WoodProgrammer/prom-migrator/cmd => ./cmd
66

main.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"errors"
54
"fmt"
65
"os"
76
"strings"
@@ -27,36 +26,41 @@ var (
2726
metricType string
2827
)
2928

29+
func newPrometheusHandler(host string) prom.Prometheus {
30+
return &prom.PromHandler{}
31+
}
32+
3033
func CallPrometheus() {
3134

32-
newPrometheusClient := prom.PromClient{}
35+
var promHandler prom.Prometheus
36+
promHandler = newPrometheusHandler(promHost)
3337
rawMetricData := []string{}
3438

3539
if !strings.Contains(query, "\"") {
3640
query = strings.ReplaceAll(query, "{", "{job=\"") // Example fix
3741
}
3842
url := fmt.Sprintf("http://%s:%s/api/v1/query_range?query=%s&start=%s&end=%s&step=%s",
3943
promHost, promPort, query, startStamp, endStamp, step)
40-
status, data := newPrometheusClient.FetchPrometheusData(url)
44+
status, data := promHandler.FetchPrometheusData(url)
4145

4246
if status != 200 {
43-
err := errors.New(fmt.Sprintf("un expected response from Prometheus server %d", status))
44-
log.Err(err).Msg("Error while running newPrometheusClient.FetchPrometheusData")
47+
err := fmt.Errorf("un expected response from Prometheus server %d", status)
48+
log.Err(err).Msg("Error while running promHandler.FetchPrometheusData")
4549
return
4650
}
4751
rawMetricData = append(rawMetricData, fmt.Sprintf("# TYPE %s %s", strings.Split(query, "{")[0], metricType))
4852

4953
parsedData, ok := data["data"].(map[string]interface{})
5054

5155
if !ok {
52-
err := errors.New("json parsing error on rawPrometheus data ")
56+
err := fmt.Errorf("json parsing error on rawPrometheus data ")
5357
log.Err(err).Msg("Error parsing 'data'")
5458
return
5559
}
5660

5761
results, ok := parsedData["result"].([]interface{})
5862
if !ok {
59-
err := errors.New("json parsing error on parsedData['result'] data ")
63+
err := fmt.Errorf("json parsing error on parsedData['result'] data ")
6064
log.Err(err).Msg("Error parsing 'result'")
6165
return
6266
}
@@ -70,6 +74,9 @@ func CallPrometheus() {
7074

7175
labelMap := []string{}
7276
metric, ok := result["metric"].(map[string]interface{})
77+
if !ok {
78+
continue
79+
}
7380
metricName, ok := metric["__name__"].(string)
7481
if ok {
7582
for key, value := range metric {
@@ -93,7 +100,7 @@ func CallPrometheus() {
93100
}
94101

95102
}
96-
rawMetricData = append(rawMetricData, fmt.Sprintf("# EOF"))
103+
rawMetricData = append(rawMetricData, "# EOF")
97104

98105
err := ensureDir(dataDir)
99106
if err != nil {
@@ -104,7 +111,7 @@ func CallPrometheus() {
104111

105112
if len(targetDir) != 0 {
106113
cmd.FileHandler(fileName, rawMetricData)
107-
newPrometheusClient.ImportPrometheusData(fileName, targetDir)
114+
promHandler.ImportPrometheusData(fileName, targetDir)
108115
}
109116
}
110117

0 commit comments

Comments
 (0)