Skip to content

Commit

Permalink
Merge pull request #73 from opsdis/issue_72
Browse files Browse the repository at this point in the history
Issue 72
  • Loading branch information
thenodon authored Nov 14, 2024
2 parents 8eef4f6 + deb7ee8 commit a22e10f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ to scrape.

## Service discovery
The service discovery is exposed on the `/sd` endpoint where the query parameter `target` is the name of fabric in the
`config.yml` file, e.g. `'http://localhost:9643/sd?fabric=xyz'`. The output can look like this:
`config.yml` file, e.g. `'http://localhost:9643/sd?target=xyz'`. The output can look like this:

```json
....
Expand Down
9 changes: 8 additions & 1 deletion aci-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,15 @@ func (p aciAPI) CollectMetrics() (string, []MetricDefinition, error) {
}

end := time.Since(start)

if metrics == nil {
// if no metrics are returned the apic or node may be down
metrics = append(metrics, *p.up(0.0))
} else {
metrics = append(metrics, *p.up(1.0))
}

metrics = append(metrics, *p.scrape(end.Seconds()))
metrics = append(metrics, *p.up(1.0))
log.WithFields(log.Fields{
LogFieldRequestID: p.ctx.Value(LogFieldRequestID),
LogFieldExecTime: end.Microseconds(),
Expand Down
30 changes: 30 additions & 0 deletions aci-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"net/http"

mapset "github.com/deckarep/golang-set/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -77,6 +78,7 @@ func isFlagPassed(name string) bool {
}

var version = "undefined"
var querySet = mapset.NewSet[string]()

func main() {

Expand Down Expand Up @@ -248,6 +250,9 @@ func main() {
GroupClassQueries: queries.GroupClassQueries,
}

// Create a set of all query names - used to validate the query parameter
createQueryNameSet(allQueries)

// Init all fabrics
allFabrics := make(map[string]*Fabric)

Expand Down Expand Up @@ -341,6 +346,19 @@ func main() {
log.Fatal(s.ListenAndServe())
}

func createQueryNameSet(allQueries AllQueries) {
for queryName, _ := range allQueries.ClassQueries {
querySet.Add(queryName)
}
for queryName, _ := range allQueries.CompoundClassQueries {
querySet.Add(queryName)
}
for queryName, _ := range allQueries.GroupClassQueries {
querySet.Add(queryName)
}
querySet.Add("faults")
}

func readConfigDirectory(configDirName *string, dirPath string, queries *AllQueries) {
configDir := filepath.Join(dirPath, *configDirName)
_, err := os.Stat(configDir)
Expand Down Expand Up @@ -530,6 +548,18 @@ func (h HandlerInit) getMonitorMetrics(w http.ResponseWriter, r *http.Request) {
// If the queries query parameter include a comma, split it and add to the queries array
querySplit := strings.Split(queryString, ",")
for _, query := range querySplit {
// Validate that the query is a valid query
if !querySet.Contains(query) {
w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
w.Header().Set("Content-Length", "0")
log.WithFields(log.Fields{
LogFieldFabric: fabric,
"query": query,
}).Warning("not a valid query")
lrw := loggingResponseWriter{ResponseWriter: w}
lrw.WriteHeader(400)
return
}
queries = append(queries, strings.TrimSpace(query))
}
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/fsnotify/fsnotify v1.4.7 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
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/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
Expand Down

0 comments on commit a22e10f

Please sign in to comment.