From 6cfd6b6de82dad3b18c27c877cd3cfa6a70bada6 Mon Sep 17 00:00:00 2001 From: "pokornyIt.cz" <8665714+pokornyIt@users.noreply.github.com> Date: Thu, 2 Jul 2020 14:02:49 +0200 Subject: [PATCH] Prepare for first version (#2) * Fix after project rename * Add unit tests * More unit testing testing * Prepare for public --- .github/workflows/release.yml | 6 +- .github/workflows/{go.yml => test.yml} | 5 +- .gitignore | 2 +- README.md | 36 ++++++-- api-reader.go | 4 +- config.go | 37 ++++---- config_test.go | 82 ++++++++++++++++++ go.mod | 5 +- go.sum | 61 ++----------- main.go | 21 +++-- matrix.go | 75 ++++++++++++++++ matrix_test.go | 100 ++++++++++++++++++++++ point.go | 42 +++++++++ point_test.go | 43 ++++++++++ query_range.go | 96 --------------------- series.go | 50 +++++++++++ series_test.go | 114 +++++++++++++++++++++++++ 17 files changed, 581 insertions(+), 198 deletions(-) rename .github/workflows/{go.yml => test.yml} (91%) create mode 100644 config_test.go create mode 100644 matrix.go create mode 100644 matrix_test.go create mode 100644 point.go create mode 100644 point_test.go create mode 100644 series.go create mode 100644 series_test.go diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 778cde0..dd60bcc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,4 +1,4 @@ -name: Release Go Binaries +name: Release on: release: @@ -14,12 +14,10 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - goos: [linux, windows, darwin] + goos: [linux, windows] goarch: [amd64, arm64] exclude: # windows/arm64 and darwin/arm64 seems useless - - goarch: arm64 - goos: darwin - goarch: arm64 goos: windows steps: diff --git a/.github/workflows/go.yml b/.github/workflows/test.yml similarity index 91% rename from .github/workflows/go.yml rename to .github/workflows/test.yml index 18f3a9d..da435bf 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -name: Build +name: Test on: push: @@ -35,6 +35,3 @@ jobs: - name: Test run: go test -v . - - - name: Show Data - run: ./prometehus_export --version \ No newline at end of file diff --git a/.gitignore b/.gitignore index 42d4bbd..a107e7b 100644 --- a/.gitignore +++ b/.gitignore @@ -91,5 +91,5 @@ vendor/ .idea/misc.xml .idea/modules.xml .idea/vcs.xml -/.idea/prometehus_export.iml dump/ +/.idea/prometheus_data_dump.iml diff --git a/README.md b/README.md index a1f3f4b..12aa481 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,31 @@ -# Prometheus Export -Dump data from prometheus to JSON files. +![Build Status](https://github.com/pokornyIt/prometheus_data_dump/workflows/Build/badge.svg) +[![License](https://img.shields.io/github/license/pokornyIt/prometheus_data_dump)](/LICENSE) +[![Go Report Card](https://goreportcard.com/badge/github.com/pokornyIt/prometheus_data_dump)](https://goreportcard.com/report/github.com/pokornyIt/nut_exporter) +![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/pokornyit/prometheus_data_dump?label=latest) -Program dump all metrics for selected jobs into JSON files. +# Prometheus Data Dump +Project designed to export data from the Prometheus database. +Exports are intend for further processing in other systems that do not support +direct integration to the Prometheus system as a data source. -## Configuration +Each Prometheus metric is export to a separate file. +The data is export for a defined number of days back and can be limited to selected "jobs". +Existing exported data is overwrite by the new export. +A special file "metrics-meta.json" is exported, which contains a description of individual metrics. + +# Program start + +The program requires the entry of selected configuration parameters for its start. +This is mainly the address of the Prometheus server from which the data will be exported. + +## Configuration file Program has config file. ```yaml -server: perftcl2.perflab.zoomint.com +server: prometehus.server path: ./dump days: 2 +step: 10 jobs: - node_exporter_zqm - cucm_monitor @@ -18,10 +34,16 @@ jobs: - **server** - FQDN or IP address of prometheus server - **path** - Path for store export data - **days** - Number of day to exports (1-60) +- **step** - Step for time slice in seconds (5 - 3600), default 10 - **jobs** - limit data only for target jobs. If omitted or empty mean export all jobs -## Line parameters +## Configuration line parameters - **--config.show** - show actual configuration and exit -- **--config.file=cfg.yml** - define config file, defaul is cfg.yml +- **--config.file=cfg.yml** - define config file, default is cfg.yml - **--path=./dump** - overwrite path defined in config file - **--server=IP** - FQDN or IP address of prometheus server + +# Contribute +We welcome any contributions. Please fork the project on GitHub and open Pull Requests for any proposed changes. + +Please note that we will not merge any changes that encourage insecure behaviour. If in doubt please open an Issue first to discuss your proposal. \ No newline at end of file diff --git a/api-reader.go b/api-reader.go index 8725a74..a6851b6 100644 --- a/api-reader.go +++ b/api-reader.go @@ -13,7 +13,7 @@ const UriFormat = "http://%s:9090/api/v1/%s" func getFormUri(uri string) (data []byte, err error) { uri = fmt.Sprintf(UriFormat, config.Server, uri) - _ = level.Info(logger).Log("msg", "read data from server ", "uri", uri) + _ = level.Debug(logger).Log("msg", "read data from server ", "uri", uri) req, err := http.NewRequest("GET", uri, nil) if err != nil { _ = level.Error(logger).Log("msg", "problem create request for uri "+uri, "error", err) @@ -50,6 +50,6 @@ func finishApiRequest(req *http.Request) (data []byte, err error) { _ = level.Error(logger).Log("msg", "problem read data from response", "error", err) return nil, err } - _ = level.Debug(logger).Log("msg", "success read "+string(len(bodies))+" bytes from uri") + _ = level.Debug(logger).Log("msg", fmt.Sprintf("success read %d bytes from uri", len(bodies))) return bodies, nil } diff --git a/config.go b/config.go index 2ca5aab..bca6947 100644 --- a/config.go +++ b/config.go @@ -74,27 +74,16 @@ func dirAccessible(directory string) bool { return err == nil } -func (c *Config) LoadFile(filename string) error { - if fileExists(filename) { - content, err := ioutil.ReadFile(filename) - if err != nil { - return err - } - err = yaml.UnmarshalStrict(content, c) - if err != nil { - err = json.Unmarshal(content, c) - if err != nil { - return err - } - } - } +func (c *Config) overWriteFromLine() { if len(*server) > 0 { c.Server = *server } if len(*directoryData) > 0 { c.Path = *directoryData } +} +func (c *Config) validate() error { match, err := regexp.MatchString("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", c.Server) if !match || err != nil { match, err = regexp.MatchString("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$", c.Server) @@ -112,7 +101,7 @@ func (c *Config) LoadFile(filename string) error { if c.Days < 1 || c.Days > 60 { return errors.New("defined days back not valid (1 - 60)") } - if c.Step < 1 || c.Step > 60*60 { + if c.Step < 5 || c.Step > 3600 { c.Step = 10 } if !dirExists(c.Path) { @@ -124,6 +113,24 @@ func (c *Config) LoadFile(filename string) error { return nil } +func (c *Config) LoadFile(filename string) error { + if fileExists(filename) { + content, err := ioutil.ReadFile(filename) + if err != nil { + return err + } + err = yaml.UnmarshalStrict(content, c) + if err != nil { + err = json.Unmarshal(content, c) + if err != nil { + return err + } + } + } + c.overWriteFromLine() + return c.validate() +} + func (c *Config) print() string { a := fmt.Sprintf("\r\n%s\r\nActual configuration:\r\n", applicationName) a = fmt.Sprintf("%sServer: [%s]\r\n", a, c.Server) diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..64289ee --- /dev/null +++ b/config_test.go @@ -0,0 +1,82 @@ +package main + +import "testing" + +func TestConfig_overWriteFromLine(t *testing.T) { + type args struct { + server string + path string + } + type fields struct { + Server string + Path string + Days int + Jobs []string + Step int + } + tests := []struct { + name string + fields fields + args args + expect args + }{ + {"none", fields{"server.local", "/my", 1, nil, 10}, args{}, args{"server.local", "/my"}}, + {"server", fields{"server.local", "/my", 1, nil, 10}, args{"server.new", ""}, args{"server.new", "/my"}}, + {"path", fields{"server.local", "/my", 1, nil, 10}, args{"", "/newPath"}, args{"server.local", "/newPath"}}, + {"both", fields{"server.local", "/my", 1, nil, 10}, args{"server.new", "/newPath"}, args{"server.new", "/newPath"}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &Config{ + Server: tt.fields.Server, + Path: tt.fields.Path, + Days: tt.fields.Days, + Jobs: tt.fields.Jobs, + Step: tt.fields.Step, + } + server = &tt.args.server + directoryData = &tt.args.path + c.overWriteFromLine() + if c.Server != tt.expect.server { + t.Errorf("overWriteFromLine() = server %s, want %s", c.Server, tt.expect.server) + } + if c.Path != tt.expect.path { + t.Errorf("overWriteFromLine() = path %s, want %s", c.Path, tt.expect.path) + } + }) + } +} + +func TestConfig_validate(t *testing.T) { + type fields struct { + Server string + Path string + Days int + Jobs []string + Step int + } + tests := []struct { + name string + fields fields + wantErr bool + }{ + {"all valid", fields{"server.local", "./", 2, nil, 10}, false}, + {"no server", fields{"", "./", 2, nil, 10}, true}, + {"no path", fields{"", "", 2, nil, 10}, true}, + {"wrong day", fields{"", "./", -2, nil, 10}, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &Config{ + Server: tt.fields.Server, + Path: tt.fields.Path, + Days: tt.fields.Days, + Jobs: tt.fields.Jobs, + Step: tt.fields.Step, + } + if err := c.validate(); (err != nil) != tt.wantErr { + t.Errorf("validate() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/go.mod b/go.mod index dfbf95d..33d61cb 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,11 @@ -module github.com/pokornyIt/prometehus_export +module github.com/pokornyIt/prometehus_data_dump go 1.14 require ( github.com/go-kit/kit v0.9.0 - github.com/opentracing/opentracing-go v1.1.0 // indirect github.com/prometheus/client_golang v1.0.0 github.com/prometheus/common v0.10.0 - github.com/prometheus/prometheus v2.5.0+incompatible // indirect - github.com/prometheus/tsdb v0.10.0 // indirect gopkg.in/alecthomas/kingpin.v2 v2.2.6 gopkg.in/yaml.v2 v2.2.5 ) diff --git a/go.sum b/go.sum index 1712a84..a8533c5 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,3 @@ -github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -8,70 +7,41 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= -github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= -github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= -github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= -github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= -github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.9.0 h1:wDJmvq38kDhkVxi50ni9ykkdUr1PKgqKOoi01fa0Mdk= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0 h1:MP4Eh7ZCb31lleYCFuwm0oe4/YGak+5l1vA2NOE80nA= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= -github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= -github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= -github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= -github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= -github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= -github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= -github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= -github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj2vyii2bbUNDw3kt9VxK2EY= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4= -github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= -github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= -github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0 h1:vrDKnkGzuGvhNAL56c7DBz29ZL+KxnoR0x7enabFceM= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= -github.com/prometheus/client_golang v1.7.0 h1:wCi7urQOGBsYcQROHqpUUX4ct84xp40t9R9JX0FuA/U= -github.com/prometheus/client_golang v1.7.0/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M= @@ -82,22 +52,14 @@ github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB8 github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.2 h1:6LJUbpNm42llc4HRCuvApCSWB/WfhuNo9K98Q9sNGfs= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= -github.com/prometheus/procfs v0.1.3 h1:F0+tqvhOksq22sc6iCHF5WGlWjdwj92p0udFh1VFBS8= -github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= -github.com/prometheus/prometheus v1.8.2 h1:PAL466mnJw1VolZPm1OarpdUpqukUy/eX4tagia17DM= -github.com/prometheus/prometheus v2.5.0+incompatible h1:7QPitgO2kOFG8ecuRn9O/4L9+10He72rVRJvMXrE9Hg= -github.com/prometheus/prometheus v2.5.0+incompatible/go.mod h1:oAIUtOny2rjMX0OWN5vPR5/q/twIROJvdqnQKDdil/s= -github.com/prometheus/tsdb v0.10.0 h1:If5rVCMTp6W2SiRAQFlbpJNgVlgMEd+U2GZckwK38ic= -github.com/prometheus/tsdb v0.10.0/go.mod h1:oi49uRhEe9dPUTlS3JRZOwJuVi6tmh10QSgwXEyGCt4= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= -github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -105,30 +67,17 @@ golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80= -golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= -google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= -google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= -google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= -google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= -google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= -google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 0b04bba..a695517 100644 --- a/main.go +++ b/main.go @@ -32,7 +32,7 @@ var ( BuildDate string src = rand.NewSource(time.Now().UnixNano()) // randomize base string maxRandomSize = 10 // required size of random string - metricsMeta *MetricsMetaList + //metricsMeta *MetricsMetaList ) func RandomString() string { @@ -63,16 +63,17 @@ func containsString(slice []string, item string) bool { return ok } -func saveMetaData() { +func saveMetaData() *MetricsMetaList { l, _ := readTargetsList() - metricsMeta = NewMetricsMetaList(*l) + metricsMeta := NewMetricsMetaList(*l) metricsMeta.onlyForJobs(config.Jobs) metricsMeta.saveList() _ = level.Info(logger).Log("msg", fmt.Sprintf("metrics in meta data %d", len(*metricsMeta))) + return metricsMeta } -func saveData() { +func saveData(metricsMeta *MetricsMetaList) { var wg sync.WaitGroup _ = level.Debug(logger).Log("msg", fmt.Sprintf("start %d goroutines for collect metrics for %d days ", len(*metricsMeta), config.Days)) for _, metrics := range *metricsMeta { @@ -88,18 +89,21 @@ func collectOneMetrics(wg *sync.WaitGroup, metricName string) { defer wg.Done() m := Matrix{} for i := 0; i < config.Days; i++ { - c, err := getRangeDay(metricName+"{}", 1) + c, err := getRangeDay(metricName+"{}", i+1) if err != nil { continue } + l := len(m) for _, series := range *c { - m = append(m, series) + m.appendSeries(series) } + _ = level.Debug(logger).Log("msg", fmt.Sprintf("in metrics %s add new %d series", metricName, len(m)-l)) } if len(m) == 0 { _ = level.Error(logger).Log("msg", "for metrics "+metricName+" not any data") } else { m.save(metricName) + _ = level.Debug(logger).Log("msg", "save data for metrics "+metricName) } } @@ -128,7 +132,6 @@ func main() { fmt.Printf("Program did not start due to configuration error! \r\n\tError: %s", err) os.Exit(1) } - saveMetaData() - saveData() - + m := saveMetaData() + saveData(m) } diff --git a/matrix.go b/matrix.go new file mode 100644 index 0000000..c1300e0 --- /dev/null +++ b/matrix.go @@ -0,0 +1,75 @@ +package main + +import ( + "encoding/json" + "github.com/go-kit/kit/log/level" + "os" + "strings" +) + +type Matrix []Series + +func (m Matrix) String() string { + strs := make([]string, len(m)) + for i, ss := range m { + strs[i] = ss.String() + } + return strings.Join(strs, "\n") +} + +func (m *Matrix) UnmarshalJSON(data []byte) error { + var s []Series + s = []Series{} + err := json.Unmarshal(data, &s) + if err != nil { + return err + } + *m = s + return nil +} + +func (m *Matrix) containsSeries(series Series) bool { + if len(series.Metric) == 0 { + return false + } + contain := false + for _, metric := range *m { + contain = contain || metric.sameMetrics(series) + } + return contain +} + +func (m *Matrix) save(metricsName string) { + name := config.filePath(metricsName + ".json") + f, err := os.Create(name) + if err != nil { + _ = level.Error(logger).Log("msg", "problem create meta data file ", "file", name, "error", err) + return + } + defer func() { _ = f.Close() }() + data, err := json.MarshalIndent(m, "", " ") + if err != nil { + _ = level.Error(logger).Log("msg", "problem create prepare target meta data", "file", name, "error", err) + return + } + _, err = f.Write(data) + if err != nil { + _ = level.Error(logger).Log("msg", "problem write data to file ", "file", name, "error", err) + return + } + _ = level.Debug(logger).Log("msg", "target meta data success write to file ", "file", name) +} + +func (m *Matrix) appendSeries(series Series) { + if series.forJobs(config.Jobs) { + if m.containsSeries(series) { + for i, _ := range *m { + if (*m)[i].sameMetrics(series) { + (*m)[i].Points = append((*m)[i].Points, series.Points...) + } + } + } else { + *m = append(*m, series) + } + } +} diff --git a/matrix_test.go b/matrix_test.go new file mode 100644 index 0000000..06864f5 --- /dev/null +++ b/matrix_test.go @@ -0,0 +1,100 @@ +package main + +import ( + "github.com/prometheus/client_golang/prometheus" + "testing" +) + +const testMatrixSuccess = "[{\"metric\":{\"__name__\":\"cucm_calls_active\",\"instance\":\"localhost:9718\",\"job\":\"cucm_monitor\",\"server\":\"perfcucma.perflab.zoomint.com\"},\"values\":[[1593475200,\"0\"],[1593475210,\"0\"],[1593561600,\"0\"]]},{\"metric\":{\"__name__\":\"cucm_calls_active\",\"instance\":\"localhost:9718\",\"job\":\"cucm_monitor\",\"server\":\"perfcucmb.perflab.zoomint.com\"},\"values\":[[1593475200,\"0\"],[1593475210,\"0\"],[1593561600,\"858\"]]}]" + +func TestMatrix_UnmarshalJSON(t *testing.T) { + type args struct { + data []byte + } + tests := []struct { + name string + m Matrix + args args + wantErr bool + }{ + {"valid data", Matrix{}, args{[]byte(testMatrixSuccess)}, false}, + {"empty", Matrix{}, args{[]byte("")}, true}, + {"empty array", Matrix{}, args{[]byte("[{}]")}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := tt.m.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestMatrix_appendSeries(t *testing.T) { + type args struct { + series Series + } + tests := []struct { + name string + m Matrix + args args + newSeries bool + }{ + {"contains", Matrix{ + Series{prometheus.Labels{"key": "data", "job": "test"}, nil}, + Series{prometheus.Labels{"job": "not there"}, nil}}, args{ + Series{prometheus.Labels{"key": "data", "job": "test"}, nil}, + }, false}, + {"not contains", Matrix{ + Series{prometheus.Labels{"key": "data", "job": "test"}, nil}, + Series{prometheus.Labels{"key": "data", "job": "not there"}, nil}}, args{ + Series{prometheus.Labels{"key": "data", "job": "missing"}, nil}, + }, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + originLen := len(tt.m) + tt.m.appendSeries(tt.args.series) + if tt.newSeries { + if originLen >= len(tt.m) { + t.Errorf("appendSeries() = %d, want %d", len(tt.m), originLen) + } + } else { + if originLen != len(tt.m) { + t.Errorf("appendSeries() = %d, want %d", len(tt.m), originLen) + } + } + }) + } +} + +func TestMatrix_containsSeries(t *testing.T) { + type args struct { + series Series + } + tests := []struct { + name string + m Matrix + args args + want bool + }{ + {"empty array", Matrix{}, args{}, false}, + {"contains", Matrix{ + Series{prometheus.Labels{"key": "data", "job": "test"}, nil}, + Series{prometheus.Labels{"job": "not there"}, nil}}, args{ + Series{prometheus.Labels{"key": "data", "job": "test"}, nil}, + }, true}, + {"not contains", Matrix{ + Series{prometheus.Labels{"key": "data", "job": "test"}, nil}, + Series{prometheus.Labels{"key": "data", "job": "not there"}, nil}}, args{ + Series{prometheus.Labels{"key": "data", "job": "missing"}, nil}, + }, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.m.containsSeries(tt.args.series); got != tt.want { + t.Errorf("containsSeries() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/point.go b/point.go new file mode 100644 index 0000000..b8086ad --- /dev/null +++ b/point.go @@ -0,0 +1,42 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "github.com/prometheus/common/model" + "regexp" + "strconv" +) + +var pointRex = regexp.MustCompile(`\[\s*(\d+\.?\d*)\s*\,\s*\"([^\"]+)\"\s*\]`) + +// Point represents a single data point for a given timestamp. +type Point struct { + T model.Time + V float64 + //U string +} + +func (p Point) String() string { + v := strconv.FormatFloat(p.V, 'f', -1, 64) + return fmt.Sprintf("%v @[%v]", v, p.T) +} + +func (p *Point) UnmarshalJSON(data []byte) error { + s := string(data) + match := pointRex.FindStringSubmatch(s) + if len(match) < 3 { + return errors.New("problem parse point values") + } + err := json.Unmarshal([]byte(match[1]), &p.T) + if err != nil { + return err + } + //p.U = p.T.Time().Format(time.RFC3339) + p.V, err = strconv.ParseFloat(match[2], 64) + if err != nil { + return err + } + return nil +} diff --git a/point_test.go b/point_test.go new file mode 100644 index 0000000..97659ef --- /dev/null +++ b/point_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "github.com/prometheus/common/model" + "testing" +) + +func TestPoint_UnmarshalJSON(t *testing.T) { + type fields struct { + T model.Time + V float64 + } + type args struct { + data []byte + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + {"Empty", fields{}, args{[]byte("")}, true}, + {"Bracket", fields{}, args{[]byte("[]")}, true}, + {"No number", fields{}, args{[]byte("[abc,\"abc\"]")}, true}, + {"Mixed number", fields{}, args{[]byte("[abc12 , \"a12bc\"]")}, true}, + {"Success format", fields{}, args{[]byte("[1593561600,\"10\"]")}, false}, + {"Format with space", fields{}, args{[]byte("[1593561600, \"10\"]")}, false}, + {"Format many spaces", fields{}, args{[]byte(" [ 1593561600 , \"10\" ]")}, false}, + {"Float T", fields{}, args{[]byte(" [ 1593561600.360, \"10\" ]")}, false}, + {"Float V", fields{}, args{[]byte(" [ 1593561600, \"10.1\" ]")}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &Point{ + T: tt.fields.T, + V: tt.fields.V, + } + if err := p.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/query_range.go b/query_range.go index 128fc88..8a3da9a 100644 --- a/query_range.go +++ b/query_range.go @@ -5,89 +5,14 @@ import ( "errors" "fmt" "github.com/go-kit/kit/log/level" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/common/model" - "os" - "regexp" - "strconv" - "strings" "time" ) -var pointRex = regexp.MustCompile(`\[(\d+\.?\d*)\,\"([^\"]+)\"\]`) - -// Point represents a single data point for a given timestamp. -type Point struct { - T model.Time - V float64 - //U string -} - -type Series struct { - Metric prometheus.Labels `json:"metric"` - Points []Point `json:"values"` -} - -type Matrix []Series - -func (p Point) String() string { - v := strconv.FormatFloat(p.V, 'f', -1, 64) - return fmt.Sprintf("%v @[%v]", v, p.T) -} - type Result struct { ResultType string `json:"resultType"` Result Matrix `json:"result"` } -func (p *Point) UnmarshalJSON(data []byte) error { - s := string(data) - match := pointRex.FindStringSubmatch(s) - if len(match) < 3 { - return errors.New("problem parse point values") - } - err := json.Unmarshal([]byte(match[1]), &p.T) - if err != nil { - return err - } - //p.U = p.T.Time().Format(time.RFC3339) - p.V, err = strconv.ParseFloat(match[2], 64) - if err != nil { - return err - } - return nil -} - -func (s Series) String() string { - vals := make([]string, len(s.Points)) - for i, v := range s.Points { - vals[i] = v.String() - } - return fmt.Sprintf("%s =>\n%s", s.Metric, strings.Join(vals, "\n")) -} - -func (m Matrix) String() string { - // TODO(fabxc): sort, or can we rely on order from the querier? - strs := make([]string, len(m)) - - for i, ss := range m { - strs[i] = ss.String() - } - - return strings.Join(strs, "\n") -} - -func (m *Matrix) UnmarshalJSON(data []byte) error { - var s []Series - s = []Series{} - err := json.Unmarshal(data, &s) - if err != nil { - return err - } - *m = s - return nil -} - func UnmarshalResult(data []byte) (*Result, error) { var r Result err := json.Unmarshal(data, &r) @@ -118,24 +43,3 @@ func getRangeDay(job string, dayBack int) (*Matrix, error) { } return &t.Result, nil } - -func (m *Matrix) save(metricsName string) { - name := config.filePath(metricsName + ".json") - f, err := os.Create(name) - if err != nil { - _ = level.Error(logger).Log("msg", "problem create meta data file ", "file", name, "error", err) - return - } - defer func() { _ = f.Close() }() - data, err := json.MarshalIndent(m, "", " ") - if err != nil { - _ = level.Error(logger).Log("msg", "problem create prepare target meta data", "file", name, "error", err) - return - } - _, err = f.Write(data) - if err != nil { - _ = level.Error(logger).Log("msg", "problem write data to file ", "file", name, "error", err) - return - } - _ = level.Debug(logger).Log("msg", "target meta data success write to file ", "file", name) -} diff --git a/series.go b/series.go new file mode 100644 index 0000000..771bafa --- /dev/null +++ b/series.go @@ -0,0 +1,50 @@ +package main + +import ( + "fmt" + "github.com/prometheus/client_golang/prometheus" + "strings" +) + +type Series struct { + Metric prometheus.Labels `json:"metric"` + Points []Point `json:"values"` +} + +func (s Series) String() string { + vals := make([]string, len(s.Points)) + for i, v := range s.Points { + vals[i] = v.String() + } + return fmt.Sprintf("%s =>\n%s", s.Metric, strings.Join(vals, "\n")) +} + +func (s Series) sameMetrics(series Series) bool { + if len(s.Metric) != len(series.Metric) { + return false + } + for key, metric := range series.Metric { + if s.Metric[key] != metric { + return false + } + } + return true +} + +func (s Series) forJob(jobName string) bool { + if len(jobName) == 0 { + return false + } + return s.Metric["job"] == jobName +} + +func (s Series) forJobs(jobs []string) bool { + if len(jobs) == 0 { + return true + } + existName := false + for _, job := range jobs { + existName = existName || s.forJob(job) + } + return existName +} diff --git a/series_test.go b/series_test.go new file mode 100644 index 0000000..a4512dd --- /dev/null +++ b/series_test.go @@ -0,0 +1,114 @@ +package main + +import ( + "github.com/prometheus/client_golang/prometheus" + "testing" +) + +func TestSeries_forJob(t *testing.T) { + type fields struct { + Metric prometheus.Labels + Points []Point + } + type args struct { + jobName string + } + tests := []struct { + name string + fields fields + args args + want bool + }{ + {"for job", fields{Metric: prometheus.Labels{"any": "any", "job": "test_job"}, Points: nil}, args{jobName: "test_job"}, true}, + {"not for job", fields{Metric: prometheus.Labels{"any": "any", "job": "test_job_no"}, Points: nil}, args{jobName: "test_job"}, false}, + {"label not exist", fields{Metric: prometheus.Labels{"any": "any", "job_not": "test_job_no"}, Points: nil}, args{jobName: "test_job"}, false}, + {"label not exist", fields{Metric: prometheus.Labels{"any": "any", "job_not": "test_job_no"}, Points: nil}, args{jobName: "test_job"}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := Series{ + Metric: tt.fields.Metric, + Points: tt.fields.Points, + } + if got := s.forJob(tt.args.jobName); got != tt.want { + t.Errorf("forJob() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestSeries_forJobs(t *testing.T) { + type fields struct { + Metric prometheus.Labels + Points []Point + } + type args struct { + jobs []string + } + tests := []struct { + name string + fields fields + args args + want bool + }{ + {"for job", fields{Metric: prometheus.Labels{"any": "any", "job": "test_job"}, Points: nil}, args{jobs: []string{"test_job", "second"}}, true}, + {"not for job", fields{Metric: prometheus.Labels{"any": "any", "job": "test_job_no"}, Points: nil}, args{jobs: []string{"test_job", "second"}}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := Series{ + Metric: tt.fields.Metric, + Points: tt.fields.Points, + } + if got := s.forJobs(tt.args.jobs); got != tt.want { + t.Errorf("forJobs() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestSeries_sameMetrics(t *testing.T) { + type fields struct { + Metric prometheus.Labels + Points []Point + } + type args struct { + series Series + } + tests := []struct { + name string + fields fields + args args + want bool + }{ + {"same metrics", fields{Metric: prometheus.Labels{"key": "any", "job": "test_job"}, Points: nil}, + args{Series{Metric: prometheus.Labels{"key": "any", "job": "test_job"}}}, true}, + {"different value", fields{Metric: prometheus.Labels{"key": "any", "job": "test_job_no"}, Points: nil}, + args{Series{Metric: prometheus.Labels{"key": "any", "job": "test_job"}}}, false}, + {"different key", fields{Metric: prometheus.Labels{"key_diff": "any", "job": "test_job"}, Points: nil}, + args{Series{Metric: prometheus.Labels{"key": "any", "job": "test_job"}}}, false}, + {"empty source", fields{Metric: prometheus.Labels{"key": "any", "job": "test_job"}, Points: nil}, + args{Series{Metric: prometheus.Labels{}}}, false}, + {"empty test", fields{Metric: prometheus.Labels{}, Points: nil}, + args{Series{Metric: prometheus.Labels{"key": "any", "job": "test_job"}}}, false}, + {"empty both", fields{Metric: prometheus.Labels{}, Points: nil}, + args{Series{Metric: prometheus.Labels{"key": "any", "job": "test_job"}}}, false}, + {"add test", fields{Metric: prometheus.Labels{"key": "any", "job": "test_job", "add": "add"}, Points: nil}, + args{Series{Metric: prometheus.Labels{"key": "any", "job": "test_job"}}}, false}, + {"add source", fields{Metric: prometheus.Labels{"key": "any", "job": "test_job"}, Points: nil}, + args{Series{Metric: prometheus.Labels{"key": "any", "job": "test_job", "add": "add"}}}, false}, + {"add source", fields{Metric: prometheus.Labels{"key": "any", "job": "test_job"}, Points: nil}, + args{Series{Metric: prometheus.Labels{"key": "any", "job": "test_job", "add": "add"}}}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := Series{ + Metric: tt.fields.Metric, + Points: tt.fields.Points, + } + if got := s.sameMetrics(tt.args.series); got != tt.want { + t.Errorf("sameMetrics() = %v, want %v", got, tt.want) + } + }) + } +}