1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
// Package format provides utilities to format metrics and notifications in
// various formats.
package format // import "collectd.org/format"
import (
"context"
"fmt"
"io"
"strings"
"time"
"collectd.org/api"
)
// Putval implements the Writer interface for PUTVAL formatted output.
type Putval struct {
w io.Writer
}
// NewPutval returns a new Putval object writing to the provided io.Writer.
func NewPutval(w io.Writer) *Putval {
return &Putval{
w: w,
}
}
// Write formats the ValueList in the PUTVAL format and writes it to the
// assiciated io.Writer.
func (p *Putval) Write(_ context.Context, vl *api.ValueList) error {
s, err := formatValues(vl)
if err != nil {
return err
}
_, err = fmt.Fprintf(p.w, "PUTVAL %q interval=%.3f %s\n",
vl.Identifier.String(), vl.Interval.Seconds(), s)
return err
}
func formatValues(vl *api.ValueList) (string, error) {
fields := make([]string, 1+len(vl.Values))
fields[0] = formatTime(vl.Time)
for i, v := range vl.Values {
switch v := v.(type) {
case api.Counter:
fields[i+1] = fmt.Sprintf("%d", v)
case api.Gauge:
fields[i+1] = fmt.Sprintf("%.15g", v)
case api.Derive:
fields[i+1] = fmt.Sprintf("%d", v)
default:
return "", fmt.Errorf("unexpected type %T", v)
}
}
return strings.Join(fields, ":"), nil
}
func formatTime(t time.Time) string {
if t.IsZero() {
return "N"
}
return fmt.Sprintf("%.3f", float64(t.UnixNano())/1000000000.0)
}
|