// newName converts one data source of a value list to a string representation. func newName(vl api.ValueList, index int) string { var name string if vl.Plugin == vl.Type { name = "collectd_" + vl.Type } else { name = "collectd_" + vl.Plugin + "_" + vl.Type } if dsname := vl.DSName(index); dsname != "value" { name += "_" + dsname } switch vl.Values[index].(type) { case api.Counter: name += "_total" } return name }
// Unmarshal translates a ValueList into InfluxDB data points. func (s *Service) UnmarshalValueList(vl *api.ValueList) []models.Point { timestamp := vl.Time.UTC() var points []models.Point for i := range vl.Values { var name string name = fmt.Sprintf("%s_%s", vl.Identifier.Plugin, vl.DSName(i)) tags := make(map[string]string) fields := make(map[string]interface{}) // Convert interface back to actual type, then to float64 switch value := vl.Values[i].(type) { case api.Gauge: fields["value"] = float64(value) case api.Derive: fields["value"] = float64(value) case api.Counter: fields["value"] = float64(value) } if vl.Identifier.Host != "" { tags["host"] = vl.Identifier.Host } if vl.Identifier.PluginInstance != "" { tags["instance"] = vl.Identifier.PluginInstance } if vl.Identifier.Type != "" { tags["type"] = vl.Identifier.Type } if vl.Identifier.TypeInstance != "" { tags["type_instance"] = vl.Identifier.TypeInstance } // Drop invalid points p, err := models.NewPoint(name, models.NewTags(tags), fields, timestamp) if err != nil { s.Logger.Info(fmt.Sprintf("Dropping point %v: %v", name, err)) atomic.AddInt64(&s.stats.InvalidDroppedPoints, 1) continue } points = append(points, p) } return points }
func parseTime(partType uint16, payload []byte, state *api.ValueList) error { v, err := parseInt(payload) if err != nil { return err } switch partType { case typeInterval: state.Interval = time.Duration(v) * time.Second case typeIntervalHR: state.Interval = cdtime.Time(v).Duration() case typeTime: state.Time = time.Unix(int64(v), 0) case typeTimeHR: state.Time = cdtime.Time(v).Time() } return nil }
// Write formats the ValueList in the PUTVAL format and writes it to the // assiciated io.Writer. func (g *Graphite) Write(vl api.ValueList) error { for i, v := range vl.Values { dsName := "" if g.AlwaysAppendDS || len(vl.Values) != 1 { dsName = vl.DSName(i) } name := g.formatName(vl.Identifier, dsName) val, err := g.formatValue(v) if err != nil { return err } t := vl.Time if t.IsZero() { t = time.Now() } fmt.Fprintf(g.W, "%s %s %d\r\n", name, val, t.Unix()) } return nil }
// newDesc converts one data source of a value list to a Prometheus description. func newDesc(vl api.ValueList, index int) *prometheus.Desc { help := fmt.Sprintf("Collectd exporter: '%s' Type: '%s' Dstype: '%T' Dsname: '%s'", vl.Plugin, vl.Type, vl.Values[index], vl.DSName(index)) return prometheus.NewDesc(newName(vl, index), help, []string{}, newLabels(vl)) }