Skip to content

Commit

Permalink
introduce a cheap, sparse variant of service status
Browse files Browse the repository at this point in the history
The UI needs to get hold of the service status _a lot_ and most of the
time only cares about "is it connected or not" rather than counts of
probes, etc. As of weaveworks/scope#2983, the former is a lot cheaper
to obtain than the latter.
  • Loading branch information
rade committed Dec 14, 2017
1 parent 0aba62a commit 8d9ab91
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions users/api/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ func (a *API) getOrgServiceStatus(currentUser *users.User, w http.ResponseWriter
return
}
r = r.WithContext(user.InjectOrgID(r.Context(), org.ID))

status := a.getServiceStatus(r.Context())
r.ParseForm()
_, sparse := r.Form["sparse"]
status := a.getServiceStatus(r.Context(), sparse)
connected := (status.flux.Fluxd.Connected ||
status.scope.NumberOfProbes > 0 ||
status.prom.NumberOfMetrics > 0 ||
Expand Down Expand Up @@ -136,7 +137,7 @@ type serviceStatus struct {
net netStatus
}

func (a *API) getServiceStatus(ctx context.Context) serviceStatus {
func (a *API) getServiceStatus(ctx context.Context, sparse bool) serviceStatus {
var flux fluxStatus
var scope scopeStatus
var prom promStatus
Expand Down Expand Up @@ -168,21 +169,39 @@ func (a *API) getServiceStatus(ctx context.Context) serviceStatus {
go func() {
defer wg.Done()

resp, err := doRequest(ctx, "scope", a.scopeProbesAPI)
api := a.scopeProbesAPI
if sparse {
api = api + "?sparse"
}
resp, err := doRequest(ctx, "scope", api)
if err != nil {
scope.Error = err.Error()
return
}
defer resp.Body.Close()

var probes []interface{}
err = json.NewDecoder(resp.Body).Decode(&probes)
if sparse {
var hasProbes bool
err = json.NewDecoder(resp.Body).Decode(&hasProbes)
if hasProbes {
// We fake this. The "correct" approach would be to
// have a getOrgServiceStatusViewSparse structure,
// that only contains HasX instead of NumberOfX for
// scope/prom/net. But that entails an enormous amount
// of code duplication for little gain, and
// complicates the client end too.
scope.NumberOfProbes = 1
}
} else {
err = json.NewDecoder(resp.Body).Decode(&probes)
scope.NumberOfProbes = len(probes)
}
if err != nil {
scope.Error = "Could not decode scope data"
logging.With(ctx).Errorf("Could not decode scope data: %s", err)
return
}
scope.NumberOfProbes = len(probes)
}()

// Get prom status.
Expand Down

0 comments on commit 8d9ab91

Please sign in to comment.