Skip to content

Commit f216883

Browse files
authored
Merge pull request #124 from PureStorage-OpenConnect/123-new-metric-label-proposal-add-subscription-info
Add subscription type to purefa_info
2 parents 3e22f06 + 69cf90e commit f216883

File tree

9 files changed

+188
-74
lines changed

9 files changed

+188
-74
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ GOTEST=$(GOCMD) test
44
GOVET=$(GOCMD) vet
55
BINARY_NAME=pure-fa-om-exporter
66
MODULE_NAME=purestorage/fa-openmetrics-exporter
7-
VERSION?=1.0.15
7+
VERSION?=1.0.16
88
SERVICE_PORT?=9490
99
DOCKER_REGISTRY?= quay.io/purestorage/
1010
EXPORT_RESULT?=false # for CI please set EXPORT_RESULT to true

build/docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM golang:alpine as build
2-
ARG VERSION=1.0.15
2+
ARG VERSION=1.0.16
33

44
WORKDIR /usr/src/app
55

internal/openmetrics-exporter/arrays_collector.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package collectors
22

33
import (
4+
client "purestorage/fa-openmetrics-exporter/internal/rest-client"
5+
46
"github.com/prometheus/client_golang/prometheus"
5-
"purestorage/fa-openmetrics-exporter/internal/rest-client"
67
)
78

89
type ArraysCollector struct {
@@ -21,20 +22,33 @@ func (c *ArraysCollector) Collect(ch chan<- prometheus.Metric) {
2122
}
2223
array := arrays.Items[0]
2324

24-
ch <- prometheus.MustNewConstMetric(
25-
c.ArraysDesc,
26-
prometheus.GaugeValue,
27-
1.0,
28-
array.Name, array.Id, array.Os, array.Version,
29-
)
30-
}
25+
subscriptions := c.Client.GetSubscriptions()
26+
if len(subscriptions.Items) == 0 {
27+
28+
ch <- prometheus.MustNewConstMetric(
29+
c.ArraysDesc,
30+
prometheus.GaugeValue,
31+
1.0,
32+
array.Name, array.Os, "", array.Id, array.Version,
33+
)
3134

35+
} else {
36+
subscription := subscriptions.Items[0]
37+
38+
ch <- prometheus.MustNewConstMetric(
39+
c.ArraysDesc,
40+
prometheus.GaugeValue,
41+
1.0,
42+
array.Name, array.Os, subscription.Service, array.Id, array.Version,
43+
)
44+
}
45+
}
3246
func NewArraysCollector(fa *client.FAClient) *ArraysCollector {
3347
return &ArraysCollector{
3448
ArraysDesc: prometheus.NewDesc(
3549
"purefa_info",
3650
"FlashArray system information",
37-
[]string{"array_name", "system_id", "os", "version"},
51+
[]string{"array_name", "os", "subscription_type", "system_id", "version"},
3852
prometheus.Labels{},
3953
),
4054
Client: fa,
Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,56 @@
11
package collectors
22

3-
43
import (
4+
"encoding/json"
55
"fmt"
6-
"testing"
7-
"regexp"
8-
"strings"
96
"net/http"
107
"net/http/httptest"
11-
"encoding/json"
12-
"os"
8+
"os"
9+
"regexp"
10+
"strings"
11+
"testing"
1312

14-
"purestorage/fa-openmetrics-exporter/internal/rest-client"
13+
client "purestorage/fa-openmetrics-exporter/internal/rest-client"
1514
)
1615

1716
func TestArraysCollector(t *testing.T) {
1817

1918
res, _ := os.ReadFile("../../test/data/arrays.json")
19+
ressub, _ := os.ReadFile("../../test/data/subscriptions.json")
2020
vers, _ := os.ReadFile("../../test/data/versions.json")
21-
var arrs client.ArraysList
22-
json.Unmarshal(res, &arrs)
21+
var arrs client.ArraysList
22+
var subs client.SubscriptionsList
23+
json.Unmarshal(res, &arrs)
24+
json.Unmarshal(ressub, &subs)
2325
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
24-
valid := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/arrays$`)
25-
if r.URL.Path == "/api/api_version" {
26-
w.Header().Set("Content-Type", "application/json")
27-
w.WriteHeader(http.StatusOK)
28-
w.Write([]byte(vers))
29-
} else if valid.MatchString(r.URL.Path) {
26+
urlarr := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/arrays$`)
27+
urlsubs := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/subscriptions$`)
28+
if r.URL.Path == "/api/api_version" {
29+
w.Header().Set("Content-Type", "application/json")
30+
w.WriteHeader(http.StatusOK)
31+
w.Write([]byte(vers))
32+
} else if urlarr.MatchString(r.URL.Path) {
3033
w.Header().Set("x-auth-token", "faketoken")
3134
w.Header().Set("Content-Type", "application/json")
3235
w.WriteHeader(http.StatusOK)
3336
w.Write([]byte(res))
37+
} else if urlsubs.MatchString(r.URL.Path) {
38+
w.Header().Set("x-auth-token", "faketoken")
39+
w.Header().Set("Content-Type", "application/json")
40+
w.WriteHeader(http.StatusOK)
41+
w.Write([]byte(ressub))
3442
}
35-
}))
36-
endp := strings.Split(server.URL, "/")
37-
e := endp[len(endp)-1]
43+
}))
44+
endp := strings.Split(server.URL, "/")
45+
e := endp[len(endp)-1]
46+
a := arrs.Items[0]
47+
s := subs.Items[0]
3848
want := make(map[string]bool)
39-
for _, a := range arrs.Items {
40-
want[fmt.Sprintf("label:{name:\"array_name\" value:\"%s\"} label:{name:\"os\" value:\"%s\"} label:{name:\"system_id\" value:\"%s\"} label:{name:\"version\" value:\"%s\"} gauge:{value:1}", a.Name, a.Os, a.Id, a.Version)] = true
41-
}
42-
defer server.Close()
49+
50+
want[fmt.Sprintf("label:{name:\"array_name\" value:\"%s\"} label:{name:\"os\" value:\"%s\"} label:{name:\"subscription_type\" value:\"%s\"} label:{name:\"system_id\" value:\"%s\"} label:{name:\"version\" value:\"%s\"} gauge:{value:1}", a.Name, a.Os, s.Service, a.Id, a.Version)] = true
51+
4352
c := client.NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
44-
ac := NewArraysCollector(c)
53+
ac := NewArraysCollector(c)
4554
metricsCheck(t, ac, want)
55+
server.Close()
4656
}
Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package client
22

3-
43
import (
5-
"testing"
6-
"regexp"
7-
"strings"
4+
"encoding/json"
85
"net/http"
96
"net/http/httptest"
10-
"encoding/json"
11-
"os"
7+
"os"
8+
"regexp"
9+
"strings"
10+
"testing"
1211

1312
"github.com/google/go-cmp/cmp"
1413
)
@@ -17,29 +16,29 @@ func TestArrays(t *testing.T) {
1716

1817
res, _ := os.ReadFile("../../test/data/arrays.json")
1918
vers, _ := os.ReadFile("../../test/data/versions.json")
20-
var arrs ArraysList
21-
json.Unmarshal(res, &arrs)
19+
var arrs ArraysList
20+
json.Unmarshal(res, &arrs)
2221
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
23-
valid := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/arrays$`)
24-
if r.URL.Path == "/api/api_version" {
25-
w.Header().Set("Content-Type", "application/json")
26-
w.WriteHeader(http.StatusOK)
27-
w.Write([]byte(vers))
28-
} else if valid.MatchString(r.URL.Path) {
22+
valid := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/arrays$`)
23+
if r.URL.Path == "/api/api_version" {
24+
w.Header().Set("Content-Type", "application/json")
25+
w.WriteHeader(http.StatusOK)
26+
w.Write([]byte(vers))
27+
} else if valid.MatchString(r.URL.Path) {
2928
w.Header().Set("x-auth-token", "faketoken")
3029
w.Header().Set("Content-Type", "application/json")
3130
w.WriteHeader(http.StatusOK)
3231
w.Write([]byte(res))
3332
}
34-
}))
35-
endp := strings.Split(server.URL, "/")
36-
e := endp[len(endp)-1]
37-
t.Run("arrays_1", func(t *testing.T) {
38-
defer server.Close()
39-
c := NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
40-
al := c.GetArrays()
41-
if diff := cmp.Diff(al.Items[0], arrs.Items[0]); diff != "" {
42-
t.Errorf("Mismatch (-want +got):\n%s", diff)
43-
}
44-
})
33+
}))
34+
endp := strings.Split(server.URL, "/")
35+
e := endp[len(endp)-1]
36+
t.Run("arrays_1", func(t *testing.T) {
37+
defer server.Close()
38+
c := NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
39+
al := c.GetArrays()
40+
if diff := cmp.Diff(al.Items[0], arrs.Items[0]); diff != "" {
41+
t.Errorf("Mismatch (-want +got):\n%s", diff)
42+
}
43+
})
4544
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package client
2+
3+
type Subscription struct {
4+
Id string `json:"id"`
5+
Service string `json:"service"`
6+
}
7+
8+
type SubscriptionsList struct {
9+
ContinuationToken string `json:"continuation_token"`
10+
TotalItemCount int32 `json:"total_item_count"`
11+
MoreItemsRemaining bool `json:"more_items_remaining"`
12+
Items []Subscription `json:"items"`
13+
}
14+
15+
func (fa *FAClient) GetSubscriptions() *SubscriptionsList {
16+
uri := "/subscriptions"
17+
result := new(SubscriptionsList)
18+
res, err := fa.RestClient.R().
19+
SetResult(&result).
20+
Get(uri)
21+
if err != nil {
22+
fa.Error = err
23+
}
24+
if res.StatusCode() == 401 {
25+
fa.RefreshSession()
26+
}
27+
res, err = fa.RestClient.R().
28+
SetResult(&result).
29+
Get(uri)
30+
if err != nil {
31+
fa.Error = err
32+
}
33+
34+
return result
35+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package client
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"os"
8+
"regexp"
9+
"strings"
10+
"testing"
11+
12+
"github.com/google/go-cmp/cmp"
13+
)
14+
15+
func TestSubscriptions(t *testing.T) {
16+
17+
res, _ := os.ReadFile("../../test/data/subscriptions.json")
18+
vers, _ := os.ReadFile("../../test/data/versions.json")
19+
var subs SubscriptionsList
20+
json.Unmarshal(res, &subs)
21+
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
22+
valid := regexp.MustCompile(`^/api/([0-9]+.[0-9]+)?/subscriptions$`)
23+
if r.URL.Path == "/api/api_version" {
24+
w.Header().Set("Content-Type", "application/json")
25+
w.WriteHeader(http.StatusOK)
26+
w.Write([]byte(vers))
27+
} else if valid.MatchString(r.URL.Path) {
28+
w.Header().Set("x-auth-token", "faketoken")
29+
w.Header().Set("Content-Type", "application/json")
30+
w.WriteHeader(http.StatusOK)
31+
w.Write([]byte(res))
32+
}
33+
}))
34+
endp := strings.Split(server.URL, "/")
35+
e := endp[len(endp)-1]
36+
t.Run("subscriptions_1", func(t *testing.T) {
37+
defer server.Close()
38+
c := NewRestClient(e, "fake-api-token", "latest", "test-user-agent-string", false)
39+
vpl := c.GetSubscriptions()
40+
if diff := cmp.Diff(vpl.Items, subs.Items); diff != "" {
41+
t.Errorf("Mismatch (-want +got):\n%s", diff)
42+
}
43+
})
44+
}

0 commit comments

Comments
 (0)