Created
April 4, 2023 18:44
-
-
Save konstl000/9925825104a050a0954031e8cc0b8182 to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import ( | |
"github.com/aws/aws-sdk-go-v2/config" | |
"github.com/aws/aws-sdk-go-v2/aws" | |
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" | |
"github.com/aws/aws-sdk-go-v2/service/s3" | |
"context" | |
"os" | |
"log" | |
"fmt" | |
"strings" | |
) | |
type AppState struct{ | |
LogGroup string | |
Bucket string | |
BucketRegion string | |
} | |
func initState()(*AppState){ | |
return &AppState{ | |
LogGroup: os.Getenv("LOG_GROUP"), | |
Bucket: os.Getenv("BUCKET"), | |
BucketRegion: os.Getenv("BUCKET_REGION"), | |
} | |
} | |
func getConfig(ctx context.Context)(aws.Config, error) { | |
return config.LoadDefaultConfig(ctx) | |
} | |
func (s *AppState)putToBucket(ctx context.Context, cfg aws.Config, key string, body string){ | |
s3cli:=s3.NewFromConfig(cfg, func(o *s3.Options){ | |
o.Region=s.BucketRegion | |
}) | |
input:=&s3.PutObjectInput{ | |
Bucket: &s.Bucket, | |
Key: &key, | |
Body: strings.NewReader(body), | |
ContentType: aws.String("text/plain"), | |
} | |
res, err:=s3cli.PutObject(ctx, input) | |
if err!=nil{ | |
log.Print(err) | |
}else{ | |
log.Print("etag: ", res.ETag) | |
} | |
} | |
func main(){ | |
ctx:=context.Background() | |
cfg, err:=getConfig(ctx) | |
if err!=nil{ | |
log.Fatal(err) | |
} | |
s:=initState() | |
cw:=cloudwatchlogs.NewFromConfig(cfg) | |
input:=&cloudwatchlogs.DescribeLogStreamsInput{ | |
Descending: aws.Bool(true), | |
LogGroupName: &s.LogGroup, | |
} | |
streams, err:=cw.DescribeLogStreams(ctx, input) | |
if err!=nil{ | |
log.Fatal(err) | |
} | |
for _, stream:=range streams.LogStreams{ | |
streamContents:="" | |
eInput:=&cloudwatchlogs.GetLogEventsInput{ | |
LogStreamName: stream.LogStreamName, | |
LogGroupName: &s.LogGroup, | |
} | |
events, err:=cw.GetLogEvents(ctx, eInput) | |
if err!=nil{ | |
log.Fatal(err) | |
} | |
for _, event :=range events.Events{ | |
if *event.Message!=""{ | |
streamContents+=fmt.Sprintf("%v %v\n", *event.Timestamp, *event.Message) | |
} | |
} | |
if len(events.Events)>0{ | |
s.putToBucket(ctx, cfg, *stream.LogStreamName, streamContents) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment