Skip to content

Commit

Permalink
fix(statuspage): improve display of next profile (opensearch-project#474
Browse files Browse the repository at this point in the history
)

* fix: improve statuspage display of next profile

* fix: improve Download Next Pprof

* fix: fix profileListener.NextMatchingProfile()
  • Loading branch information
maxbrunet authored Jun 21, 2022
1 parent 001eb8f commit 05617ec
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 40 deletions.
35 changes: 21 additions & 14 deletions cmd/parca-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,12 @@ func main() {
q.Add("query", labelSet.String())

statusPage.ActiveProfilers = append(statusPage.ActiveProfilers, template.ActiveProfiler{
Type: profileType,
Labels: labelSet,
LastTakenAgo: time.Since(profiler.LastSuccessfulProfileStartedAt()),
Error: profiler.LastError(),
Link: fmt.Sprintf("/query?%s", q.Encode()),
Type: profileType,
Labels: labelSet,
Interval: flags.ProfilingDuration,
NextStartedAgo: time.Since(profiler.NextProfileStartedAt()),
Error: profiler.LastError(),
Link: fmt.Sprintf("/query?%s", q.Encode()),
})
}
}
Expand Down Expand Up @@ -294,20 +295,21 @@ func main() {
return
}

// We profile every 10 seconds so leaving 1s wiggle room. If after
// 11s no profile has matched, then there is very likely no
// We profile every ProfilingDuration so leaving 1s wiggle room. If after
// ProfilingDuration+1s no profile has matched, then there is very likely no
// profiler running that matches the label-set.
ctx, cancel := context.WithTimeout(ctx, time.Second*11)
timeout := flags.ProfilingDuration + time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

profile, err := profileListener.NextMatchingProfile(ctx, matchers)
if profile == nil || errors.Is(err, context.Canceled) {
http.Error(w,
"No profile taken in the last 11 seconds that matches the requested label-matchers query. "+
"Profiles are taken every 10 seconds so either the profiler matching the label-set has stopped profiling, "+
http.Error(w, fmt.Sprintf(
"No profile taken in the last %s that matches the requested label-matchers query. "+
"Profiles are taken every %s so either the profiler matching the label-set has stopped profiling, "+
"or the label-set was incorrect.",
http.StatusNotFound,
)
timeout, flags.ProfilingDuration,
), http.StatusNotFound)
return
}
if err != nil {
Expand All @@ -321,7 +323,12 @@ func main() {
q := url.Values{}
q.Add("query", query)

fmt.Fprintf(w, "<p><a href='/query?%s'>Download Pprof</a></p>\n", q.Encode())
fmt.Fprintf(
w,
"<p><a title='May take up %s to retrieve' href='/query?%s'>Download Next Pprof</a></p>\n",
flags.ProfilingDuration,
q.Encode(),
)
fmt.Fprint(w, "<code><pre>\n")
fmt.Fprint(w, profile.String())
fmt.Fprint(w, "\n</pre></code>")
Expand Down
3 changes: 2 additions & 1 deletion pkg/agent/profile_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func (l *profileListener) NextMatchingProfile(ctx context.Context, matchers []*l
o := l.observe(func(r *profilestorepb.WriteRawRequest) {
var searchedSeries *profilestorepb.RawProfileSeries

seriesloop:
for _, series := range r.Series {
profileLabels := model.LabelSet{}

Expand All @@ -104,7 +105,7 @@ func (l *profileListener) NextMatchingProfile(ctx context.Context, matchers []*l
for _, matcher := range matchers {
labelValue := profileLabels[model.LabelName(matcher.Name)]
if !matcher.Matches(string(labelValue)) {
continue
continue seriesloop
}
}
searchedSeries = series
Expand Down
18 changes: 9 additions & 9 deletions pkg/template/statuspage.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,37 @@
<tr>
<th>Profile Type</th>
<th>Labels</th>
<th>Last Profile Taken</th>
<th>Next Profile Started</th>
<th>Error</th>
<th>Show Profile</th>
<th>Show Next Profile</th>
</tr>
{{range $profiler := .ActiveProfilers}}
{{- range $profiler := .ActiveProfilers }}
<tr>
<td>
{{ .Type }}
</td>
<td>
{{range $label := .Labels}}
{{- range $label := .Labels }}
<span class='label'>{{.Name}}="{{.Value}}"</span>
{{end}}
{{- end }}
</td>
<td>
{{ .LastTakenAgo }} ago
{{ .NextStartedAgo }} ago
</td>
<td>
{{ .Error }}
</td>
<td>
<a href='{{ .Link }}'>Show Profile</a>
<a title='May take up to {{ .Interval }} to display' href='{{ .Link }}'>Show Next Profile</a>
</td>
</tr>
{{else}}
{{- else }}
<tr>
<td>
No active profilers
</td>
</tr>
{{end}}
{{- end }}
</table>
</div>
<div>
Expand Down
11 changes: 6 additions & 5 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ var StatusPageTemplateBytes []byte
var StatusPageTemplate = template.Must(template.New("statuspage").Parse(string(StatusPageTemplateBytes)))

type ActiveProfiler struct {
Type string
Labels labels.Labels
LastTakenAgo time.Duration
Error error
Link string
Type string
Labels labels.Labels
Interval time.Duration
NextStartedAgo time.Duration
Error error
Link string
}

type StatusPage struct {
Expand Down
7 changes: 4 additions & 3 deletions pkg/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ func TestStatusPageTemplate(t *testing.T) {
Name: "name2",
Value: "value2",
}},
LastTakenAgo: time.Second * 3,
Error: errors.New("test"),
Link: "/test123",
Interval: time.Second * 10,
NextStartedAgo: time.Second * 3,
Error: errors.New("test"),
Link: "/test123",
}},
})
require.NoError(t, err)
Expand Down
11 changes: 3 additions & 8 deletions pkg/template/testdata/statuspage.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,17 @@
<tr>
<th>Profile Type</th>
<th>Labels</th>
<th>Last Profile Taken</th>
<th>Next Profile Started</th>
<th>Error</th>
<th>Show Profile</th>
<th>Show Next Profile</th>
</tr>

<tr>
<td>
test_profile_type
</td>
<td>

<span class='label'>name1="value1"</span>

<span class='label'>name2="value2"</span>

</td>
<td>
3s ago
Expand All @@ -78,10 +74,9 @@
test
</td>
<td>
<a href='/test123'>Show Profile</a>
<a title='May take up to 10s to display' href='/test123'>Show Next Profile</a>
</td>
</tr>

</table>
</div>
<div>
Expand Down

0 comments on commit 05617ec

Please sign in to comment.