Skip to content

Commit c58c033

Browse files
authored
Merge pull request #38 from FireTail-io/health-endpoint
Health endpoint
2 parents fe8fdaf + 8ff4a29 commit c58c033

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Find below a full list of the environment variables used by the FireTail Lambda
142142
| `AWS_LAMBDA_EXEC_WRAPPER` | None | Must be set to `/opt/firetail-wrapper.sh`. |
143143
| `FIRETAIL_API_TOKEN` | None | Your API token for the FireTail Logging API. If left unset, no logs will be sent to the FireTail Logging API |
144144
| `FIRETAIL_API_URL` | `https://api.logging.eu-west-1.prod.firetail.app/logs/bulk` | The URL of the FireTail Logging API |
145+
| `FIRETAIL_API_URL_HEALTH` | `https://api.logging.eu-west-1.prod.firetail.app/health` | The URL of a health endpoint to send a request to during startup to aid debugging |
145146
| `FIRETAIL_EXTENSION_DEBUG` | `false` | Enables debug logging from the extension if set to a value parsed as `true` by [strconv.ParseBool](https://pkg.go.dev/strconv#ParseBool) |
146147
| `FIRETAIL_LOG_BUFFER_SIZE` | `1000` | The maximum amount of logs the extension will hold in its buffer from which logs are batched and sent to FireTail |
147148
| `FIRETAIL_MAX_BATCH_SIZE` | `100` | The maximum size of a batch of logs to be sent to the FireTail logging API in one request |

firetail/record_receiver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func RecordReceiver(recordsChannel chan Record, maxBatchSize int, firetailApiUrl
2121
}
2222

2323
// Give the batch to the batch callback. If it errs, we continue
24+
log.Printf("Attempting to send batch of %d record(s) to Firetail...", len(recordsBatch))
2425
recordsSent, err := SendRecordsToSaaS(recordsBatch, firetailApiUrl, firetailApiToken)
2526
if err != nil {
2627
log.Println("Error sending records to Firetail:", err.Error())

main.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io/ioutil"
1010
"log"
11+
"net/http"
1112
"os"
1213
"path"
1314
"strconv"
@@ -18,24 +19,43 @@ func main() {
1819
// Configure logging
1920
extensionName := path.Base(os.Args[0])
2021
log.SetPrefix(fmt.Sprintf("[%s] ", extensionName))
21-
if isDebug, err := strconv.ParseBool(os.Getenv("FIRETAIL_EXTENSION_DEBUG")); err != nil || !isDebug {
22+
isDebug, err := strconv.ParseBool(os.Getenv("FIRETAIL_EXTENSION_DEBUG"))
23+
if err != nil || !isDebug {
2224
// If we're not in debug mode, we'll just send the logs to the void
2325
log.SetOutput(ioutil.Discard)
2426
} else {
2527
log.Println("Firetail extension starting in debug mode.")
2628
}
2729

2830
// Log the value of AWS_LAMBDA_EXEC_WRAPPER and AWS_LAMBDA_RUNTIME_API for debugging
29-
log.Println("AWS_LAMBDA_EXEC_WRAPPER:", os.Getenv("AWS_LAMBDA_EXEC_WRAPPER"))
30-
log.Println("AWS_LAMBDA_RUNTIME_API:", os.Getenv("AWS_LAMBDA_RUNTIME_API"))
31+
if isDebug {
32+
log.Println("AWS_LAMBDA_EXEC_WRAPPER:", os.Getenv("AWS_LAMBDA_EXEC_WRAPPER"))
33+
log.Println("AWS_LAMBDA_RUNTIME_API:", os.Getenv("AWS_LAMBDA_RUNTIME_API"))
34+
}
35+
36+
// If the health endpoint env var is set, make a request to it to check it's reachable
37+
if healthEndpoint := os.Getenv("FIRETAIL_API_URL_HEALTH"); healthEndpoint != "" {
38+
resp, err := http.Get(healthEndpoint)
39+
if err != nil {
40+
log.Println("Error making request to health endpoint:", err.Error())
41+
} else {
42+
defer resp.Body.Close()
43+
healthResponse, err := ioutil.ReadAll(resp.Body)
44+
if err != nil {
45+
log.Println("Error reading health endpoint response:", err.Error())
46+
} else {
47+
log.Println("Health endpoint response:", strconv.Itoa(resp.StatusCode), string(healthResponse))
48+
}
49+
}
50+
}
3151

3252
// This context will be cancelled whenever a SIGTERM or SIGINT signal is received
3353
// We'll use it for our requests to the extensions API & to shutdown the log server
3454
ctx := getContext()
3555

3656
// Create a Lambda Extensions API client & register our extension
3757
extensionClient := extensionsapi.NewClient()
38-
_, err := extensionClient.Register(ctx, extensionName)
58+
_, err = extensionClient.Register(ctx, extensionName)
3959
if err != nil {
4060
panic(err)
4161
}

proxy/proxy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,18 @@ func (p *ProxyServer) recordAssembler() {
146146

147147
eventBody, err := io.ReadAll(event.Body)
148148
if err != nil {
149-
log.Println("Error reading event body:", err)
149+
log.Println("Error reading event body:", err.Error())
150150
continue
151151
}
152152
responseBody, err := io.ReadAll(lambdaResponse.Body)
153153
if err != nil {
154-
log.Println("Error reading response body:", err)
154+
log.Println("Error reading response body:", err.Error())
155155
continue
156156
}
157157

158158
var recordResponse firetail.RecordResponse
159159
if err := json.Unmarshal(responseBody, &recordResponse); err != nil {
160-
log.Println("Error unmarshalling response body:", err)
160+
log.Println("Error unmarshalling response body:", err.Error())
161161
continue
162162
}
163163

0 commit comments

Comments
 (0)