@@ -38,24 +38,36 @@ var bufferPool = sync.Pool{New: func() interface{} {
3838
3939type ApmServerTransportStatusType string
4040
41+ // Constants for the state of the transport used in
42+ // the backoff implementation.
4143const (
4244 Failing ApmServerTransportStatusType = "Failing"
4345 Pending ApmServerTransportStatusType = "Pending"
4446 Healthy ApmServerTransportStatusType = "Healthy"
4547)
4648
49+ // A struct to track the state and status of sending
50+ // to the APM server. Used in the backoff implementation.
4751type ApmServerTransportStateType struct {
4852 sync.Mutex
4953 Status ApmServerTransportStatusType
5054 ReconnectionCount int
5155 GracePeriodTimer * time.Timer
5256}
5357
58+ // The status of transport to the APM server.
59+ //
60+ // This instance of the ApmServerTransportStateType is public for use in tests.
5461var ApmServerTransportState = ApmServerTransportStateType {
5562 Status : Healthy ,
5663 ReconnectionCount : - 1 ,
5764}
5865
66+ // PostToApmServer takes a chunk of APM agent data and posts it to the APM server.
67+ //
68+ // The function compresses the APM agent data, if it's not already compressed.
69+ // It sets the APM transport status to failing upon errors, as part of the backoff
70+ // strategy.
5971func PostToApmServer (client * http.Client , agentData AgentData , config * extensionConfig , ctx context.Context ) error {
6072 // todo: can this be a streaming or streaming style call that keeps the
6173 // connection open across invocations?
@@ -123,10 +135,21 @@ func PostToApmServer(client *http.Client, agentData AgentData, config *extension
123135 return nil
124136}
125137
138+ // IsTransportStatusHealthyOrPending returns true if the APM server transport status is
139+ // healthy or pending, and false otherwise.
140+ //
141+ // This function is public for use in tests.
126142func IsTransportStatusHealthyOrPending () bool {
127143 return ApmServerTransportState .Status != Failing
128144}
129145
146+ // SetApmServerTransportState takes a state of the APM server transport and updates
147+ // the current state of the transport. For a change to a failing state, the grace period
148+ // is calculated and a go routine is started that waits for that period to complete
149+ // before changing the status to "pending". This would allow a subsequent send attempt
150+ // to the APM server.
151+ //
152+ // This function is public for use in tests.
130153func SetApmServerTransportState (status ApmServerTransportStatusType , ctx context.Context ) {
131154 switch status {
132155 case Healthy :
@@ -165,6 +188,8 @@ func computeGracePeriod() time.Duration {
165188 return time .Duration ((gracePeriodWithoutJitter + jitter * gracePeriodWithoutJitter ) * float64 (time .Second ))
166189}
167190
191+ // EnqueueAPMData adds a AgentData struct to the agent data channel, effectively queueing for a send
192+ // to the APM server.
168193func EnqueueAPMData (agentDataChannel chan AgentData , agentData AgentData ) {
169194 select {
170195 case agentDataChannel <- agentData :
0 commit comments