{"action":"created","comment":{"url":"https://api.github.com/repos/elastic/logstash/pulls/comments/51577221","id":51577221,"diff_hunk":"@@ -28,49 +35,107 @@ def fetch_or_store(namespaces, key, default_value = nil)\n     end\n \n     # This method allow to retrieve values for a specific path,\n+    # This method support the following queries\n+    #\n+    # stats/pipelines/pipeline_X\n+    # stats/pipelines/pipeline_X,pipeline_2\n+    # stats/os,jvm\n     #\n+    # If you use the `,` on a key the metric store will return the both values at that level\n+    #\n+    # The returned hash will keep the same structure as it had in the `Concurrent::Map`\n+    # but will be a normal ruby hash. This will allow the api to easily seriliaze the content\n+    # of the map\n     #\n     # @param [Array] The path where values should be located\n     # @return nil if the values are not found\n+    def get_with_path(path)\n+      key_paths = path.gsub(/^#{KEY_PATH_SEPARATOR}+/, \"\").split(KEY_PATH_SEPARATOR)\n+      get(*key_paths)\n+    end\n+\n+    # Use an array of symbols instead of path\n     def get(*key_paths)\n-      get_recursively(key_paths, @store)\n+      # Normalize the symbols access\n+      key_paths.map(&:to_sym)\n+      new_hash = Hash.new({})\n+\n+      get_recursively(key_paths, @store, new_hash)\n+\n+      new_hash\n     end\n \n-    # Return all the individuals Metric\n-    #\n-    # @return [Array] An array of all metric transformed in `Logstash::Event`, or in case of passing a block it yields\n-    # the expected value as other Enumerable implementations.\n-    def each(&block)\n-      data = each_recursively(@store).flatten\n-      if block_given?\n-        data.each(&block)\n+    # Return all the individuals Metric,\n+    # This call mimic a Enum's each if a block is provided\n+    def each(path = nil, &block)\n+      metrics = if path.nil?\n+        get_all\n       else\n-        return data\n+        transform_to_array(get_with_path(path))\n       end\n+\n+      block_given? ? metrics.each(&block) : metrics\n     end\n+    alias_method :all, :each\n \n     private\n-    def get_recursively(key_paths, map)\n-      key_candidate = key_paths.shift\n+    def get_all\n+      each_recursively(@store).flatten\n+    end\n+\n+    def get_recursively(key_paths, map, new_hash)\n+      key_candidates = extract_filter_keys(key_paths.shift)\n \n-      if key_paths.empty?\n-        return map[key_candidate]\n-      else \n-        next_map = map[key_candidate]\n+      key_candidates.each do |key_candidate|\n+        raise MetricNotFound, \"For path: #{key_candidate}\" if map[key_candidate].nil?\n \n-        if next_map.is_a?(Concurrent::Map)\n-          return get_recursively(key_paths, next_map)\n+        if key_paths.empty? # End of the user requested path\n+          if map[key_candidate].is_a?(Concurrent::Map)\n+            new_hash[key_candidate] = transform_to_hash(map[key_candidate])\n+          else\n+            new_hash[key_candidate] = map[key_candidate]\n+          end\n         else\n-          return nil\n+          if map[key_candidate].is_a?(Concurrent::Map)\n+            new_hash[key_candidate] = get_recursively(key_paths, map[key_candidate], {})\n+          else\n+            new_hash[key_candidate] = map[key_candidate]\n+          end\n         end\n       end\n+      return new_hash\n     end\n \n+    def extract_filter_keys(key)\n+      key.to_s.strip.split(FILTER_KEYS_SEPARATOR).map(&:to_sym)\n+    end\n+\n+    def transform_to_array(map)\n+      map.values.collect do |value|\n+        value.is_a?(Hash) ? transform_to_array(value) : value\n+      end.flatten\n+    end\n+\n+    def transform_to_hash(map, new_hash = Hash.new({}))","path":"logstash-core/lib/logstash/instrument/metric_store.rb","position":120,"original_position":120,"commit_id":"b603a756e6049a5f5f546f8e51dce5a6c763b953","original_commit_id":"b603a756e6049a5f5f546f8e51dce5a6c763b953","user":{"login":"ph","id":640,"avatar_url":"https://avatars.githubusercontent.com/u/640?v=3","gravatar_id":"","url":"https://api.github.com/users/ph","html_url":"https://github.com/ph","followers_url":"https://api.github.com/users/ph/followers","following_url":"https://api.github.com/users/ph/following{/other_user}","gists_url":"https://api.github.com/users/ph/gists{/gist_id}","starred_url":"https://api.github.com/users/ph/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ph/subscriptions","organizations_url":"https://api.github.com/users/ph/orgs","repos_url":"https://api.github.com/users/ph/repos","events_url":"https://api.github.com/users/ph/events{/privacy}","received_events_url":"https://api.github.com/users/ph/received_events","type":"User","site_admin":false},"body":"There is a small difference between `Hash.new()` and `Hash.new({})`, the argument is actually the default values for the keys, this make the code simpler because I don't have to worry about a default values for a key.\r\n\r\n```ruby\r\njruby-1.7.23 :001 > m = Hash.new\r\n => {}\r\njruby-1.7.23 :002 > m[:j][:k]\r\nNoMethodError: undefined method `[]' for nil:NilClass\r\n\r\njruby-1.7.23 :003 > m = Hash.new({})\r\n => {}\r\njruby-1.7.23 :004 > m[:j][:k] = \"dd\"\r\n\r\n```","created_at":"2016-02-02T14:49:14Z","updated_at":"2016-02-02T14:49:14Z","html_url":"https://github.com/elastic/logstash/pull/4602#discussion_r51577221","pull_request_url":"https://api.github.com/repos/elastic/logstash/pulls/4602","_links":{"self":{"href":"https://api.github.com/repos/elastic/logstash/pulls/comments/51577221"},"html":{"href":"https://github.com/elastic/logstash/pull/4602#discussion_r51577221"},"pull_request":{"href":"https://api.github.com/repos/elastic/logstash/pulls/4602"}}},"pull_request":{"url":"https://api.github.com/repos/elastic/logstash/pulls/4602","id":57847795,"html_url":"https://github.com/elastic/logstash/pull/4602","diff_url":"https://github.com/elastic/logstash/pull/4602.diff","patch_url":"https://github.com/elastic/logstash/pull/4602.patch","issue_url":"https://api.github.com/repos/elastic/logstash/issues/4602","number":4602,"state":"open","locked":false,"title":"Metrics/flexibility","user":{"login":"ph","id":640,"avatar_url":"https://avatars.githubusercontent.com/u/640?v=3","gravatar_id":"","url":"https://api.github.com/users/ph","html_url":"https://github.com/ph","followers_url":"https://api.github.com/users/ph/followers","following_url":"https://api.github.com/users/ph/following{/other_user}","gists_url":"https://api.github.com/users/ph/gists{/gist_id}","starred_url":"https://api.github.com/users/ph/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ph/subscriptions","organizations_url":"https://api.github.com/users/ph/orgs","repos_url":"https://api.github.com/users/ph/repos","events_url":"https://api.github.com/users/ph/events{/privacy}","received_events_url":"https://api.github.com/users/ph/received_events","type":"User","site_admin":false},"body":"Refactoring of the metric:\r\n\r\n- Conform to the metric defined in Follow #4492\r\n- Better filtering of the metrics from the metric store Fixes #4461\r\n- Hide the implementation details of the metric store It return an hash, Fixes #4529\r\n- MetricStore#each now support a path to only fetch a subset on the metrics.","created_at":"2016-02-01T14:42:00Z","updated_at":"2016-02-02T14:49:14Z","closed_at":null,"merged_at":null,"merge_commit_sha":"924fd4831b96e0dd65a92b1bad13cb9288b04bb9","assignee":{"login":"purbon","id":68540,"avatar_url":"https://avatars.githubusercontent.com/u/68540?v=3","gravatar_id":"","url":"https://api.github.com/users/purbon","html_url":"https://github.com/purbon","followers_url":"https://api.github.com/users/purbon/followers","following_url":"https://api.github.com/users/purbon/following{/other_user}","gists_url":"https://api.github.com/users/purbon/gists{/gist_id}","starred_url":"https://api.github.com/users/purbon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/purbon/subscriptions","organizations_url":"https://api.github.com/users/purbon/orgs","repos_url":"https://api.github.com/users/purbon/repos","events_url":"https://api.github.com/users/purbon/events{/privacy}","received_events_url":"https://api.github.com/users/purbon/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/elastic/logstash/pulls/4602/commits","review_comments_url":"https://api.github.com/repos/elastic/logstash/pulls/4602/comments","review_comment_url":"https://api.github.com/repos/elastic/logstash/pulls/comments{/number}","comments_url":"https://api.github.com/repos/elastic/logstash/issues/4602/comments","statuses_url":"https://api.github.com/repos/elastic/logstash/statuses/b603a756e6049a5f5f546f8e51dce5a6c763b953","head":{"label":"ph:metrics/flexibility","ref":"metrics/flexibility","sha":"b603a756e6049a5f5f546f8e51dce5a6c763b953","user":{"login":"ph","id":640,"avatar_url":"https://avatars.githubusercontent.com/u/640?v=3","gravatar_id":"","url":"https://api.github.com/users/ph","html_url":"https://github.com/ph","followers_url":"https://api.github.com/users/ph/followers","following_url":"https://api.github.com/users/ph/following{/other_user}","gists_url":"https://api.github.com/users/ph/gists{/gist_id}","starred_url":"https://api.github.com/users/ph/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ph/subscriptions","organizations_url":"https://api.github.com/users/ph/orgs","repos_url":"https://api.github.com/users/ph/repos","events_url":"https://api.github.com/users/ph/events{/privacy}","received_events_url":"https://api.github.com/users/ph/received_events","type":"User","site_admin":false},"repo":{"id":24861697,"name":"logstash","full_name":"ph/logstash","owner":{"login":"ph","id":640,"avatar_url":"https://avatars.githubusercontent.com/u/640?v=3","gravatar_id":"","url":"https://api.github.com/users/ph","html_url":"https://github.com/ph","followers_url":"https://api.github.com/users/ph/followers","following_url":"https://api.github.com/users/ph/following{/other_user}","gists_url":"https://api.github.com/users/ph/gists{/gist_id}","starred_url":"https://api.github.com/users/ph/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ph/subscriptions","organizations_url":"https://api.github.com/users/ph/orgs","repos_url":"https://api.github.com/users/ph/repos","events_url":"https://api.github.com/users/ph/events{/privacy}","received_events_url":"https://api.github.com/users/ph/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ph/logstash","description":"logstash - logs/event transport, processing, management, search.","fork":true,"url":"https://api.github.com/repos/ph/logstash","forks_url":"https://api.github.com/repos/ph/logstash/forks","keys_url":"https://api.github.com/repos/ph/logstash/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ph/logstash/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ph/logstash/teams","hooks_url":"https://api.github.com/repos/ph/logstash/hooks","issue_events_url":"https://api.github.com/repos/ph/logstash/issues/events{/number}","events_url":"https://api.github.com/repos/ph/logstash/events","assignees_url":"https://api.github.com/repos/ph/logstash/assignees{/user}","branches_url":"https://api.github.com/repos/ph/logstash/branches{/branch}","tags_url":"https://api.github.com/repos/ph/logstash/tags","blobs_url":"https://api.github.com/repos/ph/logstash/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ph/logstash/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ph/logstash/git/refs{/sha}","trees_url":"https://api.github.com/repos/ph/logstash/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ph/logstash/statuses/{sha}","languages_url":"https://api.github.com/repos/ph/logstash/languages","stargazers_url":"https://api.github.com/repos/ph/logstash/stargazers","contributors_url":"https://api.github.com/repos/ph/logstash/contributors","subscribers_url":"https://api.github.com/repos/ph/logstash/subscribers","subscription_url":"https://api.github.com/repos/ph/logstash/subscription","commits_url":"https://api.github.com/repos/ph/logstash/commits{/sha}","git_commits_url":"https://api.github.com/repos/ph/logstash/git/commits{/sha}","comments_url":"https://api.github.com/repos/ph/logstash/comments{/number}","issue_comment_url":"https://api.github.com/repos/ph/logstash/issues/comments{/number}","contents_url":"https://api.github.com/repos/ph/logstash/contents/{+path}","compare_url":"https://api.github.com/repos/ph/logstash/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ph/logstash/merges","archive_url":"https://api.github.com/repos/ph/logstash/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ph/logstash/downloads","issues_url":"https://api.github.com/repos/ph/logstash/issues{/number}","pulls_url":"https://api.github.com/repos/ph/logstash/pulls{/number}","milestones_url":"https://api.github.com/repos/ph/logstash/milestones{/number}","notifications_url":"https://api.github.com/repos/ph/logstash/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ph/logstash/labels{/name}","releases_url":"https://api.github.com/repos/ph/logstash/releases{/id}","deployments_url":"https://api.github.com/repos/ph/logstash/deployments","created_at":"2014-10-06T19:37:59Z","updated_at":"2015-06-05T14:18:00Z","pushed_at":"2016-02-01T21:04:57Z","git_url":"git://github.com/ph/logstash.git","ssh_url":"git@github.com:ph/logstash.git","clone_url":"https://github.com/ph/logstash.git","svn_url":"https://github.com/ph/logstash","homepage":"http://logstash.net/","size":74184,"stargazers_count":0,"watchers_count":0,"language":"Ruby","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":"elastic:feature/metrics","ref":"feature/metrics","sha":"ac2e030eca1fd288486088f9e23fec74ddad8cc9","user":{"login":"elastic","id":6764390,"avatar_url":"https://avatars.githubusercontent.com/u/6764390?v=3","gravatar_id":"","url":"https://api.github.com/users/elastic","html_url":"https://github.com/elastic","followers_url":"https://api.github.com/users/elastic/followers","following_url":"https://api.github.com/users/elastic/following{/other_user}","gists_url":"https://api.github.com/users/elastic/gists{/gist_id}","starred_url":"https://api.github.com/users/elastic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elastic/subscriptions","organizations_url":"https://api.github.com/users/elastic/orgs","repos_url":"https://api.github.com/users/elastic/repos","events_url":"https://api.github.com/users/elastic/events{/privacy}","received_events_url":"https://api.github.com/users/elastic/received_events","type":"Organization","site_admin":false},"repo":{"id":1090311,"name":"logstash","full_name":"elastic/logstash","owner":{"login":"elastic","id":6764390,"avatar_url":"https://avatars.githubusercontent.com/u/6764390?v=3","gravatar_id":"","url":"https://api.github.com/users/elastic","html_url":"https://github.com/elastic","followers_url":"https://api.github.com/users/elastic/followers","following_url":"https://api.github.com/users/elastic/following{/other_user}","gists_url":"https://api.github.com/users/elastic/gists{/gist_id}","starred_url":"https://api.github.com/users/elastic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elastic/subscriptions","organizations_url":"https://api.github.com/users/elastic/orgs","repos_url":"https://api.github.com/users/elastic/repos","events_url":"https://api.github.com/users/elastic/events{/privacy}","received_events_url":"https://api.github.com/users/elastic/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/elastic/logstash","description":"logstash - transport and process your logs, events, or other data","fork":false,"url":"https://api.github.com/repos/elastic/logstash","forks_url":"https://api.github.com/repos/elastic/logstash/forks","keys_url":"https://api.github.com/repos/elastic/logstash/keys{/key_id}","collaborators_url":"https://api.github.com/repos/elastic/logstash/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/elastic/logstash/teams","hooks_url":"https://api.github.com/repos/elastic/logstash/hooks","issue_events_url":"https://api.github.com/repos/elastic/logstash/issues/events{/number}","events_url":"https://api.github.com/repos/elastic/logstash/events","assignees_url":"https://api.github.com/repos/elastic/logstash/assignees{/user}","branches_url":"https://api.github.com/repos/elastic/logstash/branches{/branch}","tags_url":"https://api.github.com/repos/elastic/logstash/tags","blobs_url":"https://api.github.com/repos/elastic/logstash/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/elastic/logstash/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/elastic/logstash/git/refs{/sha}","trees_url":"https://api.github.com/repos/elastic/logstash/git/trees{/sha}","statuses_url":"https://api.github.com/repos/elastic/logstash/statuses/{sha}","languages_url":"https://api.github.com/repos/elastic/logstash/languages","stargazers_url":"https://api.github.com/repos/elastic/logstash/stargazers","contributors_url":"https://api.github.com/repos/elastic/logstash/contributors","subscribers_url":"https://api.github.com/repos/elastic/logstash/subscribers","subscription_url":"https://api.github.com/repos/elastic/logstash/subscription","commits_url":"https://api.github.com/repos/elastic/logstash/commits{/sha}","git_commits_url":"https://api.github.com/repos/elastic/logstash/git/commits{/sha}","comments_url":"https://api.github.com/repos/elastic/logstash/comments{/number}","issue_comment_url":"https://api.github.com/repos/elastic/logstash/issues/comments{/number}","contents_url":"https://api.github.com/repos/elastic/logstash/contents/{+path}","compare_url":"https://api.github.com/repos/elastic/logstash/compare/{base}...{head}","merges_url":"https://api.github.com/repos/elastic/logstash/merges","archive_url":"https://api.github.com/repos/elastic/logstash/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/elastic/logstash/downloads","issues_url":"https://api.github.com/repos/elastic/logstash/issues{/number}","pulls_url":"https://api.github.com/repos/elastic/logstash/pulls{/number}","milestones_url":"https://api.github.com/repos/elastic/logstash/milestones{/number}","notifications_url":"https://api.github.com/repos/elastic/logstash/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/elastic/logstash/labels{/name}","releases_url":"https://api.github.com/repos/elastic/logstash/releases{/id}","deployments_url":"https://api.github.com/repos/elastic/logstash/deployments","created_at":"2010-11-18T01:31:50Z","updated_at":"2016-02-02T09:47:32Z","pushed_at":"2016-02-02T14:26:23Z","git_url":"git://github.com/elastic/logstash.git","ssh_url":"git@github.com:elastic/logstash.git","clone_url":"https://github.com/elastic/logstash.git","svn_url":"https://github.com/elastic/logstash","homepage":"https://www.elastic.co/products/logstash","size":79058,"stargazers_count":5377,"watchers_count":5377,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1677,"mirror_url":null,"open_issues_count":755,"forks":1677,"open_issues":755,"watchers":5377,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/elastic/logstash/pulls/4602"},"html":{"href":"https://github.com/elastic/logstash/pull/4602"},"issue":{"href":"https://api.github.com/repos/elastic/logstash/issues/4602"},"comments":{"href":"https://api.github.com/repos/elastic/logstash/issues/4602/comments"},"review_comments":{"href":"https://api.github.com/repos/elastic/logstash/pulls/4602/comments"},"review_comment":{"href":"https://api.github.com/repos/elastic/logstash/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/elastic/logstash/pulls/4602/commits"},"statuses":{"href":"https://api.github.com/repos/elastic/logstash/statuses/b603a756e6049a5f5f546f8e51dce5a6c763b953"}}}}