func newMetric(m *metrics.Metric, l *metrics.LabelSet) (prometheus.Metric, error) { var value float64 var valueType prometheus.ValueType switch m.Kind { case metrics.Counter: valueType = prometheus.CounterValue value = float64(l.Datum.Get()) case metrics.Gauge: valueType = prometheus.GaugeValue value = float64(l.Datum.Get()) } return prometheus.NewConstMetric(newDesc(m, l), valueType, value) }
// newMetric converts one data source of a value list to a Prometheus metric. func newMetric(vl api.ValueList, index int) (prometheus.Metric, error) { var value float64 var valueType prometheus.ValueType switch v := vl.Values[index].(type) { case api.Gauge: value = float64(v) valueType = prometheus.GaugeValue case api.Derive: value = float64(v) valueType = prometheus.CounterValue case api.Counter: value = float64(v) valueType = prometheus.CounterValue default: return nil, fmt.Errorf("unknown value type: %T", v) } return prometheus.NewConstMetric(newDesc(vl, index), valueType, value) }
func (p *PrometheusClient) Write(metrics []telegraf.Metric) error { p.Lock() defer p.Unlock() if len(metrics) == 0 { return nil } for _, point := range metrics { key := point.Name() key = invalidNameCharRE.ReplaceAllString(key, "_") // convert tags into prometheus labels var labels []string l := prometheus.Labels{} for k, v := range point.Tags() { k = invalidNameCharRE.ReplaceAllString(k, "_") if len(k) == 0 { continue } labels = append(labels, k) l[k] = v } // Get a type if it's available, defaulting to Untyped var mType prometheus.ValueType switch point.Type() { case telegraf.Counter: mType = prometheus.CounterValue case telegraf.Gauge: mType = prometheus.GaugeValue default: mType = prometheus.UntypedValue } for n, val := range point.Fields() { // Ignore string and bool fields. switch val.(type) { case string: continue case bool: continue } // sanitize the measurement name n = invalidNameCharRE.ReplaceAllString(n, "_") var mname string if n == "value" { mname = key } else { mname = fmt.Sprintf("%s_%s", key, n) } desc := prometheus.NewDesc(mname, "Telegraf collected metric", nil, l) var metric prometheus.Metric var err error // switch for field type switch val := val.(type) { case int64: metric, err = prometheus.NewConstMetric(desc, mType, float64(val)) case float64: metric, err = prometheus.NewConstMetric(desc, mType, val) default: continue } if err != nil { log.Printf("E! Error creating prometheus metric, "+ "key: %s, labels: %v,\nerr: %s\n", mname, l, err.Error()) } p.metrics[desc.String()] = metric } } return nil }
func (p *PrometheusClient) Write(metrics []telegraf.Metric) error { p.Lock() defer p.Unlock() p.metrics = make(map[string]prometheus.Metric) if len(metrics) == 0 { return nil } for _, point := range metrics { key := point.Name() key = sanitizedChars.Replace(key) var labels []string l := prometheus.Labels{} for k, v := range point.Tags() { k = sanitizedChars.Replace(k) if len(k) == 0 { continue } if !labelName.MatchString(k) { continue } labels = append(labels, k) l[k] = v } for n, val := range point.Fields() { // Ignore string and bool fields. switch val.(type) { case string: continue case bool: continue } // sanitize the measurement name n = sanitizedChars.Replace(n) var mname string if n == "value" { mname = key } else { mname = fmt.Sprintf("%s_%s", key, n) } // verify that it is a valid measurement name if !metricName.MatchString(mname) { continue } desc := prometheus.NewDesc(mname, "Telegraf collected metric", nil, l) var metric prometheus.Metric var err error switch val := val.(type) { case int64: metric, err = prometheus.NewConstMetric(desc, prometheus.UntypedValue, float64(val)) case float64: metric, err = prometheus.NewConstMetric(desc, prometheus.UntypedValue, val) default: continue } if err != nil { log.Printf("ERROR creating prometheus metric, "+ "key: %s, labels: %v,\nerr: %s\n", mname, l, err.Error()) } p.metrics[desc.String()] = metric } } return nil }