{"action":"created","comment":{"url":"https://api.github.com/repos/aws/amazon-ecs-agent/pulls/comments/31080059","id":31080059,"diff_hunk":"@@ -0,0 +1,138 @@\n+// Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.\n+//\n+// Licensed under the Apache License, Version 2.0 (the \"License\"). You may\n+// not use this file except in compliance with the License. A copy of the\n+// License is located at\n+//\n+//\thttp://aws.amazon.com/apache2.0/\n+//\n+// or in the \"license\" file accompanying this file. This file is distributed\n+// on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either\n+// express or implied. See the License for the specific language governing\n+// permissions and limitations under the License.\n+\n+package tcsclient\n+\n+import (\n+\t\"bytes\"\n+\t\"errors\"\n+\t\"net/http\"\n+\t\"time\"\n+\n+\t\"github.com/aws/amazon-ecs-agent/agent/ecs_client/authv4\"\n+\t\"github.com/aws/amazon-ecs-agent/agent/ecs_client/authv4/credentials\"\n+\t\"github.com/aws/amazon-ecs-agent/agent/logger\"\n+\t\"github.com/aws/amazon-ecs-agent/agent/stats\"\n+\t\"github.com/aws/amazon-ecs-agent/agent/tcs/model/ecstcs\"\n+\t\"github.com/aws/amazon-ecs-agent/agent/wsclient\"\n+\t\"github.com/gorilla/websocket\"\n+)\n+\n+var log = logger.ForModule(\"tcs client\")\n+\n+// clientServer implements wsclient.ClientServer interface for metrics backend.\n+type clientServer struct {\n+\tstatsEngine  stats.Engine\n+\tpublishTimer *timer\n+\twsclient.ClientServerImpl\n+\tsigner authv4.HttpSigner\n+}\n+\n+// New returns a client/server to bidirectionally communicate with the backend.\n+// The returned struct should have both 'Connect' and 'Serve' called upon it\n+// before being used.\n+func New(url string, region string, credentialProvider credentials.AWSCredentialProvider, acceptInvalidCert bool, statsEngine stats.Engine, publishMetricsInterval time.Duration) wsclient.ClientServer {\n+\tcs := &clientServer{\n+\t\tstatsEngine:  statsEngine,\n+\t\tpublishTimer: newTimer(publishMetricsInterval, publishMetrics),\n+\t\tsigner:       authv4.NewHttpSigner(region, wsclient.ServiceName, credentialProvider, nil),\n+\t}\n+\tcs.URL = url\n+\tcs.Region = region\n+\tcs.CredentialProvider = credentialProvider\n+\tcs.AcceptInvalidCert = acceptInvalidCert\n+\tcs.ServiceError = &tcsError{}\n+\tcs.RequestHandlers = make(map[string]wsclient.RequestHandler)\n+\tcs.TypeDecoder = &TcsDecoder{}\n+\treturn cs\n+}\n+\n+// Serve begins serving requests using previously registered handlers (see\n+// AddRequestHandler). All request handlers should be added prior to making this\n+// call as unhandled requests will be discarded.\n+func (cs *clientServer) Serve() error {\n+\tlog.Debug(\"Starting websocket poll loop\")\n+\tif cs.Conn == nil {\n+\t\treturn errors.New(\"nil connection\")\n+\t}\n+\n+\t// Start the timer function to publish metrics to the backend.\n+\tif cs.statsEngine != nil {\n+\t\tcs.publishTimer.start(cs)\n+\t}\n+\n+\treturn cs.ConsumeMessages()\n+}\n+\n+// MakeRequest makes a request using the given input. Note, the input *MUST* be\n+// a pointer to a valid backend type that this client recognises\n+func (cs *clientServer) MakeRequest(input interface{}) error {\n+\tpayload, err := cs.CreateRequestMessage(input)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\n+\tdata := cs.signRequest(payload)\n+\n+\t// Over the wire we send something like\n+\t// {\"type\":\"AckRequest\",\"message\":{\"messageId\":\"xyz\"}}\n+\t// return cs.Conn.WriteMessage(websocket.TextMessage, send)\n+\treturn cs.Conn.WriteMessage(websocket.TextMessage, data)\n+}\n+\n+func (cs *clientServer) signRequest(payload []byte) []byte {\n+\tsigner := authv4.NewHttpSigner(cs.Region, \"ecs\", cs.CredentialProvider, nil)\n+\treqBody := bytes.NewBuffer(payload)\n+\t// NewRequest never returns an error if the url parses and we just verified\n+\t// it did above\n+\trequest, _ := http.NewRequest(\"GET\", cs.URL, reqBody)\n+\tsigner.SignHttpRequest(request)\n+\n+\tvar data []byte\n+\tfor k, vs := range request.Header {\n+\t\tfor _, v := range vs {\n+\t\t\tdata = append(data, k...)\n+\t\t\tdata = append(data, \": \"...)\n+\t\t\tdata = append(data, v...)\n+\t\t\tdata = append(data, \"\\r\\n\"...)\n+\t\t}\n+\t}\n+\tdata = append(data, \"\\r\\n\"...)\n+\tdata = append(data, payload...)\n+\n+\treturn data\n+}\n+\n+// Close closes the underlying connection.\n+func (cs *clientServer) Close() error {\n+\tcs.publishTimer.stop()\n+\tif cs.Conn != nil {\n+\t\treturn cs.Conn.Close()\n+\t}\n+\treturn nil\n+}\n+\n+// publishMetrics invokes the PublishMetricsRequest on the clientserver object.\n+// The argument must be of the clientServer type.\n+func publishMetrics(cs interface{}) error {\n+\tclsrv, ok := cs.(*clientServer)","path":"agent/tcs/client/client.go","position":128,"original_position":128,"commit_id":"170aad686112deb2679db302d7fe8308246d0af4","original_commit_id":"170aad686112deb2679db302d7fe8308246d0af4","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":"What is `clsrv` here?  A more descriptive variable name would be appreciated.","created_at":"2015-05-26T21:08:46Z","updated_at":"2015-05-26T21:08:46Z","html_url":"https://github.com/aws/amazon-ecs-agent/pull/91#discussion_r31080059","pull_request_url":"https://api.github.com/repos/aws/amazon-ecs-agent/pulls/91","_links":{"self":{"href":"https://api.github.com/repos/aws/amazon-ecs-agent/pulls/comments/31080059"},"html":{"href":"https://github.com/aws/amazon-ecs-agent/pull/91#discussion_r31080059"},"pull_request":{"href":"https://api.github.com/repos/aws/amazon-ecs-agent/pulls/91"}}},"pull_request":{"url":"https://api.github.com/repos/aws/amazon-ecs-agent/pulls/91","id":36169269,"html_url":"https://github.com/aws/amazon-ecs-agent/pull/91","diff_url":"https://github.com/aws/amazon-ecs-agent/pull/91.diff","patch_url":"https://github.com/aws/amazon-ecs-agent/pull/91.patch","issue_url":"https://api.github.com/repos/aws/amazon-ecs-agent/issues/91","number":91,"state":"open","locked":false,"title":"Telemetry Service communication integration","user":{"login":"aaithal","id":1183359,"avatar_url":"https://avatars.githubusercontent.com/u/1183359?v=3","gravatar_id":"","url":"https://api.github.com/users/aaithal","html_url":"https://github.com/aaithal","followers_url":"https://api.github.com/users/aaithal/followers","following_url":"https://api.github.com/users/aaithal/following{/other_user}","gists_url":"https://api.github.com/users/aaithal/gists{/gist_id}","starred_url":"https://api.github.com/users/aaithal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaithal/subscriptions","organizations_url":"https://api.github.com/users/aaithal/orgs","repos_url":"https://api.github.com/users/aaithal/repos","events_url":"https://api.github.com/users/aaithal/events{/privacy}","received_events_url":"https://api.github.com/users/aaithal/received_events","type":"User","site_admin":false},"body":"r? @samuelkarp @euank ","created_at":"2015-05-26T04:39:55Z","updated_at":"2015-05-26T21:08:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"726ec747a426dffcf638a3e451eded642aff9c9a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/aws/amazon-ecs-agent/pulls/91/commits","review_comments_url":"https://api.github.com/repos/aws/amazon-ecs-agent/pulls/91/comments","review_comment_url":"https://api.github.com/repos/aws/amazon-ecs-agent/pulls/comments{/number}","comments_url":"https://api.github.com/repos/aws/amazon-ecs-agent/issues/91/comments","statuses_url":"https://api.github.com/repos/aws/amazon-ecs-agent/statuses/170aad686112deb2679db302d7fe8308246d0af4","head":{"label":"aaithal:TelemetryCommunicationServiceIntegration","ref":"TelemetryCommunicationServiceIntegration","sha":"170aad686112deb2679db302d7fe8308246d0af4","user":{"login":"aaithal","id":1183359,"avatar_url":"https://avatars.githubusercontent.com/u/1183359?v=3","gravatar_id":"","url":"https://api.github.com/users/aaithal","html_url":"https://github.com/aaithal","followers_url":"https://api.github.com/users/aaithal/followers","following_url":"https://api.github.com/users/aaithal/following{/other_user}","gists_url":"https://api.github.com/users/aaithal/gists{/gist_id}","starred_url":"https://api.github.com/users/aaithal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaithal/subscriptions","organizations_url":"https://api.github.com/users/aaithal/orgs","repos_url":"https://api.github.com/users/aaithal/repos","events_url":"https://api.github.com/users/aaithal/events{/privacy}","received_events_url":"https://api.github.com/users/aaithal/received_events","type":"User","site_admin":false},"repo":{"id":30386191,"name":"amazon-ecs-agent","full_name":"aaithal/amazon-ecs-agent","owner":{"login":"aaithal","id":1183359,"avatar_url":"https://avatars.githubusercontent.com/u/1183359?v=3","gravatar_id":"","url":"https://api.github.com/users/aaithal","html_url":"https://github.com/aaithal","followers_url":"https://api.github.com/users/aaithal/followers","following_url":"https://api.github.com/users/aaithal/following{/other_user}","gists_url":"https://api.github.com/users/aaithal/gists{/gist_id}","starred_url":"https://api.github.com/users/aaithal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaithal/subscriptions","organizations_url":"https://api.github.com/users/aaithal/orgs","repos_url":"https://api.github.com/users/aaithal/repos","events_url":"https://api.github.com/users/aaithal/events{/privacy}","received_events_url":"https://api.github.com/users/aaithal/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/aaithal/amazon-ecs-agent","description":"Amazon EC2 Container Service Agent","fork":true,"url":"https://api.github.com/repos/aaithal/amazon-ecs-agent","forks_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/forks","keys_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/teams","hooks_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/hooks","issue_events_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/issues/events{/number}","events_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/events","assignees_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/assignees{/user}","branches_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/branches{/branch}","tags_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/tags","blobs_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/git/refs{/sha}","trees_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/statuses/{sha}","languages_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/languages","stargazers_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/stargazers","contributors_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/contributors","subscribers_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/subscribers","subscription_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/subscription","commits_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/commits{/sha}","git_commits_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/git/commits{/sha}","comments_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/comments{/number}","issue_comment_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/issues/comments{/number}","contents_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/contents/{+path}","compare_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/merges","archive_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/downloads","issues_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/issues{/number}","pulls_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/pulls{/number}","milestones_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/milestones{/number}","notifications_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/labels{/name}","releases_url":"https://api.github.com/repos/aaithal/amazon-ecs-agent/releases{/id}","created_at":"2015-02-06T00:24:31Z","updated_at":"2015-04-27T22:13:41Z","pushed_at":"2015-05-26T17:58:15Z","git_url":"git://github.com/aaithal/amazon-ecs-agent.git","ssh_url":"git@github.com:aaithal/amazon-ecs-agent.git","clone_url":"https://github.com/aaithal/amazon-ecs-agent.git","svn_url":"https://github.com/aaithal/amazon-ecs-agent","homepage":"http://aws.amazon.com/ecs/","size":1343,"stargazers_count":2,"watchers_count":2,"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":2,"default_branch":"master"}},"base":{"label":"aws:dev","ref":"dev","sha":"5db695abc874cc76b5970959cf4ca49595867fe4","user":{"login":"aws","id":2232217,"avatar_url":"https://avatars.githubusercontent.com/u/2232217?v=3","gravatar_id":"","url":"https://api.github.com/users/aws","html_url":"https://github.com/aws","followers_url":"https://api.github.com/users/aws/followers","following_url":"https://api.github.com/users/aws/following{/other_user}","gists_url":"https://api.github.com/users/aws/gists{/gist_id}","starred_url":"https://api.github.com/users/aws/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aws/subscriptions","organizations_url":"https://api.github.com/users/aws/orgs","repos_url":"https://api.github.com/users/aws/repos","events_url":"https://api.github.com/users/aws/events{/privacy}","received_events_url":"https://api.github.com/users/aws/received_events","type":"Organization","site_admin":false},"repo":{"id":27551774,"name":"amazon-ecs-agent","full_name":"aws/amazon-ecs-agent","owner":{"login":"aws","id":2232217,"avatar_url":"https://avatars.githubusercontent.com/u/2232217?v=3","gravatar_id":"","url":"https://api.github.com/users/aws","html_url":"https://github.com/aws","followers_url":"https://api.github.com/users/aws/followers","following_url":"https://api.github.com/users/aws/following{/other_user}","gists_url":"https://api.github.com/users/aws/gists{/gist_id}","starred_url":"https://api.github.com/users/aws/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aws/subscriptions","organizations_url":"https://api.github.com/users/aws/orgs","repos_url":"https://api.github.com/users/aws/repos","events_url":"https://api.github.com/users/aws/events{/privacy}","received_events_url":"https://api.github.com/users/aws/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/aws/amazon-ecs-agent","description":"Amazon EC2 Container Service Agent","fork":false,"url":"https://api.github.com/repos/aws/amazon-ecs-agent","forks_url":"https://api.github.com/repos/aws/amazon-ecs-agent/forks","keys_url":"https://api.github.com/repos/aws/amazon-ecs-agent/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aws/amazon-ecs-agent/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aws/amazon-ecs-agent/teams","hooks_url":"https://api.github.com/repos/aws/amazon-ecs-agent/hooks","issue_events_url":"https://api.github.com/repos/aws/amazon-ecs-agent/issues/events{/number}","events_url":"https://api.github.com/repos/aws/amazon-ecs-agent/events","assignees_url":"https://api.github.com/repos/aws/amazon-ecs-agent/assignees{/user}","branches_url":"https://api.github.com/repos/aws/amazon-ecs-agent/branches{/branch}","tags_url":"https://api.github.com/repos/aws/amazon-ecs-agent/tags","blobs_url":"https://api.github.com/repos/aws/amazon-ecs-agent/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aws/amazon-ecs-agent/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aws/amazon-ecs-agent/git/refs{/sha}","trees_url":"https://api.github.com/repos/aws/amazon-ecs-agent/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aws/amazon-ecs-agent/statuses/{sha}","languages_url":"https://api.github.com/repos/aws/amazon-ecs-agent/languages","stargazers_url":"https://api.github.com/repos/aws/amazon-ecs-agent/stargazers","contributors_url":"https://api.github.com/repos/aws/amazon-ecs-agent/contributors","subscribers_url":"https://api.github.com/repos/aws/amazon-ecs-agent/subscribers","subscription_url":"https://api.github.com/repos/aws/amazon-ecs-agent/subscription","commits_url":"https://api.github.com/repos/aws/amazon-ecs-agent/commits{/sha}","git_commits_url":"https://api.github.com/repos/aws/amazon-ecs-agent/git/commits{/sha}","comments_url":"https://api.github.com/repos/aws/amazon-ecs-agent/comments{/number}","issue_comment_url":"https://api.github.com/repos/aws/amazon-ecs-agent/issues/comments{/number}","contents_url":"https://api.github.com/repos/aws/amazon-ecs-agent/contents/{+path}","compare_url":"https://api.github.com/repos/aws/amazon-ecs-agent/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aws/amazon-ecs-agent/merges","archive_url":"https://api.github.com/repos/aws/amazon-ecs-agent/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aws/amazon-ecs-agent/downloads","issues_url":"https://api.github.com/repos/aws/amazon-ecs-agent/issues{/number}","pulls_url":"https://api.github.com/repos/aws/amazon-ecs-agent/pulls{/number}","milestones_url":"https://api.github.com/repos/aws/amazon-ecs-agent/milestones{/number}","notifications_url":"https://api.github.com/repos/aws/amazon-ecs-agent/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aws/amazon-ecs-agent/labels{/name}","releases_url":"https://api.github.com/repos/aws/amazon-ecs-agent/releases{/id}","created_at":"2014-12-04T17:31:49Z","updated_at":"2015-05-26T20:14:42Z","pushed_at":"2015-05-26T18:31:26Z","git_url":"git://github.com/aws/amazon-ecs-agent.git","ssh_url":"git@github.com:aws/amazon-ecs-agent.git","clone_url":"https://github.com/aws/amazon-ecs-agent.git","svn_url":"https://github.com/aws/amazon-ecs-agent","homepage":"http://aws.amazon.com/ecs/","size":2494,"stargazers_count":225,"watchers_count":225,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":33,"mirror_url":null,"open_issues_count":12,"forks":33,"open_issues":12,"watchers":225,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/aws/amazon-ecs-agent/pulls/91"},"html":{"href":"https://github.com/aws/amazon-ecs-agent/pull/91"},"issue":{"href":"https://api.github.com/repos/aws/amazon-ecs-agent/issues/91"},"comments":{"href":"https://api.github.com/repos/aws/amazon-ecs-agent/issues/91/comments"},"review_comments":{"href":"https://api.github.com/repos/aws/amazon-ecs-agent/pulls/91/comments"},"review_comment":{"href":"https://api.github.com/repos/aws/amazon-ecs-agent/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/aws/amazon-ecs-agent/pulls/91/commits"},"statuses":{"href":"https://api.github.com/repos/aws/amazon-ecs-agent/statuses/170aad686112deb2679db302d7fe8308246d0af4"}}}}