protected String regexToGrabCurrentAndNextLevel ( final String query ) { if ( StringUtils . isEmpty ( query ) ) { throw new IllegalArgumentException ( "Query(glob) string cannot be null/empty" ) ; } String queryRegex = getRegex ( query ) ; int totalQueryTokens = getTotalTokens ( query ) ; if ( totalQueryTokens == 1 ) { // get metric names which matches the given query and have a next level, // Ex: For metric foo.bar.baz.qux, if query=*, we should get foo.bar. We are not // grabbing 0 level as it will give back bar, baz, qux because of the way data is structured. String baseRegex = convertRegexToCaptureUptoNextToken ( queryRegex ) ; return baseRegex + METRIC_TOKEN_SEPARATOR_REGEX + REGEX_TO_GRAB_SINGLE_TOKEN ; } else { String [ ] queryRegexParts = queryRegex . split ( "\\\\." ) ; String queryRegexUptoPrevLevel = StringUtils . join ( queryRegexParts , METRIC_TOKEN_SEPARATOR_REGEX , 0 , totalQueryTokens - 1 ) ; String baseRegex = convertRegexToCaptureUptoNextToken ( queryRegexUptoPrevLevel ) ; String queryRegexLastLevel = queryRegexParts [ totalQueryTokens - 1 ] ; String lastTokenRegex = convertRegexToCaptureUptoNextToken ( queryRegexLastLevel ) ; // Ex: For metric foo.bar.baz.qux.xxx, if query=foo.bar.b*, get foo.bar.baz, foo.bar.baz.qux // In this case baseRegex = "foo.bar", lastTokenRegex = "b[^.]*"' and the final // regex is foo\.bar\.b[^.]*(\.[^.]*){0,1} return baseRegex + METRIC_TOKEN_SEPARATOR_REGEX + lastTokenRegex + "(" + METRIC_TOKEN_SEPARATOR_REGEX + REGEX_TO_GRAB_SINGLE_TOKEN + ")" + "{0,1}" ; } }
Returns regex which could grab metric names from current level to the next level for a given query .