Exemplo n.º 1
0
func TestRegisterWithOrGet(t *testing.T) {
	// Replace the default registerer just to be sure. This is bad, but this
	// whole test will go away once RegisterOrGet is removed.
	oldRegisterer := prometheus.DefaultRegisterer
	defer func() {
		prometheus.DefaultRegisterer = oldRegisterer
	}()
	prometheus.DefaultRegisterer = prometheus.NewRegistry()
	original := prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "test",
			Help: "help",
		},
		[]string{"foo", "bar"},
	)
	equalButNotSame := prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "test",
			Help: "help",
		},
		[]string{"foo", "bar"},
	)
	if err := prometheus.Register(original); err != nil {
		t.Fatal(err)
	}
	if err := prometheus.Register(equalButNotSame); err == nil {
		t.Fatal("expected error when registringe equal collector")
	}
	existing, err := prometheus.RegisterOrGet(equalButNotSame)
	if err != nil {
		t.Fatal(err)
	}
	if existing != original {
		t.Error("expected original collector but got something else")
	}
	if existing == equalButNotSame {
		t.Error("expected original callector but got new one")
	}
}
Exemplo n.º 2
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
			}

			mVec := prometheus.NewUntypedVec(
				prometheus.UntypedOpts{
					Name: mname,
					Help: "Telegraf collected metric",
				},
				labels,
			)
			collector, err := prometheus.RegisterOrGet(mVec)
			if err != nil {
				log.Printf("prometheus_client: Metric failed to register with prometheus, %s", err)
				continue
			}
			mVec, ok := collector.(*prometheus.UntypedVec)
			if !ok {
				continue
			}

			switch val := val.(type) {
			case int64:
				m, err := mVec.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 := mVec.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
}