-
Notifications
You must be signed in to change notification settings - Fork 46
Unicron add api logs in dynamodb #4894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8cc0ae3
AWS DynamoDB API logs wip
lukaszgryglicki bd55fe7
Add scripts to add IAM roles for new tables
lukaszgryglicki b7bf511
Integrate cla-{stage}-api-log table to py and go backends
lukaszgryglicki 49016f6
whitespace cleanup
lukaszgryglicki e4fb4aa
Final tweaks
lukaszgryglicki 0d75d4b
Example get logs
lukaszgryglicki d2d6901
Address AI feedback
lukaszgryglicki 44edeaa
Fix make test & make lint for golang backend
lukaszgryglicki b95cc2d
One more AI feedback update
lukaszgryglicki c08fbf0
Add policy roles permissions for cla-{stage}-api-log table
lukaszgryglicki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright The Linux Foundation and each contributor to CommunityBridge. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package api_logs | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
| ) | ||
|
|
||
| // APILog data model for DynamoDB table cla-{stage}-api-log | ||
| type APILog struct { | ||
| URL string `dynamodbav:"url" json:"url"` | ||
| DT int64 `dynamodbav:"dt" json:"dt"` | ||
| Bucket string `dynamodbav:"bucket" json:"bucket"` | ||
| } | ||
|
|
||
| // String returns a string representation of the APILog | ||
| func (a *APILog) String() string { | ||
| return fmt.Sprintf("APILog{URL: %s, DT: %d, Bucket: %s}", a.URL, a.DT, a.Bucket) | ||
| } | ||
|
|
||
| // NewAPILog creates a new APILog entry with current timestamp | ||
| func NewAPILog(url, bucket string) *APILog { | ||
| return &APILog{ | ||
| URL: url, | ||
| DT: time.Now().UnixMilli(), // Unix timestamp in milliseconds | ||
| Bucket: bucket, | ||
| } | ||
| } | ||
lukaszgryglicki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // Copyright The Linux Foundation and each contributor to CommunityBridge. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package api_logs | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/aws/aws-sdk-go/aws" | ||
| "github.com/aws/aws-sdk-go/service/dynamodb" | ||
| "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" | ||
| ) | ||
|
|
||
| const ( | ||
| // APILogTableName is the DynamoDB table name for API logs | ||
| APILogTableName = "cla-%s-api-log" | ||
| ) | ||
|
|
||
| // Repository interface for API logs | ||
| type Repository interface { | ||
| LogAPIRequest(ctx context.Context, url string) error | ||
| } | ||
|
|
||
| // repository implements the Repository interface | ||
| type repository struct { | ||
| stage string | ||
| dynamoDBClient *dynamodb.DynamoDB | ||
| } | ||
|
|
||
| // NewRepository creates a new API logs repository | ||
| func NewRepository(stage string, dynamoDBClient *dynamodb.DynamoDB) Repository { | ||
| return &repository{ | ||
| stage: stage, | ||
| dynamoDBClient: dynamoDBClient, | ||
| } | ||
| } | ||
|
|
||
| // LogAPIRequest logs an API request to the DynamoDB table | ||
| // Creates three entries: ALL bucket, daily bucket (YYYY-MM-DD), and monthly bucket (YYYY-MM) | ||
| // IMPORTANT: table key is (url, dt). To avoid overwrites, dt is shifted by -1/0/+1 ms per bucket. | ||
| func (r *repository) LogAPIRequest(ctx context.Context, url string) error { | ||
| // 200% fail-safe: never panic on nil ctx/client | ||
| if ctx == nil { | ||
| ctx = context.Background() | ||
| } | ||
| if r == nil || r.dynamoDBClient == nil { | ||
| return fmt.Errorf("dynamodb client is nil") | ||
| } | ||
|
|
||
| now := time.Now().UTC() | ||
| timestamp := now.UnixMilli() | ||
|
|
||
| // Generate bucket names | ||
| dailyBucket := now.Format("2006-01-02") // YYYY-MM-DD | ||
| monthlyBucket := now.Format("2006-01") // YYYY-MM | ||
|
|
||
| entries := []*APILog{ | ||
| {URL: url, DT: timestamp - 1, Bucket: "ALL"}, | ||
| {URL: url, DT: timestamp, Bucket: dailyBucket}, | ||
| {URL: url, DT: timestamp + 1, Bucket: monthlyBucket}, | ||
| } | ||
| tableName := fmt.Sprintf(APILogTableName, r.stage) | ||
|
|
||
| var errs []string | ||
| for _, logEntry := range entries { | ||
| // Convert to DynamoDB attribute value | ||
| av, err := dynamodbattribute.MarshalMap(logEntry) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Sprintf("bucket=%s marshal=%v", logEntry.Bucket, err)) | ||
| continue | ||
| } | ||
|
|
||
| // Put item to DynamoDB | ||
| input := &dynamodb.PutItemInput{ | ||
| TableName: aws.String(tableName), | ||
| Item: av, | ||
| } | ||
|
|
||
| _, err = r.dynamoDBClient.PutItemWithContext(ctx, input) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Sprintf("bucket=%s put=%v", logEntry.Bucket, err)) | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| // Return error so middleware can emit a single LG:* line. | ||
| if len(errs) > 0 { | ||
| return fmt.Errorf("%s", strings.Join(errs, "; ")) | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.