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 all 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
23 changes: 17 additions & 6 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
#### POST /search

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.

Responds with a JSON array containing the selectable query elements.

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 parameter, no matching is performed. If a parameter is specified with "*", matching is done for all possible value of that parameter.

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
480 changes: 397 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 @@ -51,6 +51,7 @@
import io.cryostat.jfr.datasource.events.RecordingService;
import io.cryostat.jfr.datasource.sys.FileSystemService;

import io.quarkus.vertx.web.ReactiveRoutes;
import io.quarkus.vertx.web.Route;
import io.quarkus.vertx.web.Route.HttpMethod;
import io.smallrye.common.annotation.Blocking;
Expand All @@ -62,8 +63,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,17 +81,31 @@ void root(RoutingContext context) {
response.end();
}

@Route(path = "/search", methods = HttpMethod.GET)
@Route(
path = "/search",
methods = HttpMethod.POST,
produces = {ReactiveRoutes.APPLICATION_JSON})
void search(RoutingContext context) {
HttpServerResponse response = context.response();
setHeaders(response, "application/json", "GET");
response.end(recordingService.search());
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)
@Route(
path = "/query",
methods = HttpMethod.POST,
produces = {ReactiveRoutes.APPLICATION_JSON})
void query(RoutingContext context) {
HttpServerResponse response = context.response();
setHeaders(response, "application/json", "POST");
try {
JsonObject body = context.getBodyAsJson();
if (body != null && !body.isEmpty()) {
Expand All @@ -106,42 +121,50 @@ void query(RoutingContext context) {
response.end("Error: invalid query body");
}

@Route(path = "/annotations", methods = HttpMethod.GET)
@Route(
path = "/annotations",
methods = HttpMethod.POST,
produces = {"text/plain"})
void annotations(RoutingContext context) {
HttpServerResponse response = context.response();
setHeaders(response, "text/plain", "GET");
response.end(recordingService.annotations());
}

@Route(path = "/set", methods = HttpMethod.POST)
@Route(
path = "/set",
methods = HttpMethod.POST,
produces = {"text/plain"})
@Blocking
void set(RoutingContext context) {
HttpServerResponse response = context.response();
setHeaders(response, "text/plain", "POST");

String file = context.getBodyAsString();
String filePath = jfrDir + File.separator + file;

setFile(filePath, file, response, new StringBuilder());
}

@Route(path = "/upload", methods = HttpMethod.POST)
@Route(
path = "/upload",
methods = HttpMethod.POST,
produces = {"text/plain"})
@Blocking
void upload(RoutingContext context) {
HttpServerResponse response = context.response();
setHeaders(response, "text/plain", "POST");

final StringBuilder responseBuilder = new StringBuilder();

uploadFiles(context.fileUploads(), responseBuilder);
response.end(responseBuilder.toString());
}

@Route(path = "/load", methods = HttpMethod.POST)
@Route(
path = "/load",
methods = HttpMethod.POST,
produces = {"text/plain"})
@Blocking
void load(RoutingContext context) {
HttpServerResponse response = context.response();
setHeaders(response, "text/plain", "POST");

final StringBuilder responseBuilder = new StringBuilder();

Expand All @@ -151,10 +174,12 @@ void load(RoutingContext context) {
setFile(filePath, lastFile, response, responseBuilder);
}

@Route(path = "/list", methods = HttpMethod.GET)
@Route(
path = "/list",
methods = HttpMethod.GET,
produces = {"text/plain"})
void list(RoutingContext context) {
HttpServerResponse response = context.response();
setHeaders(response, "text/plain", "GET");

try {
StringBuilder responseBuilder = new StringBuilder();
Expand All @@ -172,20 +197,24 @@ void list(RoutingContext context) {
}
}

@Route(path = "/current", methods = HttpMethod.GET)
@Route(
path = "/current",
methods = HttpMethod.GET,
produces = {"text/plain"})
void current(RoutingContext context) {
HttpServerResponse response = context.response();
setHeaders(response, "text/plain", "GET");

LOGGER.info("Current: " + loadedFile);
response.end(loadedFile + System.lineSeparator());
}

@Route(path = "/delete_all", methods = HttpMethod.DELETE)
@Route(
path = "/delete_all",
methods = HttpMethod.DELETE,
produces = {"text/plain"})
@Blocking
void deleteAll(RoutingContext context) {
HttpServerResponse response = context.response();
setHeaders(response, "text/plain", "DELETE");

final StringBuilder stringBuilder = new StringBuilder();
try {
Expand All @@ -201,11 +230,13 @@ void deleteAll(RoutingContext context) {
}
}

@Route(path = "/delete", methods = HttpMethod.DELETE)
@Route(
path = "/delete",
methods = HttpMethod.DELETE,
produces = {"text/plain"})
@Blocking
void delete(RoutingContext context) {
HttpServerResponse response = context.response();
setHeaders(response, "text/plain", "DELETE");

String fileName = context.getBodyAsString();
if (fileName == null || fileName.isEmpty()) {
Expand Down Expand Up @@ -328,11 +359,4 @@ private void deleteFile(String filename) throws IOException {
throw new FileNotFoundException(filename + " does not exist");
}
}

private void setHeaders(HttpServerResponse response, String contentType, String allowedMethod) {
response.putHeader("content-type", contentType);
response.putHeader("Access-Control-Allow-Methods", allowedMethod);
response.putHeader("Access-Control-Allow-Origin", "*");
response.putHeader("Access-Control-Allow-Headers", "accept, content-type");
}
}
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;
}
}
Loading