Skip to content

Commit

Permalink
Refactor stats for v14
Browse files Browse the repository at this point in the history
  • Loading branch information
gidsi committed Dec 9, 2020
1 parent d80a488 commit 843457d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
1 change: 1 addition & 0 deletions collector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/spaceapi/directory-api/collector
go 1.12

require (
github.com/codingsince1985/geo-golang v1.6.1
github.com/felixge/httpsnoop v1.0.1
github.com/golang/protobuf v1.3.5 // indirect
github.com/prometheus/client_golang v1.3.0
Expand Down
3 changes: 3 additions & 0 deletions collector/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ 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/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/codingsince1985/geo-golang v1.6.1 h1:dqKTgt7YgNuux1TYSV/xXftyN9KEhs600PPr6tFGC98=
github.com/codingsince1985/geo-golang v1.6.1/go.mod h1:kBEFPG1vFhk0BqA38LyzoZp3VsvgkVtXN9JqZZHAZw4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
Expand Down Expand Up @@ -41,6 +43,7 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
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/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
54 changes: 37 additions & 17 deletions collector/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@ var (
Name: "spaceapi_field",
Help: "Fields used from the spec",
},
[]string{"version", "route", "field"},
[]string{"field"},
)
spaceVersionGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "spaceapi_version",
Help: "Versions used in the directory",
},
[]string{"version"},
)
spaceCountryGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "spaceapi_country",
Help: "Countries spaces are from",
},
[]string{"country", "route"},
[]string{"country"},
)
httpRequestSummary = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Expand All @@ -39,6 +46,7 @@ var (

func init() {
latLonCountry = make(map[float64]map[float64]string)
prometheus.MustRegister(spaceVersionGauge)
prometheus.MustRegister(spaceFieldGauge)
prometheus.MustRegister(spaceCountryGauge)
prometheus.MustRegister(httpRequestSummary)
Expand All @@ -64,7 +72,7 @@ func generateCountryStatistics(entries map[string]entry) {

countryCode, err := getCountryCodeForLatLong(latVal.Interface().(float64), lonVal.Interface().(float64))
if err == nil {
spaceCountryGauge.With(prometheus.Labels{"country": countryCode, "route": value.Url}).Inc()
spaceCountryGauge.With(prometheus.Labels{"country": countryCode}).Inc()
} else {
log.Printf("%v\n", err)
}
Expand Down Expand Up @@ -93,24 +101,24 @@ func getCountryCodeForLatLong(lat, long float64) (string, error) {
}

func generateFieldStatistic(jsonArray map[string]entry) {
newStats := make(map[string]map[string][]string)
newStats := make(map[string][]string)

spaceVersionGauge.Reset()
for _, value := range jsonArray {
apiVersion, fields, err := getNewStats(value.Data)
apiVersions, fields, err := getNewStats(value.Data)
if err == nil {
if _, ok := newStats[value.Url]; !ok {
newStats[value.Url] = make(map[string][]string)
}
newStats[value.Url] = fields

newStats[value.Url][apiVersion] = fields
for _, version := range apiVersions {
spaceVersionGauge.With(prometheus.Labels{"version": version}).Inc()
}
}
}

spaceFieldGauge.Reset()
for spaceName, value := range newStats {
for version, fields := range value {
for _, field := range fields {
spaceFieldGauge.With(prometheus.Labels{"version": version, "route": spaceName, "field": field}).Set(1)
}
for _, fields := range newStats {
for _, field := range fields {
spaceFieldGauge.With(prometheus.Labels{"field": field}).Inc()
}
}
}
Expand All @@ -123,14 +131,26 @@ func statisticMiddelware(inner http.Handler) http.Handler {
return http.HandlerFunc(mw)
}

func getNewStats(value interface{}) (string, []string, error) {
func getNewStats(value interface{}) ([]string, []string, error) {
castedValue := value.(map[string]interface{})

var versions []string
apiVersion, ok := castedValue["api"].(string)
if ok {
versions = append(versions, apiVersion)
}

apiCompatibility, ok := castedValue["api_compatibility"].([]interface{})
if ok {
return apiVersion, flatten(castedValue, ""), nil
for _, version := range apiCompatibility {
versions = append(versions, version.(string))
}
}

if len(versions) > 0 {
return versions, flatten(castedValue, ""), nil
} else {
return "", nil, errors.New("api or space doesn't exist")
return []string{}, nil, errors.New("api or space doesn't exist")
}
}

Expand Down

0 comments on commit 843457d

Please sign in to comment.