Example #1
0
// MarshalJSON implements the "encoding/json".Marshaler interface for
// ValueList.
func (vl *ValueList) MarshalJSON() ([]byte, error) {
	jvl := jsonValueList{
		Values:         make([]json.Number, len(vl.Values)),
		DSTypes:        make([]string, len(vl.Values)),
		DSNames:        make([]string, len(vl.Values)),
		Time:           cdtime.New(vl.Time),
		Interval:       cdtime.NewDuration(vl.Interval),
		Host:           vl.Host,
		Plugin:         vl.Plugin,
		PluginInstance: vl.PluginInstance,
		Type:           vl.Type,
		TypeInstance:   vl.TypeInstance,
	}

	for i, v := range vl.Values {
		switch v := v.(type) {
		case Gauge:
			jvl.Values[i] = json.Number(fmt.Sprintf("%.15g", v))
		case Derive:
			jvl.Values[i] = json.Number(fmt.Sprintf("%d", v))
		case Counter:
			jvl.Values[i] = json.Number(fmt.Sprintf("%d", v))
		default:
			return nil, fmt.Errorf("unexpected data source type: %T", v)
		}
		jvl.DSTypes[i] = v.Type()
		jvl.DSNames[i] = vl.DSName(i)
	}

	return json.Marshal(jvl)
}
Example #2
0
func (b *Buffer) writeInterval(d time.Duration) error {
	if b.state.Interval == d {
		return nil
	}
	b.state.Interval = d

	return b.writeInt(typeIntervalHR, uint64(cdtime.NewDuration(d)))
}