{"action":"created","comment":{"url":"https://api.github.com/repos/docker/docker/pulls/comments/38002651","id":38002651,"diff_hunk":"@@ -0,0 +1,330 @@\n+// Package awslogs provides the logdriver for forwarding container logs to Amazon CloudWatch Logs\n+package awslogs\n+\n+import (\n+\t\"fmt\"\n+\t\"os\"\n+\t\"sort\"\n+\t\"strings\"\n+\t\"sync\"\n+\t\"time\"\n+\n+\t\"github.com/aws/aws-sdk-go/aws\"\n+\t\"github.com/aws/aws-sdk-go/aws/awserr\"\n+\t\"github.com/aws/aws-sdk-go/service/cloudwatchlogs\"\n+\t\"github.com/docker/docker/daemon/logger\"\n+\t\"github.com/docker/docker/vendor/src/github.com/Sirupsen/logrus\"\n+)\n+\n+const (\n+\tname                  = \"awslogs\"\n+\tregionKey             = \"aws-region\"\n+\tregionEnvKey          = \"AWS_REGION\"\n+\tlogGroupKey           = \"awslogs-group\"\n+\tlogStreamKey          = \"awslogs-stream\"\n+\tbatchPublishFrequency = 5 * time.Second\n+\n+\t// See: http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html\n+\tperEventBytes          = 26\n+\tmaximumBytesPerPut     = 1048576\n+\tmaximumLogEventsPerPut = 10000\n+\n+\t// See: http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/cloudwatch_limits.html\n+\tmaximumBytesPerEvent = 262144 - perEventBytes\n+\n+\tresourceAlreadyExistsCode = \"ResourceAlreadyExistsException\"\n+\tdataAlreadyAcceptedCode   = \"DataAlreadyAcceptedException\"\n+\tinvalidSequenceTokenCode  = \"InvalidSequenceTokenException\"\n+)\n+\n+type logStream struct {\n+\tlogStreamName string\n+\tlogGroupName  string\n+\tcwlogsclient  api\n+\tmessages      chan *logger.Message\n+\tlock          sync.RWMutex\n+\tclosed        bool\n+\tsequenceToken *string\n+}\n+\n+type api interface {\n+\tCreateLogStream(*cloudwatchlogs.CreateLogStreamInput) (*cloudwatchlogs.CreateLogStreamOutput, error)\n+\tPutLogEvents(*cloudwatchlogs.PutLogEventsInput) (*cloudwatchlogs.PutLogEventsOutput, error)\n+}\n+\n+type sortableevents []*cloudwatchlogs.InputLogEvent\n+\n+// init registers the awslogs driver and sets the default region, if provided\n+func init() {\n+\tif os.Getenv(regionEnvKey) != \"\" {\n+\t\taws.DefaultConfig.Region = aws.String(os.Getenv(regionEnvKey))\n+\t}\n+\tif err := logger.RegisterLogDriver(name, New); err != nil {\n+\t\tlogrus.Fatal(err)\n+\t}\n+\tif err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {\n+\t\tlogrus.Fatal(err)\n+\t}\n+}\n+\n+// New creates an awslogs logger using the configuration passed in on the\n+// context.  Supported context configuration variables are aws-region,\n+// awslogs-group, and awslogs-stream.  When available, configuration is\n+// also taken from environment variables AWS_REGION, AWS_ACCESS_KEY_ID,\n+// AWS_SECRET_ACCESS_KEY, the shared credentials file (~/.aws/credentials), and\n+// the EC2 Instance Metadata Service.\n+func New(ctx logger.Context) (logger.Logger, error) {\n+\tlogGroupName := ctx.Config[logGroupKey]\n+\tlogStreamName := ctx.ContainerID\n+\tif ctx.Config[logStreamKey] != \"\" {\n+\t\tlogStreamName = ctx.Config[logStreamKey]\n+\t}\n+\tconfig := aws.DefaultConfig\n+\tif ctx.Config[regionKey] != \"\" {\n+\t\tconfig = aws.DefaultConfig.Merge(&aws.Config{\n+\t\t\tRegion: aws.String(ctx.Config[regionKey]),\n+\t\t})\n+\t}\n+\tcontainerStream := &logStream{\n+\t\tlogStreamName: logStreamName,\n+\t\tlogGroupName:  logGroupName,\n+\t\tcwlogsclient:  cloudwatchlogs.New(config),\n+\t\tmessages:      make(chan *logger.Message, 4096),\n+\t}\n+\terr := containerStream.create()\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\tgo containerStream.collectBatch()\n+\n+\treturn containerStream, nil\n+}\n+\n+// Name returns the name of the awslogs logging driver\n+func (l *logStream) Name() string {\n+\treturn name\n+}\n+\n+// Log submits messages for logging by an instance of the awslogs logging driver\n+func (l *logStream) Log(msg *logger.Message) error {\n+\tl.lock.RLock()\n+\tdefer l.lock.RUnlock()\n+\tif !l.closed {\n+\t\tl.messages <- msg\n+\t}\n+\treturn nil\n+}\n+\n+// Close closes the instance of the awslogs logging driver\n+func (l *logStream) Close() error {\n+\tl.lock.Lock()\n+\tdefer l.lock.Unlock()\n+\tif !l.closed {\n+\t\tclose(l.messages)\n+\t}\n+\tl.closed = true\n+\treturn nil\n+}\n+\n+// create creates a log stream for the instance of the awslogs logging driver\n+func (l *logStream) create() error {\n+\tinput := &cloudwatchlogs.CreateLogStreamInput{\n+\t\tLogGroupName:  aws.String(l.logGroupName),\n+\t\tLogStreamName: aws.String(l.logStreamName),\n+\t}\n+\n+\t_, err := l.cwlogsclient.CreateLogStream(input)\n+\n+\tif err != nil {\n+\t\tif awsErr, ok := err.(awserr.Error); ok {\n+\t\t\tfields := logrus.Fields{\n+\t\t\t\t\"errorCode\":     awsErr.Code(),\n+\t\t\t\t\"message\":       awsErr.Message(),\n+\t\t\t\t\"origError\":     awsErr.OrigErr(),\n+\t\t\t\t\"logGroupName\":  l.logGroupName,\n+\t\t\t\t\"logStreamName\": l.logStreamName,\n+\t\t\t}\n+\t\t\tif awsErr.Code() == resourceAlreadyExistsCode {\n+\t\t\t\t// Allow creation to succeed\n+\t\t\t\tlogrus.WithFields(fields).Warn(\"Log stream already exists\")","path":"daemon/logger/awslogs/cloudwatchlogs.go","position":149,"original_position":149,"commit_id":"982666ce495952c7ebaeba12b7e396dd30666c00","original_commit_id":"982666ce495952c7ebaeba12b7e396dd30666c00","user":{"login":"samuelkarp","id":737750,"avatar_url":"https://avatars.githubusercontent.com/u/737750?v=3","gravatar_id":"","url":"https://api.github.com/users/samuelkarp","html_url":"https://github.com/samuelkarp","followers_url":"https://api.github.com/users/samuelkarp/followers","following_url":"https://api.github.com/users/samuelkarp/following{/other_user}","gists_url":"https://api.github.com/users/samuelkarp/gists{/gist_id}","starred_url":"https://api.github.com/users/samuelkarp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samuelkarp/subscriptions","organizations_url":"https://api.github.com/users/samuelkarp/orgs","repos_url":"https://api.github.com/users/samuelkarp/repos","events_url":"https://api.github.com/users/samuelkarp/events{/privacy}","received_events_url":"https://api.github.com/users/samuelkarp/received_events","type":"User","site_admin":false},"body":"I think I'd lean more toward Info level, as multiple containers writing to the same log stream at the same time can have performance implications.","created_at":"2015-08-26T16:25:42Z","updated_at":"2015-08-26T16:25:42Z","html_url":"https://github.com/docker/docker/pull/15495#discussion_r38002651","pull_request_url":"https://api.github.com/repos/docker/docker/pulls/15495","_links":{"self":{"href":"https://api.github.com/repos/docker/docker/pulls/comments/38002651"},"html":{"href":"https://github.com/docker/docker/pull/15495#discussion_r38002651"},"pull_request":{"href":"https://api.github.com/repos/docker/docker/pulls/15495"}}},"pull_request":{"url":"https://api.github.com/repos/docker/docker/pulls/15495","id":42178181,"html_url":"https://github.com/docker/docker/pull/15495","diff_url":"https://github.com/docker/docker/pull/15495.diff","patch_url":"https://github.com/docker/docker/pull/15495.patch","issue_url":"https://api.github.com/repos/docker/docker/issues/15495","number":15495,"state":"open","locked":false,"title":"Add awslogs driver for Amazon CloudWatch Logs","user":{"login":"samuelkarp","id":737750,"avatar_url":"https://avatars.githubusercontent.com/u/737750?v=3","gravatar_id":"","url":"https://api.github.com/users/samuelkarp","html_url":"https://github.com/samuelkarp","followers_url":"https://api.github.com/users/samuelkarp/followers","following_url":"https://api.github.com/users/samuelkarp/following{/other_user}","gists_url":"https://api.github.com/users/samuelkarp/gists{/gist_id}","starred_url":"https://api.github.com/users/samuelkarp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samuelkarp/subscriptions","organizations_url":"https://api.github.com/users/samuelkarp/orgs","repos_url":"https://api.github.com/users/samuelkarp/repos","events_url":"https://api.github.com/users/samuelkarp/events{/privacy}","received_events_url":"https://api.github.com/users/samuelkarp/received_events","type":"User","site_admin":false},"body":"This pull request adds a new logging driver `awslogs` which sends log events to Amazon CloudWatch Logs.","created_at":"2015-08-11T21:08:19Z","updated_at":"2015-08-26T16:25:42Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ac8070e1126f68517aa20f197c1e245e9088e452","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/docker/docker/pulls/15495/commits","review_comments_url":"https://api.github.com/repos/docker/docker/pulls/15495/comments","review_comment_url":"https://api.github.com/repos/docker/docker/pulls/comments{/number}","comments_url":"https://api.github.com/repos/docker/docker/issues/15495/comments","statuses_url":"https://api.github.com/repos/docker/docker/statuses/982666ce495952c7ebaeba12b7e396dd30666c00","head":{"label":"samuelkarp:awslogs-logging-driver","ref":"awslogs-logging-driver","sha":"982666ce495952c7ebaeba12b7e396dd30666c00","user":{"login":"samuelkarp","id":737750,"avatar_url":"https://avatars.githubusercontent.com/u/737750?v=3","gravatar_id":"","url":"https://api.github.com/users/samuelkarp","html_url":"https://github.com/samuelkarp","followers_url":"https://api.github.com/users/samuelkarp/followers","following_url":"https://api.github.com/users/samuelkarp/following{/other_user}","gists_url":"https://api.github.com/users/samuelkarp/gists{/gist_id}","starred_url":"https://api.github.com/users/samuelkarp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samuelkarp/subscriptions","organizations_url":"https://api.github.com/users/samuelkarp/orgs","repos_url":"https://api.github.com/users/samuelkarp/repos","events_url":"https://api.github.com/users/samuelkarp/events{/privacy}","received_events_url":"https://api.github.com/users/samuelkarp/received_events","type":"User","site_admin":false},"repo":{"id":36832556,"name":"docker","full_name":"samuelkarp/docker","owner":{"login":"samuelkarp","id":737750,"avatar_url":"https://avatars.githubusercontent.com/u/737750?v=3","gravatar_id":"","url":"https://api.github.com/users/samuelkarp","html_url":"https://github.com/samuelkarp","followers_url":"https://api.github.com/users/samuelkarp/followers","following_url":"https://api.github.com/users/samuelkarp/following{/other_user}","gists_url":"https://api.github.com/users/samuelkarp/gists{/gist_id}","starred_url":"https://api.github.com/users/samuelkarp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samuelkarp/subscriptions","organizations_url":"https://api.github.com/users/samuelkarp/orgs","repos_url":"https://api.github.com/users/samuelkarp/repos","events_url":"https://api.github.com/users/samuelkarp/events{/privacy}","received_events_url":"https://api.github.com/users/samuelkarp/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/samuelkarp/docker","description":"Docker - the open-source application container engine","fork":true,"url":"https://api.github.com/repos/samuelkarp/docker","forks_url":"https://api.github.com/repos/samuelkarp/docker/forks","keys_url":"https://api.github.com/repos/samuelkarp/docker/keys{/key_id}","collaborators_url":"https://api.github.com/repos/samuelkarp/docker/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/samuelkarp/docker/teams","hooks_url":"https://api.github.com/repos/samuelkarp/docker/hooks","issue_events_url":"https://api.github.com/repos/samuelkarp/docker/issues/events{/number}","events_url":"https://api.github.com/repos/samuelkarp/docker/events","assignees_url":"https://api.github.com/repos/samuelkarp/docker/assignees{/user}","branches_url":"https://api.github.com/repos/samuelkarp/docker/branches{/branch}","tags_url":"https://api.github.com/repos/samuelkarp/docker/tags","blobs_url":"https://api.github.com/repos/samuelkarp/docker/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/samuelkarp/docker/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/samuelkarp/docker/git/refs{/sha}","trees_url":"https://api.github.com/repos/samuelkarp/docker/git/trees{/sha}","statuses_url":"https://api.github.com/repos/samuelkarp/docker/statuses/{sha}","languages_url":"https://api.github.com/repos/samuelkarp/docker/languages","stargazers_url":"https://api.github.com/repos/samuelkarp/docker/stargazers","contributors_url":"https://api.github.com/repos/samuelkarp/docker/contributors","subscribers_url":"https://api.github.com/repos/samuelkarp/docker/subscribers","subscription_url":"https://api.github.com/repos/samuelkarp/docker/subscription","commits_url":"https://api.github.com/repos/samuelkarp/docker/commits{/sha}","git_commits_url":"https://api.github.com/repos/samuelkarp/docker/git/commits{/sha}","comments_url":"https://api.github.com/repos/samuelkarp/docker/comments{/number}","issue_comment_url":"https://api.github.com/repos/samuelkarp/docker/issues/comments{/number}","contents_url":"https://api.github.com/repos/samuelkarp/docker/contents/{+path}","compare_url":"https://api.github.com/repos/samuelkarp/docker/compare/{base}...{head}","merges_url":"https://api.github.com/repos/samuelkarp/docker/merges","archive_url":"https://api.github.com/repos/samuelkarp/docker/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/samuelkarp/docker/downloads","issues_url":"https://api.github.com/repos/samuelkarp/docker/issues{/number}","pulls_url":"https://api.github.com/repos/samuelkarp/docker/pulls{/number}","milestones_url":"https://api.github.com/repos/samuelkarp/docker/milestones{/number}","notifications_url":"https://api.github.com/repos/samuelkarp/docker/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/samuelkarp/docker/labels{/name}","releases_url":"https://api.github.com/repos/samuelkarp/docker/releases{/id}","created_at":"2015-06-03T21:49:41Z","updated_at":"2015-06-03T21:49:48Z","pushed_at":"2015-08-26T05:02:21Z","git_url":"git://github.com/samuelkarp/docker.git","ssh_url":"git@github.com:samuelkarp/docker.git","clone_url":"https://github.com/samuelkarp/docker.git","svn_url":"https://github.com/samuelkarp/docker","homepage":"http://www.docker.com","size":65930,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"docker:master","ref":"master","sha":"8720f71b522fd0a7abbd47374b27e11d7f1beb7d","user":{"login":"docker","id":5429470,"avatar_url":"https://avatars.githubusercontent.com/u/5429470?v=3","gravatar_id":"","url":"https://api.github.com/users/docker","html_url":"https://github.com/docker","followers_url":"https://api.github.com/users/docker/followers","following_url":"https://api.github.com/users/docker/following{/other_user}","gists_url":"https://api.github.com/users/docker/gists{/gist_id}","starred_url":"https://api.github.com/users/docker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/docker/subscriptions","organizations_url":"https://api.github.com/users/docker/orgs","repos_url":"https://api.github.com/users/docker/repos","events_url":"https://api.github.com/users/docker/events{/privacy}","received_events_url":"https://api.github.com/users/docker/received_events","type":"Organization","site_admin":false},"repo":{"id":7691631,"name":"docker","full_name":"docker/docker","owner":{"login":"docker","id":5429470,"avatar_url":"https://avatars.githubusercontent.com/u/5429470?v=3","gravatar_id":"","url":"https://api.github.com/users/docker","html_url":"https://github.com/docker","followers_url":"https://api.github.com/users/docker/followers","following_url":"https://api.github.com/users/docker/following{/other_user}","gists_url":"https://api.github.com/users/docker/gists{/gist_id}","starred_url":"https://api.github.com/users/docker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/docker/subscriptions","organizations_url":"https://api.github.com/users/docker/orgs","repos_url":"https://api.github.com/users/docker/repos","events_url":"https://api.github.com/users/docker/events{/privacy}","received_events_url":"https://api.github.com/users/docker/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/docker/docker","description":"Docker - the open-source application container engine","fork":false,"url":"https://api.github.com/repos/docker/docker","forks_url":"https://api.github.com/repos/docker/docker/forks","keys_url":"https://api.github.com/repos/docker/docker/keys{/key_id}","collaborators_url":"https://api.github.com/repos/docker/docker/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/docker/docker/teams","hooks_url":"https://api.github.com/repos/docker/docker/hooks","issue_events_url":"https://api.github.com/repos/docker/docker/issues/events{/number}","events_url":"https://api.github.com/repos/docker/docker/events","assignees_url":"https://api.github.com/repos/docker/docker/assignees{/user}","branches_url":"https://api.github.com/repos/docker/docker/branches{/branch}","tags_url":"https://api.github.com/repos/docker/docker/tags","blobs_url":"https://api.github.com/repos/docker/docker/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/docker/docker/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/docker/docker/git/refs{/sha}","trees_url":"https://api.github.com/repos/docker/docker/git/trees{/sha}","statuses_url":"https://api.github.com/repos/docker/docker/statuses/{sha}","languages_url":"https://api.github.com/repos/docker/docker/languages","stargazers_url":"https://api.github.com/repos/docker/docker/stargazers","contributors_url":"https://api.github.com/repos/docker/docker/contributors","subscribers_url":"https://api.github.com/repos/docker/docker/subscribers","subscription_url":"https://api.github.com/repos/docker/docker/subscription","commits_url":"https://api.github.com/repos/docker/docker/commits{/sha}","git_commits_url":"https://api.github.com/repos/docker/docker/git/commits{/sha}","comments_url":"https://api.github.com/repos/docker/docker/comments{/number}","issue_comment_url":"https://api.github.com/repos/docker/docker/issues/comments{/number}","contents_url":"https://api.github.com/repos/docker/docker/contents/{+path}","compare_url":"https://api.github.com/repos/docker/docker/compare/{base}...{head}","merges_url":"https://api.github.com/repos/docker/docker/merges","archive_url":"https://api.github.com/repos/docker/docker/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/docker/docker/downloads","issues_url":"https://api.github.com/repos/docker/docker/issues{/number}","pulls_url":"https://api.github.com/repos/docker/docker/pulls{/number}","milestones_url":"https://api.github.com/repos/docker/docker/milestones{/number}","notifications_url":"https://api.github.com/repos/docker/docker/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/docker/docker/labels{/name}","releases_url":"https://api.github.com/repos/docker/docker/releases{/id}","created_at":"2013-01-18T18:10:57Z","updated_at":"2015-08-26T16:21:18Z","pushed_at":"2015-08-26T15:35:03Z","git_url":"git://github.com/docker/docker.git","ssh_url":"git@github.com:docker/docker.git","clone_url":"https://github.com/docker/docker.git","svn_url":"https://github.com/docker/docker","homepage":"http://www.docker.com","size":246867,"stargazers_count":24319,"watchers_count":24319,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":6220,"mirror_url":null,"open_issues_count":1040,"forks":6220,"open_issues":1040,"watchers":24319,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/docker/docker/pulls/15495"},"html":{"href":"https://github.com/docker/docker/pull/15495"},"issue":{"href":"https://api.github.com/repos/docker/docker/issues/15495"},"comments":{"href":"https://api.github.com/repos/docker/docker/issues/15495/comments"},"review_comments":{"href":"https://api.github.com/repos/docker/docker/pulls/15495/comments"},"review_comment":{"href":"https://api.github.com/repos/docker/docker/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/docker/docker/pulls/15495/commits"},"statuses":{"href":"https://api.github.com/repos/docker/docker/statuses/982666ce495952c7ebaeba12b7e396dd30666c00"}}}}