func TestListenerLoader(t *testing.T) { listenFrom := &config.ListenFrom{ ListenAddr: workarounds.GolangDoesnotAllowPointerToStringLiteral("127.0.0.1:0"), ServerAcceptDeadline: workarounds.GolangDoesnotAllowPointerToTimeLiteral(time.Millisecond), } ctx := context.Background() forwardTo := dptest.NewBasicSink() listener, err := ListenerLoader(ctx, forwardTo, listenFrom) defer listener.Close() assert.Equal(t, nil, err, "Should be ok to make") defer listener.Close() listeningDialAddress := fmt.Sprintf("127.0.0.1:%d", nettest.TCPPort(listener.psocket)) assert.Equal(t, numStats, len(listener.Stats()), "Should have no stats") assert.NoError(t, err, "Should be ok to make") conn, err := net.Dial("tcp", listeningDialAddress) assert.NoError(t, err, "Should be ok to make") assert.Equal(t, int64(0), listener.stats.invalidDatapoints) var buf bytes.Buffer fmt.Fprintf(&buf, "%s %d %d\n\nINVALIDLINE", "ametric", 2, 2) _, err = buf.WriteTo(conn) conn.Close() assert.Equal(t, nil, err, "Should be ok to write") dp := forwardTo.Next() assert.Equal(t, "ametric", dp.Metric, "Should be metric") i := dp.Value.(datapoint.IntValue).Int() assert.Equal(t, int64(2), i, "Should get 2") for atomic.LoadInt64(&listener.stats.retriedListenErrors) == 0 { time.Sleep(time.Millisecond) } assert.Equal(t, int64(1), atomic.LoadInt64(&listener.stats.invalidDatapoints)) }
for _, jsonDatapoint := range datapoints { v, err := ValueToValue(jsonDatapoint.Value) if err != nil { log.WithField("err", err).Warn("Unable to get value for datapoint") } else { dp := datapoint.New(jsonDatapoint.Metric, jsonDatapoint.Dimensions, v, fromMT(com_signalfx_metrics_protobuf.MetricType(mt)), fromTs(jsonDatapoint.Timestamp)) dps = append(dps, dp) } } } return decoder.Sink.AddDatapoints(ctx, dps) } var defaultConfig = &config.ListenFrom{ ListenAddr: workarounds.GolangDoesnotAllowPointerToStringLiteral("127.0.0.1:12345"), TimeoutDuration: workarounds.GolangDoesnotAllowPointerToTimeLiteral(time.Second * 30), Name: workarounds.GolangDoesnotAllowPointerToStringLiteral("signalfxlistener"), JSONEngine: workarounds.GolangDoesnotAllowPointerToStringLiteral("native"), } // ListenerLoader loads a listener for signalfx protocol from config func ListenerLoader(ctx context.Context, sink dpsink.Sink, listenFrom *config.ListenFrom) (*ListenerServer, error) { structdefaults.FillDefaultFrom(listenFrom, defaultConfig) log.WithField("listenFrom", listenFrom).Info("Creating signalfx listener using final config") return StartServingHTTPOnPort(ctx, sink, *listenFrom.ListenAddr, *listenFrom.TimeoutDuration, *listenFrom.Name) } type jsonMarshalStub func(v interface{}) ([]byte, error) type metricHandler struct {
atomic.AddInt64(&listener.stats.retriedListenErrors, 1) log.Debug("Timeout (or temp) waiting for connection. Expected, will continue") continue } } log.WithField("err", err).Info("Unable to accept a socket connection") return } go listener.handleConnection(conn) } } var defaultListenerConfig = &config.ListenFrom{ ListenAddr: workarounds.GolangDoesnotAllowPointerToStringLiteral("127.0.0.1:2003"), Name: workarounds.GolangDoesnotAllowPointerToStringLiteral("carbonlistener"), TimeoutDuration: workarounds.GolangDoesnotAllowPointerToTimeLiteral(time.Second * 30), MetricDeconstructor: workarounds.GolangDoesnotAllowPointerToStringLiteral(""), MetricDeconstructorOptions: workarounds.GolangDoesnotAllowPointerToStringLiteral(""), ServerAcceptDeadline: workarounds.GolangDoesnotAllowPointerToTimeLiteral(time.Second), } // ListenerLoader loads a listener for the carbon/graphite protocol from config func ListenerLoader(ctx context.Context, sink dpsink.Sink, listenFrom *config.ListenFrom) (*Listener, error) { structdefaults.FillDefaultFrom(listenFrom, defaultListenerConfig) conf := listenerConfig{ serverAcceptDeadline: *listenFrom.ServerAcceptDeadline, connectionTimeout: *listenFrom.TimeoutDuration, name: *listenFrom.Name, } // *listenFrom.Name return NewListener(