Пример #1
0
func (vs *ValueScraper) Scrape(data []byte, reg *harness.MetricRegistry) error {
	isFirst := true
	return vs.forTargetValue(data, func(result *jsonpath.Result) {
		if !isFirst {
			log.Infof("ignoring non-first value;path:<%s>", vs.valueJsonPath)
			return
		}
		isFirst = false

		if result.Type != jsonpath.JsonNumber {
			log.Warnf("skipping not numerical result;path:<%s>,value:<%s>",
				vs.valueJsonPath, result.Value)
			return
		}

		value, err := vs.parseValue(result.Value)
		if err != nil {
			// Should never happen.
			log.Errorf("could not parse numerical value as float;path:<%s>,value:<%s>",
				vs.valueJsonPath, result.Value)
			return
		}

		log.Debugf("metric updated;name:<%s>,labels:<%s>,value:<%.2f>", vs.Name, vs.Labels, value)
		reg.Get(vs.Name).(*prometheus.GaugeVec).With(vs.Labels).Set(value)
	})
}
Пример #2
0
func exampleInit(c *cli.Context, reg *harness.MetricRegistry) (harness.Collector, error) {
	fooEnabled := c.Bool("flagfoo")
	if fooEnabled {
		fmt.Println("foo enabled")
	}

	reg.Register("example_metric_A", prometheus.NewGauge(prometheus.GaugeOpts{
		Name: "example_metric_A",
		Help: "is example_metric_A",
	}))
	reg.Register("example_metric_B", prometheus.NewGauge(prometheus.GaugeOpts{
		Name: "example_metric_B",
		Help: "is example_metric_B",
	}))

	return &collector{}, nil
}
Пример #3
0
func (col *collector) Collect(reg *harness.MetricRegistry) {
	reg.Get("example_metric_A").(prometheus.Gauge).Set(128.0)
}
Пример #4
0
func (obsc *ObjectScraper) Scrape(data []byte, reg *harness.MetricRegistry) error {
	return obsc.forTargetValue(data, func(result *jsonpath.Result) {
		if result.Type != jsonpath.JsonObject && result.Type != jsonpath.JsonArray {
			log.Warnf("skipping not structual result;path:<%s>,value:<%s>",
				obsc.valueJsonPath, result.Value)
			return
		}

		labels := obsc.newLabels()
		for name, path := range obsc.labelJsonPaths {
			firstResult, err := obsc.extractFirstValue(result.Value, path)
			if err != nil {
				log.Warnf("could not find value for label path;path:<%s>,json:<%s>,err:<%s>", path, result.Value, err)
				continue
			}
			value := firstResult.Value
			if firstResult.Type == jsonpath.JsonString {
				// Strip quotes
				value = value[1 : len(value)-1]
			}
			labels[name] = string(value)
		}

		for name, configValue := range obsc.Values {
			var metricValue float64
			path := obsc.valueJsonPaths[name]

			if path == nil {
				// Static value
				value, err := obsc.parseValue([]byte(configValue))
				if err != nil {
					log.Errorf("could not use configured value as float number;name:<%s>,err:<%s>", err)
					continue
				}
				metricValue = value
			} else {
				// Dynamic value
				firstResult, err := obsc.extractFirstValue(result.Value, path)
				if err != nil {
					log.Warnf("could not find value for value path;path:<%s>,json:<%s>,err:<%s>", path, result.Value, err)
					continue
				}

				if firstResult.Type != jsonpath.JsonNumber {
					log.Warnf("skipping not numerical result;path:<%s>,value:<%s>",
						obsc.valueJsonPath, result.Value)
					continue
				}

				value, err := obsc.parseValue(firstResult.Value)
				if err != nil {
					// Should never happen.
					log.Errorf("could not parse numerical value as float;path:<%s>,value:<%s>",
						obsc.valueJsonPath, firstResult.Value)
					continue
				}
				metricValue = value
			}

			fqn := harness.MakeMetricName(obsc.Name, name)
			log.Debugf("metric updated;name:<%s>,labels:<%s>,value:<%.2f>", fqn, labels, metricValue)
			reg.Get(fqn).(*prometheus.GaugeVec).With(labels).Set(metricValue)
		}
	})
}