Skip to content

Commit

Permalink
Add new WithUserPlot Endpoint option
Browse files Browse the repository at this point in the history
  • Loading branch information
arl committed Feb 27, 2023
1 parent 5367793 commit 974c27d
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions statsviz.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,24 @@ type Endpoint struct {
intv time.Duration // interval between consecutive metrics emission
root string // http path root
plots *plot.List // plots shown on the user interface

// user plots
userPlots []plot.UserPlot
}

// NewEndpoint constructs a new Statsviz endpoint, pre-configured with default
// settings or with given options.
func NewEndpoint(opts ...Option) *Endpoint {
e := &Endpoint{
intv: defaultSendInterval,
root: defaultRoot,
plots: plot.NewList(),
intv: defaultSendInterval,
root: defaultRoot,
}

for _, opt := range opts {
opt(e)
}

e.plots = plot.NewList(e.userPlots)
return e
}

Expand All @@ -89,6 +92,21 @@ func WithRoot(path string) Option {
}
}

// WithUserPlot adds a new plot to statsviz user interface tracking an
// user-provided metric. WithUserPlot can be called multiple times.
func WithUserPlot(userPlot UserPlot) Option {
return func(e *Endpoint) {
// Sanity check
if (userPlot.heatmap != nil) == (userPlot.timeseries != nil) {
panic("userplot must be a timeseries or a heatmap")
}
e.userPlots = append(e.userPlots, plot.UserPlot{
Scatter: userPlot.timeseries,
Heatmap: userPlot.heatmap,
})
}
}

// Register registers on the given mux the HTTP handlers required for statsviz
// endpoint.
func (e *Endpoint) Register(mux *http.ServeMux) {
Expand Down

0 comments on commit 974c27d

Please sign in to comment.