Exemplo n.º 1
0
// NewListener creates a new listener for carbon datapoints
func NewListener(ctx context.Context, sink dpsink.Sink, conf listenerConfig, listenAddr string,
	metricDeconstructor string, metricDeconstructorOptions string) (*Listener, error) {
	psocket, err := net.Listen("tcp", listenAddr)
	if err != nil {
		return nil, err
	}

	deconstructor, err := metricdeconstructor.Load(metricDeconstructor, metricDeconstructorOptions)
	if err != nil {
		return nil, err
	}

	counter := &dpsink.Counter{}
	finalSink := dpsink.FromChain(sink, dpsink.NextWrap(counter))

	receiver := Listener{
		sink:                finalSink,
		psocket:             psocket,
		conf:                conf,
		metricDeconstructor: deconstructor,
		ctx:                 ctx,
		st:                  stats.ToKeeperMany(protocol.ListenerDims(conf.name, "carbon"), counter),
	}
	receiver.wg.Add(1)

	go receiver.startListening()
	return &receiver, nil
}
Exemplo n.º 2
0
func TestNewCarbonDatapoint(t *testing.T) {
	identityParser, _ := metricdeconstructor.Load("", "")
	dp, err := NewCarbonDatapoint("hello 3 3", identityParser)
	assert.Equal(t, nil, err, "Should be a valid carbon line")
	assert.Equal(t, "hello", dp.Metric, "Should get metric back")

	_, err = NewCarbonDatapoint("INVALIDLINE", identityParser)
	assert.NotEqual(t, nil, err, "Line should be invalid")

	_, err = NewCarbonDatapoint("hello 3 bob", identityParser)
	assert.NotEqual(t, nil, err, "Line should be invalid")

	_, err = NewCarbonDatapoint("hello bob 3", identityParser)
	assert.NotEqual(t, nil, err, "Line should be invalid")

	dp, _ = NewCarbonDatapoint("hello 3.3 3", identityParser)
	f := dp.Value.(datapoint.FloatValue).Float()
	assert.Equal(t, 3.3, f, "Should get value back")

	carbonDp, _ := NativeCarbonLine(dp)
	assert.Equal(t, "hello 3.3 3", carbonDp, "Should get the carbon line back")

	dp, err = NewCarbonDatapoint("hello 3 3", &errorDeconstructor{})
	assert.NotEqual(t, nil, err, "Should NOT be a valid carbon line")
}