Skip to content

Commit 3ad7982

Browse files
estolfojlvoiseux
andauthored
Improve go docs (#168)
* Improve go docs * Update apm-lambda-extension/extension/apm_server.go Co-authored-by: Jean-Louis Voiseux <[email protected]> * Update apm-lambda-extension/extension/apm_server.go Co-authored-by: Jean-Louis Voiseux <[email protected]> Co-authored-by: Jean-Louis Voiseux <[email protected]>
1 parent a14c80f commit 3ad7982

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

apm-lambda-extension/extension/apm_server.go

+25
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,36 @@ var bufferPool = sync.Pool{New: func() interface{} {
3838

3939
type ApmServerTransportStatusType string
4040

41+
// Constants for the state of the transport used in
42+
// the backoff implementation.
4143
const (
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.
4751
type 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.
5461
var 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.
5971
func 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.
126142
func 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.
130153
func 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.
168193
func EnqueueAPMData(agentDataChannel chan AgentData, agentData AgentData) {
169194
select {
170195
case agentDataChannel <- agentData:

apm-lambda-extension/extension/http_server.go

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
var agentDataServer *http.Server
2727

28+
// StartHttpServer starts the server listening for APM agent data.
2829
func StartHttpServer(agentDataChan chan AgentData, config *extensionConfig) (err error) {
2930
mux := http.NewServeMux()
3031
mux.HandleFunc("/", handleInfoRequest(config.apmServerUrl))

apm-lambda-extension/extension/process_env.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func getIntFromEnv(name string) (int, error) {
6363
return value, nil
6464
}
6565

66-
// ProcessEnv : pull env into globals
66+
// ProcessEnv extracts ENV variables into globals
6767
func ProcessEnv() *extensionConfig {
6868
dataReceiverTimeoutSeconds, err := getIntFromEnv("ELASTIC_APM_DATA_RECEIVER_TIMEOUT_SECONDS")
6969
if err != nil {

apm-lambda-extension/extension/process_events.go

+4
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ import (
2323
"net/http"
2424
)
2525

26+
// ProcessShutdown processes the Shutdown event received from the
27+
// Lambda runtime API.
2628
func ProcessShutdown() {
2729
Log.Info("Received SHUTDOWN event, exiting")
2830
agentDataServer.Close()
2931
}
3032

33+
// FlushAPMData reads all the apm data in the apm data channel and sends it to the APM server.
3134
func FlushAPMData(client *http.Client, dataChannel chan AgentData, config *extensionConfig, ctx context.Context) {
3235
if !IsTransportStatusHealthyOrPending() {
3336
Log.Debug("Flush skipped - Transport unhealthy")
@@ -48,6 +51,7 @@ func FlushAPMData(client *http.Client, dataChannel chan AgentData, config *exten
4851
}
4952
}
5053

54+
// PrettyPrint prints formatted, legible json data.
5155
func PrettyPrint(v interface{}) string {
5256
data, err := json.MarshalIndent(v, "", "\t")
5357
if err != nil {

apm-lambda-extension/logsapi/client.go

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const (
5555
Extension EventType = "extension"
5656
)
5757

58+
// SubEventType is a Logs API sub event type
5859
type SubEventType string
5960

6061
const (
@@ -108,6 +109,7 @@ type Destination struct {
108109
Encoding HttpEncoding `json:"encoding"`
109110
}
110111

112+
// SchemaVersion is the Lambda runtime API schema version
111113
type SchemaVersion string
112114

113115
const (

apm-lambda-extension/logsapi/subscribe.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ var ListenerHost = "sandbox"
3333
var Server *http.Server
3434
var Listener net.Listener
3535

36+
// LogEvent represents an event received from the Logs API
3637
type LogEvent struct {
3738
Time time.Time `json:"time"`
3839
Type SubEventType `json:"type"`
3940
StringRecord string
4041
Record LogEventRecord
4142
}
4243

44+
// LogEventRecord is a sub-object in a Logs API event
4345
type LogEventRecord struct {
4446
RequestId string `json:"requestId"`
4547
Status string `json:"status"`
@@ -63,7 +65,7 @@ func subscribe(extensionID string, eventTypes []EventType) error {
6365
return err
6466
}
6567

66-
// Subscribe : Starts the HTTP server listening for log events and subscribes to the Logs API
68+
// Subscribe starts the HTTP server listening for log events and subscribes to the Logs API
6769
func Subscribe(ctx context.Context, extensionID string, eventTypes []EventType, out chan LogEvent) (err error) {
6870
if checkAWSSamLocal() {
6971
return errors.New("Detected sam local environment")

0 commit comments

Comments
 (0)