func pushCounter(l *lua.State, name string) {
	counter, _, err := aggregations.GetCounter(name)

	if err != nil {
		lua.Errorf(l, "%s", err)
		panic("unreachable")
	}

	l.NewTable()

	for name, fn := range counterFunctions {
		l.PushGoFunction(fn(counter))
		l.SetField(-2, name)
	}
}
Ejemplo n.º 2
0
func parseCounterRequest(remoteAddress string, line []string, errorChannel chan error) error {
	counterName := line[0]
	valueString := line[1]

	isSetOperation := valueString[0] == '='

	if isSetOperation {
		valueString = strings.TrimPrefix(valueString, "=")
	}

	value, err := strconv.ParseInt(valueString, 10, 64)

	if err != nil {
		return gotelemetry.NewErrorWithFormat(
			400, "Graphite => [%s] Invalid value %s: %s",
			nil,
			remoteAddress,
			line[1],
			err.Error(),
		)
	}

	counter, isCreated, err := aggregations.GetCounter(counterName)

	if isCreated {
		errorChannel <- gotelemetry.NewLogError("Graphite => Started receiving graphite data for '%s'", counterName)
	}

	if err != nil {
		return gotelemetry.NewErrorWithFormat(
			500, "Graphite => [%s] Unable to get counter %s: %s",
			nil,
			remoteAddress,
			counterName,
			err.Error(),
		)
	}

	if isSetOperation {
		counter.SetValue(value)
	} else {
		counter.Increment(value)
	}

	return nil
}