Created
August 31, 2016 18:19
-
-
Save Xe/f0c040c98799637dd41890560c10b38f 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 ( | |
"errors" | |
"fmt" | |
"strings" | |
"time" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/awserr" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/cloudwatchlogs" | |
) | |
var ( | |
cwl *cloudwatchlogs.CloudWatchLogs | |
userID = "Xe" | |
data = "Test in hope of harambe" | |
) | |
func main() { | |
sess, err := session.NewSession(&aws.Config{Region: aws.String("us-west-2")}) | |
if err != nil { | |
fmt.Println("failed to create session,", err) | |
return | |
} | |
cwl = cloudwatchlogs.New(sess) | |
err = createStreamIfNotExists(userID) | |
if err != nil { | |
panic(err) | |
} | |
sequenceToken, now, err := getSequenceToken(userID) | |
if err != nil { | |
panic(err) | |
} | |
err = submitLogMessage(string(data), userID, *sequenceToken, now) | |
if err != nil { | |
panic(err) | |
} | |
} | |
func createStreamIfNotExists(userID string) error { | |
csi := &cloudwatchlogs.CreateLogStreamInput{ | |
LogGroupName: aws.String("userlogs"), | |
LogStreamName: aws.String(userID), | |
} | |
_, err := cwl.CreateLogStream(csi) | |
if err != nil { | |
aerr, ok := err.(awserr.Error) | |
if !ok { | |
return err | |
} | |
if strings.HasPrefix(aerr.Code(), "ResourceAlreadyExistsException") { | |
return nil | |
} | |
return err | |
} | |
return nil | |
} | |
func getSequenceToken(userID string) (*string, time.Time, error) { | |
input := &cloudwatchlogs.DescribeLogStreamsInput{ | |
Limit: aws.Int64(1), | |
LogGroupName: aws.String("userlogs"), | |
LogStreamNamePrefix: aws.String(userID), | |
} | |
now := time.Now() | |
logStreams, err := cwl.DescribeLogStreams(input) | |
if err != nil { | |
return aws.String(""), now, err | |
} | |
if len(logStreams.LogStreams) != 1 { | |
return aws.String(""), now, errors.New("userlog: specified logging group does not exist") | |
} | |
token := logStreams.LogStreams[0].UploadSequenceToken | |
return token, now, nil | |
} | |
func submitLogMessage(data, userID, sequenceToken string, now time.Time) error { | |
logInput := &cloudwatchlogs.PutLogEventsInput{ | |
LogEvents: []*cloudwatchlogs.InputLogEvent{ | |
{ | |
Message: aws.String(string(data)), | |
Timestamp: aws.Int64(now.Unix()), | |
}, | |
{ | |
Message: aws.String("hi test please work thanks <3"), | |
Timestamp: aws.Int64(now.Unix()), | |
}, | |
}, | |
LogGroupName: aws.String("userlogs"), | |
LogStreamName: aws.String(userID), | |
SequenceToken: aws.String(sequenceToken), | |
} | |
_, err := cwl.PutLogEvents(logInput) | |
if err != nil { | |
if strings.HasPrefix(err.Error(), "InvalidSequenceTokenException") { | |
return submitLogMessage(data, userID, strings.Split(err.Error(), " ")[11], now) | |
} | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment