Skip to content

Commit 8d3ded5

Browse files
authored
Merge pull request #11 from logzio/aws-dev
v1.0.2
2 parents 4419b91 + 176620b commit 8d3ded5

4 files changed

Lines changed: 53 additions & 9 deletions

File tree

.github/workflows/upload-zip.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Upload release
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
upload:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout code
12+
uses: actions/checkout@v2
13+
- name: Set up Go
14+
uses: actions/setup-go@v2
15+
with:
16+
go-version: 1.17
17+
- name: Build function
18+
run: make function
19+
- name: Upload release asset
20+
uses: actions/upload-release-asset@v1
21+
with:
22+
asset_path: ./function.zip
23+
asset_name: function.zip
24+
asset_content_type: application/zip

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,16 @@ This function has the following limitations:
2020

2121
- It can only process metrics data in OTLP 0.7 format.
2222
- It can only forward the data to a Prometheus Remote Write endpoint.
23+
24+
### Changelog
25+
26+
- v1.0.2
27+
- Stop trying to send bulks if encountered 401 status code
28+
- Add logzio identifier to each log (5 last chars of the shipping token)
29+
- Add zip workflow and artifact
30+
- v1.0.1
31+
- Improved logging (Add `zap` logger)
32+
- Add metadata (AWS account, firehose request id, lambda invocation id) to each log for context
33+
- Flush buffered logs if exists, before the function run ends
34+
- v1.0.0
35+
- Initial release: Lambda function that receives OTLP (0.7.0) data from AWS metric stream and exports the data to logz.io using Prometheus remote write

handler/handler.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ func generateValidFirehoseResponse(statusCode int, requestId string, errorMessag
8787
}
8888
}
8989
}
90-
func initLogger(ctx context.Context, request events.APIGatewayProxyRequest) zap.SugaredLogger {
90+
func initLogger(ctx context.Context, request events.APIGatewayProxyRequest, token string) zap.SugaredLogger {
9191
awsRequestId := ""
9292
account := ""
93+
logzioIdentifier := ""
9394
lambdaContext, ok := lambdacontext.FromContext(ctx)
9495
if ok {
9596
awsRequestId = lambdaContext.AwsRequestID
@@ -98,14 +99,18 @@ func initLogger(ctx context.Context, request events.APIGatewayProxyRequest) zap.
9899
if len(awsAccount) > 4 {
99100
account = awsAccount[4]
100101
}
102+
if len(token) >= 5 {
103+
logzioIdentifier = token[len(token)-5:]
104+
}
101105
firehoseRequestId := request.Headers["X-Amz-Firehose-Request-Id"]
102106
config := zap.NewProductionConfig()
103107
config.EncoderConfig.StacktraceKey = "" // to hide stacktrace info
104108
config.OutputPaths = []string{"stdout"} // write to stdout
105109
config.InitialFields = map[string]interface{}{
106-
"aws_account": account,
107-
"lambda_invocation_id": awsRequestId,
108-
"firehose_request_id": firehoseRequestId,
110+
"aws_account": account,
111+
"lambda_invocation_id": awsRequestId,
112+
"firehose_request_id": firehoseRequestId,
113+
"logzio_account_identifier": logzioIdentifier,
109114
}
110115
logger, configErr := config.Build()
111116
if configErr != nil {
@@ -250,13 +255,9 @@ func summaryValuesToMetrics(metricsToSendSlice pdata.InstrumentationLibraryMetri
250255
}
251256
}
252257
func HandleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
253-
log := initLogger(ctx, request)
254-
// flush buffered logs if exists, before the function run ends
255-
defer log.Sync()
256258
metricCount := 0
257259
dataPointCount := 0
258260
shippingErrors := new(ErrorCollector)
259-
log.Infof("Getting access key from headers")
260261
// get requestId to match firehose response requirements
261262
requestId := request.Headers["X-Amz-Firehose-Request-Id"]
262263
if requestId == "" {
@@ -266,6 +267,9 @@ func HandleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (
266267
if LogzioToken == "" {
267268
LogzioToken = request.Headers["x-amz-firehose-access-key"]
268269
}
270+
log := initLogger(ctx, request, LogzioToken)
271+
// flush buffered logs if exists, before the function run ends
272+
defer log.Sync()
269273
if LogzioToken == "" {
270274
accessKeyErr := errors.New("cant find access key in 'X-Amz-Firehose-Access-Key' or 'x-amz-firehose-access-key' headers")
271275
log.Error(accessKeyErr)
@@ -368,6 +372,9 @@ func HandleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (
368372
err = metricsExporter.PushMetrics(ctx, metricsToSend)
369373
if err != nil {
370374
log.Warnf("Error while sending metrics: %s", err)
375+
if strings.Contains(err.Error(), "status 401") {
376+
return generateValidFirehoseResponse(400, requestId, "Error while sending metrics:", err), nil
377+
}
371378
shippingErrors.Collect(err)
372379
} else {
373380
numberOfMetrics, numberOfDataPoints := metricsToSend.MetricAndDataPointCount()

handler/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestHandleRequestErrors(t *testing.T) {
7474
expected int
7575
}
7676
var getListenerUrlTests = []getListenerUrlTest{
77-
{"noValidToken", 500},
77+
{"noValidToken", 400},
7878
{"noToken", 400},
7979
{"malformedBody", 400},
8080
{"simpleevent", 400},

0 commit comments

Comments
 (0)