Skip to content

Commit

Permalink
Fix rate (#2)
Browse files Browse the repository at this point in the history
* add code + cloudformation

* lambda lib

* update s3 key

* fix yaml

* fix yaml

* fix yaml

* fix yaml

* fix yaml

* fix yaml

* remove field + fixes

* remove sleep

* added workflows

* added github action

* added cloudformation

* update code + tests

* readme

* change bucket dir

* change response time unit

* fix

* add consts

* fix workflows

* add types to on

* added examples

* added examples

* fix rate

* readme
  • Loading branch information
ShiranAvidov authored May 2, 2022
1 parent 8b0d8c2 commit 7a58899
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ To start just press the button and follow the instructions:
| LogzioListener | The Logz.io listener URL for your region. (For more details, see the regions page: https://docs.logz.io/user-guide/accounts/account-region.html) | Required | `https://listener.logz.io` |
| LogzioMetricsToken | Your Logz.io metrics token (Can be retrieved from the Manage Token page). | Required | - |
| LogzioLogsToken | Your Logz.io logs token (Can be retrieved from the Manage Token page). | Required | - |
| SchedulingInterval | The scheduling expression that determines when and how often the Lambda function runs. | Required | `rate(30 minutes)` |
| SchedulingInterval | The scheduling expression that determines when and how often the Lambda function runs. Rate below 6 minutes will cause the lambda to behave unexpectedly due to cold start and custom resource invocation. | Required | `rate(30 minutes)` |
| Headers | Your API headers separated by comma and each header's key and value are separated by `=` (`header_key_1=header_value_1,header_key_2=header_value_2`). | Optional | - |
| Body | Your API HTTP request body. | Optional | - |
| BearerToken | Your API bearer token. | Optional | - |
Expand Down
11 changes: 9 additions & 2 deletions aws/auto-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ Parameters:
Type: String
Description: >-
The scheduling expression that determines when and how often the Lambda
function runs.
function runs. Rate below 6 minutes will cause the lambda to behave unexpectedly
due to cold start and custom resource invocation.
Default: rate(30 minutes)
MinLength: 1
MaxLength: 256
Expand Down Expand Up @@ -178,4 +179,10 @@ Resources:
FunctionName: !Ref LambdaFunction
Action: 'lambda:InvokeFunction'
Principal: events.amazonaws.com
SourceArn: !GetAtt EventRule.Arn
SourceArn: !GetAtt EventRule.Arn
PrimerInvoke:
Type: 'AWS::CloudFormation::CustomResource'
DependsOn: LambdaFunction
Version: "1.0"
Properties:
ServiceToken: !GetAtt LambdaFunction.Arn
23 changes: 20 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"time"

"github.com/aws/aws-lambda-go/cfn"
"github.com/aws/aws-lambda-go/lambda"
metricsExporter "github.com/logzio/go-metrics-sdk"
"go.opentelemetry.io/otel/attribute"
Expand Down Expand Up @@ -70,6 +71,7 @@ const (
var (
debugLogger = log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile)
infoLogger = log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
errorLogger = log.New(os.Stdout, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
)

type logzioApiStatus struct {
Expand Down Expand Up @@ -498,11 +500,26 @@ func closeResponseBody(responseBody io.ReadCloser) {
}
}

func HandleRequest(ctx context.Context) error {
// Wrapper for first invocation from cloud formation custom resource
func customResourceRun(ctx context.Context, event cfn.Event) (physicalResourceID string, data map[string]interface{}, err error) {
if err = run(ctx); err != nil {
errorLogger.Printf("Error in first running: %s", err.Error())
}

return
}

func HandleRequest(ctx context.Context, event cfn.Event) error {
infoLogger.Println("Starting to get API status...")

if err := run(ctx); err != nil {
return err
// If requestID is empty - the lambda call is not from a custom resource
if event.RequestID == "" {
if err := run(ctx); err != nil {
return err
}
} else {
// Custom resource invocation
lambda.Start(cfn.LambdaWrap(customResourceRun))
}

infoLogger.Println("API status has been sent to Logz.io successfully")
Expand Down

0 comments on commit 7a58899

Please sign in to comment.