{"action":"created","comment":{"url":"https://api.github.com/repos/apache/incubator-beam/pulls/comments/81620579","pull_request_review_id":2563192,"id":81620579,"diff_hunk":"@@ -0,0 +1,201 @@\n+/*\n+ * Licensed to the Apache Software Foundation (ASF) under one\n+ * or more contributor license agreements.  See the NOTICE file\n+ * distributed with this work for additional information\n+ * regarding copyright ownership.  The ASF licenses this file\n+ * to you under the Apache License, Version 2.0 (the\n+ * \"License\"); you may not use this file except in compliance\n+ * with the License.  You may obtain a copy of the License at\n+ *\n+ *     http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+package org.apache.beam.sdk.metrics;\n+\n+import static com.google.common.base.Preconditions.checkNotNull;\n+\n+import com.google.common.collect.ImmutableList;\n+import java.util.Map;\n+import java.util.concurrent.atomic.AtomicBoolean;\n+import org.apache.beam.sdk.annotations.Experimental;\n+import org.apache.beam.sdk.annotations.Experimental.Kind;\n+import org.apache.beam.sdk.metrics.MetricUpdates.MetricUpdate;\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+/**\n+ * Holds all of the metrics produced for a single step and unit-of-commit.\n+ *\n+ * <p>A thread-local variable holds the {@link MetricsContainer} that should be used for any\n+ * metric updates produced from the executing code by interactions with the user-facing metric\n+ * interfaces (eg., {@link Counter}).\n+ *\n+ * <p>For a given runner to support metrics it is currently necessary to do 3 things:\n+ * <ol>\n+ *   <li>Create a {@link MetricsContainer} for each step that metrics will be reported at.\n+ *   </li>\n+ *   <li>Make sure to call {@link #setMetricsContainer}\n+ *   </li>\n+ *   <li>Use {@link #getCumulative()} to get {@link org.apache.beam.sdk.metrics.MetricUpdates}\n+ *   representing all the metric changes and report/aggregate those appropriately.\n+ *   </li>\n+ *   <li>Optionally, use {@link #getUpdates()} to get only the changes to a metric since the last\n+ *   time and report those periodically. After reporting deltas and before the next call to\n+ *   {@link #getUpdates()} the runner should invoke {@link #commitUpdates} to indicate that those\n+ *   deltas have been incorporated.\n+ *   </li>\n+ * </ol>\n+ *\n+ * <p>This class is thread-safe.\n+ */\n+@Experimental(Kind.METRICS)\n+public class MetricsContainer {\n+\n+  private static final Logger LOGGER = LoggerFactory.getLogger(MetricsContainer.class);\n+  private static final AtomicBoolean REPORTED_MISSING_CONTAINER = new AtomicBoolean(false);\n+\n+  private static final ThreadLocal<MetricsContainer> CONTAINER_FOR_THREAD =\n+      new ThreadLocal<MetricsContainer>();\n+\n+  private final String stepName;\n+\n+  private MetricsMap<MetricName, CounterCell> counters =\n+      new MetricsMap<>(new MetricsMap.Factory<MetricName, CounterCell>() {\n+        @Override\n+        public CounterCell createInstance(MetricName unusedKey) {\n+          return new CounterCell();\n+        }\n+      });\n+\n+  private MetricsMap<MetricName, DistributionCell> distributions =\n+      new MetricsMap<>(new MetricsMap.Factory<MetricName, DistributionCell>() {\n+        @Override\n+        public DistributionCell createInstance(MetricName unusedKey) {\n+          return new DistributionCell();\n+        }\n+      });\n+\n+  /**\n+   * Create a new {@link MetricsContainer} associated with the given {@code stepName}.\n+   */\n+  public MetricsContainer(String stepName) {\n+    this.stepName = stepName;\n+  }\n+\n+  /**\n+   * Set the {@link MetricsContainer} for the current thread.\n+   */\n+  public static void setMetricsContainer(MetricsContainer container) {\n+    CONTAINER_FOR_THREAD.set(container);\n+  }\n+\n+  /**\n+   * Clear the {@link MetricsContainer} for the current thread.\n+   */\n+  public static void unsetMetricsContainer() {\n+    CONTAINER_FOR_THREAD.remove();\n+  }\n+\n+  /**\n+   * Return the {@link MetricsContainer} for the current thread.\n+   */\n+  public static MetricsContainer getCurrentContainer() {\n+    MetricsContainer container = CONTAINER_FOR_THREAD.get();\n+    if (container == null && REPORTED_MISSING_CONTAINER.compareAndSet(false, true)) {\n+      LOGGER.error(\"Unable to get {} for the current thread.\\n\"","path":"sdks/java/core/src/main/java/org/apache/beam/sdk/metrics/MetricsContainer.java","position":110,"original_position":110,"commit_id":"d7ca4a815bd98b4d23a9ee1d0a2cab5e01dff1f9","original_commit_id":"d7ca4a815bd98b4d23a9ee1d0a2cab5e01dff1f9","user":{"login":"swegner","id":674021,"avatar_url":"https://avatars.githubusercontent.com/u/674021?v=3","gravatar_id":"","url":"https://api.github.com/users/swegner","html_url":"https://github.com/swegner","followers_url":"https://api.github.com/users/swegner/followers","following_url":"https://api.github.com/users/swegner/following{/other_user}","gists_url":"https://api.github.com/users/swegner/gists{/gist_id}","starred_url":"https://api.github.com/users/swegner/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/swegner/subscriptions","organizations_url":"https://api.github.com/users/swegner/orgs","repos_url":"https://api.github.com/users/swegner/repos","events_url":"https://api.github.com/users/swegner/events{/privacy}","received_events_url":"https://api.github.com/users/swegner/received_events","type":"User","site_admin":false},"body":"Assuming metrics get baked into standard transforms, this log message will be displayed every time a pipeline is executed on a runner that doesn't yet support metrics. Perhaps make it a bit less scary? (warning instead of error, remove reference to internal `MetricsContainer`, reword message to assume no runner support, ...)\r\n\r\nOr provide a more explicit signal to differentiate between runners which don't support metrics or using metrics on the wrong thread. For example, you could keep a bit that tracks whether `MetricsContainer.setMetricsContainer()` has ever been called.","created_at":"2016-10-03T19:20:28Z","updated_at":"2016-10-03T19:20:48Z","html_url":"https://github.com/apache/incubator-beam/pull/1024#discussion_r81620579","pull_request_url":"https://api.github.com/repos/apache/incubator-beam/pulls/1024","_links":{"self":{"href":"https://api.github.com/repos/apache/incubator-beam/pulls/comments/81620579"},"html":{"href":"https://github.com/apache/incubator-beam/pull/1024#discussion_r81620579"},"pull_request":{"href":"https://api.github.com/repos/apache/incubator-beam/pulls/1024"}}},"pull_request":{"url":"https://api.github.com/repos/apache/incubator-beam/pulls/1024","id":87224195,"html_url":"https://github.com/apache/incubator-beam/pull/1024","diff_url":"https://github.com/apache/incubator-beam/pull/1024.diff","patch_url":"https://github.com/apache/incubator-beam/pull/1024.patch","issue_url":"https://api.github.com/repos/apache/incubator-beam/issues/1024","number":1024,"state":"open","locked":false,"title":"[BEAM-147] Prototype of Metrics API for Java DirectRunner","user":{"login":"bjchambers","id":35960,"avatar_url":"https://avatars.githubusercontent.com/u/35960?v=3","gravatar_id":"","url":"https://api.github.com/users/bjchambers","html_url":"https://github.com/bjchambers","followers_url":"https://api.github.com/users/bjchambers/followers","following_url":"https://api.github.com/users/bjchambers/following{/other_user}","gists_url":"https://api.github.com/users/bjchambers/gists{/gist_id}","starred_url":"https://api.github.com/users/bjchambers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bjchambers/subscriptions","organizations_url":"https://api.github.com/users/bjchambers/orgs","repos_url":"https://api.github.com/users/bjchambers/repos","events_url":"https://api.github.com/users/bjchambers/events{/privacy}","received_events_url":"https://api.github.com/users/bjchambers/received_events","type":"User","site_admin":false},"body":"Be sure to do all of the following to help us incorporate your contribution\r\nquickly and easily:\r\n\r\n - [*] Make sure the PR title is formatted like:\r\n   `[BEAM-<Jira issue #>] Description of pull request`\r\n - [*] Make sure tests pass via `mvn clean verify`. (Even better, enable\r\n       Travis-CI on your fork and ensure the whole test matrix passes).\r\n - [*] Replace `<Jira issue #>` in the title with the actual Jira issue\r\n       number, if there is one.\r\n - [*] If this contribution is large, please file an Apache\r\n       [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.txt).\r\n\r\n---\r\n","created_at":"2016-09-29T00:03:33Z","updated_at":"2016-10-03T19:20:48Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8df793973b11c4b75c5cbebc897d684c6057841c","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/apache/incubator-beam/pulls/1024/commits","review_comments_url":"https://api.github.com/repos/apache/incubator-beam/pulls/1024/comments","review_comment_url":"https://api.github.com/repos/apache/incubator-beam/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/incubator-beam/issues/1024/comments","statuses_url":"https://api.github.com/repos/apache/incubator-beam/statuses/d7ca4a815bd98b4d23a9ee1d0a2cab5e01dff1f9","head":{"label":"bjchambers:metrics","ref":"metrics","sha":"d7ca4a815bd98b4d23a9ee1d0a2cab5e01dff1f9","user":{"login":"bjchambers","id":35960,"avatar_url":"https://avatars.githubusercontent.com/u/35960?v=3","gravatar_id":"","url":"https://api.github.com/users/bjchambers","html_url":"https://github.com/bjchambers","followers_url":"https://api.github.com/users/bjchambers/followers","following_url":"https://api.github.com/users/bjchambers/following{/other_user}","gists_url":"https://api.github.com/users/bjchambers/gists{/gist_id}","starred_url":"https://api.github.com/users/bjchambers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bjchambers/subscriptions","organizations_url":"https://api.github.com/users/bjchambers/orgs","repos_url":"https://api.github.com/users/bjchambers/repos","events_url":"https://api.github.com/users/bjchambers/events{/privacy}","received_events_url":"https://api.github.com/users/bjchambers/received_events","type":"User","site_admin":false},"repo":{"id":53010511,"name":"incubator-beam","full_name":"bjchambers/incubator-beam","owner":{"login":"bjchambers","id":35960,"avatar_url":"https://avatars.githubusercontent.com/u/35960?v=3","gravatar_id":"","url":"https://api.github.com/users/bjchambers","html_url":"https://github.com/bjchambers","followers_url":"https://api.github.com/users/bjchambers/followers","following_url":"https://api.github.com/users/bjchambers/following{/other_user}","gists_url":"https://api.github.com/users/bjchambers/gists{/gist_id}","starred_url":"https://api.github.com/users/bjchambers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bjchambers/subscriptions","organizations_url":"https://api.github.com/users/bjchambers/orgs","repos_url":"https://api.github.com/users/bjchambers/repos","events_url":"https://api.github.com/users/bjchambers/events{/privacy}","received_events_url":"https://api.github.com/users/bjchambers/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bjchambers/incubator-beam","description":"Mirror of Apache Beam (Incubating)","fork":true,"url":"https://api.github.com/repos/bjchambers/incubator-beam","forks_url":"https://api.github.com/repos/bjchambers/incubator-beam/forks","keys_url":"https://api.github.com/repos/bjchambers/incubator-beam/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bjchambers/incubator-beam/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bjchambers/incubator-beam/teams","hooks_url":"https://api.github.com/repos/bjchambers/incubator-beam/hooks","issue_events_url":"https://api.github.com/repos/bjchambers/incubator-beam/issues/events{/number}","events_url":"https://api.github.com/repos/bjchambers/incubator-beam/events","assignees_url":"https://api.github.com/repos/bjchambers/incubator-beam/assignees{/user}","branches_url":"https://api.github.com/repos/bjchambers/incubator-beam/branches{/branch}","tags_url":"https://api.github.com/repos/bjchambers/incubator-beam/tags","blobs_url":"https://api.github.com/repos/bjchambers/incubator-beam/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bjchambers/incubator-beam/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bjchambers/incubator-beam/git/refs{/sha}","trees_url":"https://api.github.com/repos/bjchambers/incubator-beam/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bjchambers/incubator-beam/statuses/{sha}","languages_url":"https://api.github.com/repos/bjchambers/incubator-beam/languages","stargazers_url":"https://api.github.com/repos/bjchambers/incubator-beam/stargazers","contributors_url":"https://api.github.com/repos/bjchambers/incubator-beam/contributors","subscribers_url":"https://api.github.com/repos/bjchambers/incubator-beam/subscribers","subscription_url":"https://api.github.com/repos/bjchambers/incubator-beam/subscription","commits_url":"https://api.github.com/repos/bjchambers/incubator-beam/commits{/sha}","git_commits_url":"https://api.github.com/repos/bjchambers/incubator-beam/git/commits{/sha}","comments_url":"https://api.github.com/repos/bjchambers/incubator-beam/comments{/number}","issue_comment_url":"https://api.github.com/repos/bjchambers/incubator-beam/issues/comments{/number}","contents_url":"https://api.github.com/repos/bjchambers/incubator-beam/contents/{+path}","compare_url":"https://api.github.com/repos/bjchambers/incubator-beam/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bjchambers/incubator-beam/merges","archive_url":"https://api.github.com/repos/bjchambers/incubator-beam/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bjchambers/incubator-beam/downloads","issues_url":"https://api.github.com/repos/bjchambers/incubator-beam/issues{/number}","pulls_url":"https://api.github.com/repos/bjchambers/incubator-beam/pulls{/number}","milestones_url":"https://api.github.com/repos/bjchambers/incubator-beam/milestones{/number}","notifications_url":"https://api.github.com/repos/bjchambers/incubator-beam/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bjchambers/incubator-beam/labels{/name}","releases_url":"https://api.github.com/repos/bjchambers/incubator-beam/releases{/id}","deployments_url":"https://api.github.com/repos/bjchambers/incubator-beam/deployments","created_at":"2016-03-03T01:35:27Z","updated_at":"2016-03-03T01:35:29Z","pushed_at":"2016-09-30T22:01:47Z","git_url":"git://github.com/bjchambers/incubator-beam.git","ssh_url":"git@github.com:bjchambers/incubator-beam.git","clone_url":"https://github.com/bjchambers/incubator-beam.git","svn_url":"https://github.com/bjchambers/incubator-beam","homepage":null,"size":18489,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"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":"apache:master","ref":"master","sha":"efb2b83ffd3f37e615c93515982827a4ba3eb4cc","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":50904245,"name":"incubator-beam","full_name":"apache/incubator-beam","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/incubator-beam","description":"Mirror of Apache Beam (Incubating)","fork":false,"url":"https://api.github.com/repos/apache/incubator-beam","forks_url":"https://api.github.com/repos/apache/incubator-beam/forks","keys_url":"https://api.github.com/repos/apache/incubator-beam/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/incubator-beam/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/incubator-beam/teams","hooks_url":"https://api.github.com/repos/apache/incubator-beam/hooks","issue_events_url":"https://api.github.com/repos/apache/incubator-beam/issues/events{/number}","events_url":"https://api.github.com/repos/apache/incubator-beam/events","assignees_url":"https://api.github.com/repos/apache/incubator-beam/assignees{/user}","branches_url":"https://api.github.com/repos/apache/incubator-beam/branches{/branch}","tags_url":"https://api.github.com/repos/apache/incubator-beam/tags","blobs_url":"https://api.github.com/repos/apache/incubator-beam/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/incubator-beam/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/incubator-beam/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/incubator-beam/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/incubator-beam/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/incubator-beam/languages","stargazers_url":"https://api.github.com/repos/apache/incubator-beam/stargazers","contributors_url":"https://api.github.com/repos/apache/incubator-beam/contributors","subscribers_url":"https://api.github.com/repos/apache/incubator-beam/subscribers","subscription_url":"https://api.github.com/repos/apache/incubator-beam/subscription","commits_url":"https://api.github.com/repos/apache/incubator-beam/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/incubator-beam/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/incubator-beam/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/incubator-beam/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/incubator-beam/contents/{+path}","compare_url":"https://api.github.com/repos/apache/incubator-beam/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/incubator-beam/merges","archive_url":"https://api.github.com/repos/apache/incubator-beam/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/incubator-beam/downloads","issues_url":"https://api.github.com/repos/apache/incubator-beam/issues{/number}","pulls_url":"https://api.github.com/repos/apache/incubator-beam/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/incubator-beam/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/incubator-beam/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/incubator-beam/labels{/name}","releases_url":"https://api.github.com/repos/apache/incubator-beam/releases{/id}","deployments_url":"https://api.github.com/repos/apache/incubator-beam/deployments","created_at":"2016-02-02T08:00:06Z","updated_at":"2016-10-02T20:26:55Z","pushed_at":"2016-10-03T18:44:18Z","git_url":"git://github.com/apache/incubator-beam.git","ssh_url":"git@github.com:apache/incubator-beam.git","clone_url":"https://github.com/apache/incubator-beam.git","svn_url":"https://github.com/apache/incubator-beam","homepage":null,"size":29050,"stargazers_count":375,"watchers_count":375,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":237,"mirror_url":"git://git.apache.org/incubator-beam.git","open_issues_count":49,"forks":237,"open_issues":49,"watchers":375,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/incubator-beam/pulls/1024"},"html":{"href":"https://github.com/apache/incubator-beam/pull/1024"},"issue":{"href":"https://api.github.com/repos/apache/incubator-beam/issues/1024"},"comments":{"href":"https://api.github.com/repos/apache/incubator-beam/issues/1024/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/incubator-beam/pulls/1024/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/incubator-beam/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/incubator-beam/pulls/1024/commits"},"statuses":{"href":"https://api.github.com/repos/apache/incubator-beam/statuses/d7ca4a815bd98b4d23a9ee1d0a2cab5e01dff1f9"}}}}
{"actor":{"display_login":"swegner"}}