Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query): update query processing to support new grafana dashboard #135

Merged
merged 7 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,24 +187,30 @@ $ curl -X DELETE "localhost:8080/delete_all"

### Query Endpoints

These endpoints match those used by the Grafana Simple JSON datasource.
These endpoints match those used by the [Grafana Simple JSON datasource](https://grafana.com/grafana/plugins/grafana-simple-json-datasource/).

#### GET /search

Responds with a JSON array containing the selectable query elements.
Responds with a JSON array containing selectable values of an event field (e.g. `jdk.ObjectAllocationSample.objectClass`) specified in the JSON body's `target` field. Used to define available selections for [dashboard variables](https://grafana.com/docs/grafana/v7.5/variables/).
andrewazores marked this conversation as resolved.
Show resolved Hide resolved

If `target` is set to `*`, responds with all selectable event fields.


CURL Example
```bash
$ curl "localhost:8080/search"
$ curl -X POST --data '{ "target": "jdk.ObjectAllocationSample.objectClass" }' "localhost:8080/search"
```


#### POST /query

Responds with a JSON array containing elements for a query. The query body format matches that of the Grafana Simple JSON datasource.
Responds with a JSON array containing data points for a query. The query body format matches that of the Grafana Simple JSON datasource.

The `target` field can have parameters to filter matching data points If there is no paramater, no matching is performed. If a parameter is specified with "*", matching is done for all possible value of that paramter.
andrewazores marked this conversation as resolved.
Show resolved Hide resolved

CURL Example
```bash
$ curl -X POST --data "Query Body" "localhost:8080/query"
$ curl -X POST --data '{ "target": "jdk.ObjectAllocationSample.weight?objectClass=java.util.HashSet", ...}' "localhost:8080/query"
```

## Supported JFR Events
Expand Down Expand Up @@ -296,6 +302,11 @@ jdk.JVMInformation
jdk.PhysicalMemory

jdk.ThreadContextSwitchRate

jdk.ObjectAllocationSample.eventThread
jdk.ObjectAllocationSample.stackTrace
jdk.ObjectAllocationSample.objectClass
jdk.ObjectAllocationSample.weight
```

### Unsupported JFR Events
Expand Down
479 changes: 396 additions & 83 deletions src/main/java/io/cryostat/jfr/datasource/events/RecordingService.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JfrResource {
private static final Logger LOGGER = LoggerFactory.getLogger(JfrResource.class);
public class Datasource {
private static final Logger LOGGER = LoggerFactory.getLogger(Datasource.class);
private static final String UNSET_FILE = "";
private volatile String loadedFile = UNSET_FILE;

Expand All @@ -80,11 +80,22 @@ void root(RoutingContext context) {
response.end();
}

@Route(path = "/search", methods = HttpMethod.GET)
@Route(path = "/search", methods = HttpMethod.POST)
void search(RoutingContext context) {
HttpServerResponse response = context.response();
setHeaders(response, "application/json", "GET");
response.end(recordingService.search());
setHeaders(response, "application/json", "POST");
tthvo marked this conversation as resolved.
Show resolved Hide resolved

JsonObject body = context.getBodyAsJson();
try {
if (body != null && !body.isEmpty()) {
LOGGER.info(body.toString());
response.end(recordingService.search(new Search(body)));
return;
}
} catch (Exception e) {
e.printStackTrace();
}
response.setStatusCode(400).end("Error: invalid search body");
}

@Route(path = "/query", methods = HttpMethod.POST)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/cryostat/jfr/datasource/server/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.time.temporal.TemporalAccessor;

import io.cryostat.jfr.datasource.utils.ArgRunnable;
import io.cryostat.jfr.datasource.utils.InvalidQueryException;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
Expand All @@ -60,7 +61,7 @@ public JsonArray getTargets() {
return this.query.getJsonArray("targets");
}

public void applyTargets(ArgRunnable<Target> runnable) {
public void applyTargets(ArgRunnable<Target> runnable) throws InvalidQueryException {
JsonArray targets = this.query.getJsonArray("targets");
for (int i = 0; i < targets.size(); i++) {
JsonObject target = targets.getJsonObject(i);
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/io/cryostat/jfr/datasource/server/Search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright The Cryostat Authors
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or data
* (collectively the "Software"), free of charge and under any and all copyright
* rights in the Software, and any and all patent rights owned or freely
* licensable by each licensor hereunder covering either (i) the unmodified
* Software as contributed to or provided by such licensor, or (ii) the Larger
* Works (as defined below), to deal in both
*
* (a) the Software, and
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software (each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
* The above copyright notice and either this complete permission notice or at
* a minimum a reference to the UPL must be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.cryostat.jfr.datasource.server;

import java.util.Optional;

import io.vertx.core.json.JsonObject;

public class Search {
private Optional<String> target;

public Search(JsonObject body) {
this.target = Optional.ofNullable(body.getString("target"));
}

public Optional<String> getTarget() {
return this.target;
}
}
77 changes: 72 additions & 5 deletions src/main/java/io/cryostat/jfr/datasource/server/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,87 @@
*/
package io.cryostat.jfr.datasource.server;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import io.cryostat.jfr.datasource.utils.InvalidQueryException;

public class Target {
private final String target;
private final String targetIdentifier;
private final String type;
private final Map<String, Set<String>> targetOptions;

public static final String durationTargetIdentifier = "events.custom.recordingDuration";
public static final String startTimeTargetIdentifier = "events.custom.startTime";

public enum ParamOperator { // TODO: Support inequality for numeric fields.
EQUAL("=");

private final String identifier;

ParamOperator(String identifier) {
this.identifier = identifier;
}

String getIdentifier() {
return this.identifier;
}
}

private static final String PARAM_SEPARATOR = "&";

public Target(String target, String type) {
this.target = target;
public Target(String target, String type) throws InvalidQueryException {
this.targetIdentifier = parseTargetIdentifier(target);
this.targetOptions = parseTargetOptions(target);
this.type = type;
}

public String getTarget() {
return this.target;
public String getTargetIdentifier() {
return this.targetIdentifier;
}

public String getType() {
return this.type;
}

public Map<String, Set<String>> getTargetOptions() {
return this.targetOptions;
}

private String parseTargetIdentifier(String target) {
final int idx = target.indexOf("?");
return idx >= 0 ? target.substring(0, target.indexOf("?")) : target;
}

private Map<String, Set<String>> parseTargetOptions(String target)
throws InvalidQueryException {
target = target.replaceAll("\\\\", ""); // Remove escapes
final Map<String, Set<String>> options = new HashMap<>();
final int idx = target.indexOf("?");
if (idx >= 0) {
for (String option : target.substring(idx + 1).split(PARAM_SEPARATOR)) {
int equalSignIndex = option.indexOf(ParamOperator.EQUAL.getIdentifier());

if (equalSignIndex < 0) {
throw new InvalidQueryException(option);
}

String fieldName = option.substring(0, equalSignIndex);
String[] fieldValues =
(equalSignIndex + 1 >= option.length())
? new String[] {""}
: option.substring(equalSignIndex + 1).split(",");

if (options.containsKey(fieldName)) {
options.get(fieldName).addAll(Arrays.asList(fieldValues));
} else {
options.put(fieldName, new HashSet<String>(Arrays.asList(fieldValues)));
}
}
}
return options;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright The Cryostat Authors
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or data
* (collectively the "Software"), free of charge and under any and all copyright
* rights in the Software, and any and all patent rights owned or freely
* licensable by each licensor hereunder covering either (i) the unmodified
* Software as contributed to or provided by such licensor, or (ii) the Larger
* Works (as defined below), to deal in both
*
* (a) the Software, and
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software (each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
* The above copyright notice and either this complete permission notice or at
* a minimum a reference to the UPL must be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.cryostat.jfr.datasource.utils;

public class InvalidQueryException extends Exception {
public InvalidQueryException(String param) {
super(String.format("Parameter \"%s\" is invalid", param));
}
}
Loading