@@ -20,16 +20,11 @@ import (
2020 "github.com/kentik/libkflow/metrics"
2121)
2222
23- // messagePrefix is an 80-byte prefix for the message header when sending kflow to the Kentik API. This is a deprecated
24- // header, but the bytes must remain for backwards compatibility with the Kentik API.
25- var messagePrefix = [80 ]byte {}
26-
2723// A Sender aggregates and transmits flow information to Kentik.
2824type Sender struct {
2925 agg * agg.Agg
3026 exit chan struct {}
3127 url * url.URL
32- timeout time.Duration
3328 client * api.Client
3429 sample int
3530 ticker * time.Ticker
@@ -43,12 +38,11 @@ type Sender struct {
4338 Metrics * metrics.Metrics
4439}
4540
46- func newSender (url * url.URL , timeout time. Duration ) * Sender {
41+ func newSender (url * url.URL ) * Sender {
4742 tickerCtx , cancelFunc := context .WithCancel (context .Background ())
4843 return & Sender {
4944 exit : make (chan struct {}),
5045 url : url ,
51- timeout : timeout ,
5246 ticker : time .NewTicker (20 * time .Minute ),
5347 tickerCtx : tickerCtx ,
5448 tickerCancelFunc : cancelFunc ,
@@ -62,80 +56,6 @@ func (s *Sender) Send(flow *flow.Flow) {
6256 s .agg .Add (flow )
6357}
6458
65- // SendFlows sends the flows to the Kentik API, returning the number of bytes sent as the payload. The device ID on
66- // the flows is set to the device ID of the sender, regardless of what it was previously set to. This is to ensure all
67- // data matches the expectations of the downstream URL/API.
68- //
69- // This will directly send the slice of flows without any additional downsampling or rate limiting. This does not
70- // contribute to the underlying Send call.
71- func (s * Sender ) SendFlows (flows []flow.Flow ) (int64 , error ) {
72- s .workers .Add (1 )
73- defer s .workers .Done ()
74-
75- if s .Device == nil {
76- return 0 , fmt .Errorf ("device not initialized" )
77- }
78- if len (flows ) == 0 {
79- return 0 , nil
80- }
81- decoratedURL , err := s .createURLString ()
82- if err != nil {
83- return 0 , fmt .Errorf ("failed to create URL string: %w" , err )
84- }
85-
86- if s .Metrics != nil {
87- s .Metrics .TotalFlowsIn .Mark (int64 (len (flows )))
88- }
89-
90- // ensure all flows have the device ID set; otherwise it may not be properly queried
91- for i := range flows {
92- flows [i ].DeviceId = uint32 (s .Device .ID )
93- }
94-
95- // ensure the sample rate is matching the kentik api expectations
96- flow .NormalizeSampleRate (flows , 0 )
97-
98- // serialize the data
99- _ , segment , err := capnp .NewMessage (capnp .SingleSegment (nil ))
100- if err != nil {
101- return 0 , fmt .Errorf ("failed to create capn proto segment: %w" , err )
102- }
103- message , err := flow .ToCapnProtoMessage (flows , segment )
104- if err != nil {
105- return 0 , fmt .Errorf ("failed to convert flows to capn proto: %w" , err )
106- }
107-
108- // write the data with additional gzip compression
109- buf := & bytes.Buffer {}
110- z := gzip .NewWriter (buf )
111- _ , err = z .Write (messagePrefix [:])
112- if err != nil {
113- return 0 , fmt .Errorf ("failed to write empty message header: %w" , err )
114- }
115- err = capnp .NewPackedEncoder (z ).Encode (message )
116- if err != nil {
117- return 0 , fmt .Errorf ("failed to encode packed capn proto message: %w" , err )
118- }
119- err = z .Close ()
120- if err != nil {
121- return 0 , fmt .Errorf ("failed to close gzip writer: %w" , err )
122- }
123-
124- // send the compressed and packed message to the Kentik API
125- payloadLength := int64 (len (buf .Bytes ()))
126- err = s .client .SendFlow (decoratedURL , buf )
127- if err != nil {
128- return 0 , err
129- }
130-
131- if s .Metrics != nil {
132- s .Metrics .TotalFlowsOut .Mark (int64 (len (flows )))
133- s .Metrics .BytesSent .Mark (payloadLength )
134- }
135-
136- return payloadLength , nil
137- }
138-
13959// Stop requests a graceful shutdown of the Sender.
14060func (s * Sender ) Stop (wait time.Duration ) bool {
14161 s .agg .Stop ()
@@ -336,12 +256,5 @@ func (s *Sender) createURLString() (string, error) {
336256 if s .url == nil {
337257 return "" , fmt .Errorf ("url not initialized" )
338258 }
339-
340- // Create a new URL to avoid modifying the original, backed by the config
341- u := * s .url
342- q := u .Query ()
343- q .Set ("sid" , "0" )
344- q .Set ("sender_id" , s .Device .ClientID ())
345- u .RawQuery = q .Encode ()
346- return u .String (), nil
259+ return createURLString (* s .url , s .Device .ClientID ()), nil
347260}
0 commit comments