示例#1
0
func (p *PrometheusClient) Write(points []*client.Point) error {
	if len(points) == 0 {
		return nil
	}

	for _, point := range points {
		var labels []string
		key := point.Name()

		for k, _ := range point.Tags() {
			if len(k) > 0 {
				labels = append(labels, k)
			}
		}

		if _, ok := p.metrics[key]; !ok {
			p.metrics[key] = prometheus.NewUntypedVec(
				prometheus.UntypedOpts{
					Name: key,
					Help: fmt.Sprintf("Telegraf collected point '%s'", key),
				},
				labels,
			)
			prometheus.MustRegister(p.metrics[key])
		}

		l := prometheus.Labels{}
		for tk, tv := range point.Tags() {
			l[tk] = tv
		}

		for _, val := range point.Fields() {
			switch val := val.(type) {
			default:
				log.Printf("Prometheus output, unsupported type. key: %s, type: %T\n",
					key, val)
			case int64:
				m, err := p.metrics[key].GetMetricWith(l)
				if err != nil {
					log.Printf("ERROR Getting metric in Prometheus output, "+
						"key: %s, labels: %v,\nerr: %s\n",
						key, l, err.Error())
					continue
				}
				m.Set(float64(val))
			case float64:
				m, err := p.metrics[key].GetMetricWith(l)
				if err != nil {
					log.Printf("ERROR Getting metric in Prometheus output, "+
						"key: %s, labels: %v,\nerr: %s\n",
						key, l, err.Error())
					continue
				}
				m.Set(val)
			}
		}
	}
	return nil
}
示例#2
0
func (p *PrometheusClient) Write(points []*client.Point) error {
	if len(points) == 0 {
		return nil
	}

	for _, point := range points {
		var labels []string
		name := point.Name()
		key := name

		for k, _ := range point.Tags() {
			if len(k) > 0 {
				labels = append(labels, k)
			}
		}

		if _, ok := p.metrics[key]; !ok {
			p.metrics[key] = prometheus.NewUntypedVec(
				prometheus.UntypedOpts{
					Name: key,
					Help: fmt.Sprintf("Telegraf collected point '%s'", name),
				},
				labels,
			)
			prometheus.MustRegister(p.metrics[key])
		}

		l := prometheus.Labels{}
		for tk, tv := range point.Tags() {
			l[tk] = tv
		}

		for _, val := range point.Fields() {
			switch val.(type) {
			case int64:
				ival := val.(int64)
				p.metrics[key].With(l).Set(float64(ival))
			case float64:
				p.metrics[key].With(l).Set(val.(float64))
			}
		}
	}
	return nil
}
示例#3
0
func (c *TailCollector) initalizeMetric(cfg *config.MetricParser) *prometheus.MetricVec {
	// Lock the map to writes (reads should be okay I think?)
	c.mmtx.Lock()
	defer c.mmtx.Unlock()

	// Setup the label set
	labelSet := make([]string, len(cfg.Labels))
	for idx, v := range cfg.Labels {
		labelSet[idx] = v.Name
	}

	var metricVec *prometheus.MetricVec

	switch cfg.Type {
	case config.METRIC_COUNTER:
		metricVec = &prometheus.NewCounterVec(
			prometheus.CounterOpts{
				Name: cfg.Name,
				Help: cfg.Help,
			},
			labelSet,
		).MetricVec

	case config.METRIC_GAUGE:
		metricVec = &prometheus.NewGaugeVec(
			prometheus.GaugeOpts{
				Name: cfg.Name,
				Help: cfg.Help,
			},
			labelSet,
		).MetricVec
	default:
		metricVec = &prometheus.NewUntypedVec(
			prometheus.UntypedOpts{
				Name: cfg.Name,
				Help: cfg.Help,
			},
			labelSet,
		).MetricVec
	}
	c.metrics[cfg.Name] = metricVec
	return metricVec
}
示例#4
0
func (p *PrometheusClient) Write(metrics []telegraf.Metric) error {
	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
			}

			// Create a new metric if it hasn't been created yet.
			if _, ok := p.metrics[mname]; !ok {
				p.metrics[mname] = prometheus.NewUntypedVec(
					prometheus.UntypedOpts{
						Name: mname,
						Help: "Telegraf collected metric",
					},
					labels,
				)
				if err := prometheus.Register(p.metrics[mname]); err != nil {
					log.Printf("prometheus_client: Metric failed to register with prometheus, %s", err)
					continue
				}
			}

			switch val := val.(type) {
			case int64:
				m, err := p.metrics[mname].GetMetricWith(l)
				if err != nil {
					log.Printf("ERROR Getting metric in Prometheus output, "+
						"key: %s, labels: %v,\nerr: %s\n",
						mname, l, err.Error())
					continue
				}
				m.Set(float64(val))
			case float64:
				m, err := p.metrics[mname].GetMetricWith(l)
				if err != nil {
					log.Printf("ERROR Getting metric in Prometheus output, "+
						"key: %s, labels: %v,\nerr: %s\n",
						mname, l, err.Error())
					continue
				}
				m.Set(val)
			default:
				continue
			}
		}
	}
	return nil
}