-
Notifications
You must be signed in to change notification settings - Fork 10
feat(diagnostics): Add thread dumps endpoint and storage #894
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
Open
Josh-Matsuoka
wants to merge
7
commits into
cryostatio:main
Choose a base branch
from
Josh-Matsuoka:thread-dumps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
325df77
Initial implementation of thread dumps
Josh-Matsuoka e74a9d5
Support other thread dump format, sanity check format query parameter
Josh-Matsuoka caa7d2a
Merge remote-tracking branch 'upstream/main' into thread-dumps
Josh-Matsuoka 368fa46
refactor, long-running api handling
Josh-Matsuoka 4fe54df
Fix error notification class
Josh-Matsuoka fb193d5
Fix downloads, filter by target, temp debug logging
Josh-Matsuoka f4d9a40
Merge remote-tracking branch 'upstream/main' into thread-dumps
Josh-Matsuoka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,22 +15,185 @@ | |
*/ | ||
package io.cryostat.diagnostic; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import io.cryostat.ConfigProperties; | ||
import io.cryostat.Producers; | ||
import io.cryostat.recordings.LongRunningRequestGenerator; | ||
import io.cryostat.recordings.LongRunningRequestGenerator.ThreadDumpRequest; | ||
import io.cryostat.targets.Target; | ||
import io.cryostat.targets.TargetConnectionManager; | ||
import io.cryostat.util.HttpMimeType; | ||
|
||
import io.smallrye.common.annotation.Blocking; | ||
import io.vertx.core.http.HttpServerResponse; | ||
import io.vertx.mutiny.core.eventbus.EventBus; | ||
import jakarta.annotation.security.RolesAllowed; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Named; | ||
import jakarta.ws.rs.DELETE; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.NotFoundException; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.core.HttpHeaders; | ||
import org.apache.commons.codec.binary.Base64; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.jboss.logging.Logger; | ||
import org.jboss.resteasy.reactive.RestPath; | ||
import org.jboss.resteasy.reactive.RestQuery; | ||
import org.jboss.resteasy.reactive.RestResponse; | ||
import org.jboss.resteasy.reactive.RestResponse.ResponseBuilder; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.GetObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.HeadObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.NoSuchKeyException; | ||
import software.amazon.awssdk.services.s3.presigner.S3Presigner; | ||
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest; | ||
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest; | ||
|
||
@Path("/api/beta/diagnostics/targets/{targetId}") | ||
@Path("/api/beta/diagnostics/") | ||
public class Diagnostics { | ||
|
||
@Inject TargetConnectionManager targetConnectionManager; | ||
@Inject S3Client storage; | ||
@Inject S3Presigner presigner; | ||
@Inject Logger log; | ||
@Inject LongRunningRequestGenerator generator; | ||
|
||
@Inject | ||
@Named(Producers.BASE64_URL) | ||
Base64 base64Url; | ||
|
||
@ConfigProperty(name = ConfigProperties.AWS_BUCKET_NAME_THREAD_DUMPS) | ||
String bucket; | ||
|
||
@ConfigProperty(name = ConfigProperties.STORAGE_PRESIGNED_DOWNLOADS_ENABLED) | ||
boolean presignedDownloadsEnabled; | ||
|
||
@ConfigProperty(name = ConfigProperties.STORAGE_EXT_URL) | ||
Optional<String> externalStorageUrl; | ||
|
||
@Inject EventBus bus; | ||
@Inject DiagnosticsHelper helper; | ||
|
||
@Path("targets/{targetId}/threaddump") | ||
@RolesAllowed("write") | ||
@Blocking | ||
@POST | ||
public String threadDump( | ||
HttpServerResponse response, @RestPath long targetId, @RestQuery String format) { | ||
log.trace("Creating new thread dump request for target: " + targetId); | ||
ThreadDumpRequest request = | ||
new ThreadDumpRequest( | ||
UUID.randomUUID().toString(), Long.toString(targetId), format); | ||
response.endHandler( | ||
(e) -> bus.publish(LongRunningRequestGenerator.THREAD_DUMP_ADDRESS, request)); | ||
return request.id(); | ||
} | ||
|
||
@Path("targets/{targetId}/threaddump") | ||
@RolesAllowed("read") | ||
@Blocking | ||
@GET | ||
public List<ThreadDump> getThreadDumps(@RestPath long targetId) { | ||
log.warn("Fetching thread dumps for target: " + targetId); | ||
log.warn("Thread dumps: " + helper.getThreadDumps(targetId)); | ||
log.warn("Storage bucket: " + bucket); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar for |
||
return helper.getThreadDumps(targetId); | ||
} | ||
|
||
@Path("/gc") | ||
@DELETE | ||
@Blocking | ||
@Path("targets/{targetId}/threaddump/{threadDumpId}") | ||
@RolesAllowed("write") | ||
public void deleteThreadDump(@RestPath String threadDumpId) { | ||
try { | ||
log.warn("Deleting thread dump with ID: " + threadDumpId); | ||
storage.headObject( | ||
HeadObjectRequest.builder().bucket(bucket).key(threadDumpId).build()); | ||
} catch (NoSuchKeyException e) { | ||
throw new NotFoundException(e); | ||
} | ||
storage.deleteObject( | ||
DeleteObjectRequest.builder().bucket(bucket).key(threadDumpId).build()); | ||
} | ||
|
||
@Path("/threaddump/download/{encodedKey}") | ||
@RolesAllowed("read") | ||
@Blocking | ||
@GET | ||
public RestResponse<Object> handleStorageDownload( | ||
@RestPath String encodedKey, @RestQuery String query) throws URISyntaxException { | ||
Pair<String, String> decodedKey = helper.decodedKey(encodedKey); | ||
var threadDumpId = decodedKey.getValue().strip(); | ||
log.warn("Handling download Request for encodedKey: " + encodedKey); | ||
log.warn("Handling download Request for query: " + query); | ||
log.warn("Decoded key: " + decodedKey.toString()); | ||
log.warn("UUID: " + threadDumpId); | ||
log.warn("Bucket: " + bucket); | ||
storage.headObject(HeadObjectRequest.builder().bucket(bucket).key(threadDumpId).build()) | ||
.sdkHttpResponse(); | ||
|
||
if (!presignedDownloadsEnabled) { | ||
return ResponseBuilder.ok() | ||
.header( | ||
HttpHeaders.CONTENT_DISPOSITION, | ||
String.format("attachment; filename=\"%s\"", decodedKey.getValue())) | ||
.header(HttpHeaders.CONTENT_TYPE, HttpMimeType.OCTET_STREAM.mime()) | ||
.entity(helper.getThreadDumpStream(encodedKey)) | ||
.build(); | ||
} | ||
|
||
log.tracev("Handling presigned download request for {0}", decodedKey); | ||
GetObjectRequest getRequest = | ||
GetObjectRequest.builder().bucket(bucket).key(threadDumpId).build(); | ||
GetObjectPresignRequest presignRequest = | ||
GetObjectPresignRequest.builder() | ||
.signatureDuration(Duration.ofMinutes(1)) | ||
.getObjectRequest(getRequest) | ||
.build(); | ||
PresignedGetObjectRequest presignedRequest = presigner.presignGetObject(presignRequest); | ||
URI uri = presignedRequest.url().toURI(); | ||
if (externalStorageUrl.isPresent()) { | ||
String extUrl = externalStorageUrl.get(); | ||
if (StringUtils.isNotBlank(extUrl)) { | ||
URI extUri = new URI(extUrl); | ||
uri = | ||
new URI( | ||
extUri.getScheme(), | ||
extUri.getAuthority(), | ||
URI.create(String.format("%s/%s", extUri.getPath(), uri.getPath())) | ||
.normalize() | ||
.getPath(), | ||
uri.getQuery(), | ||
uri.getFragment()); | ||
} | ||
} | ||
ResponseBuilder<Object> response = | ||
ResponseBuilder.create(RestResponse.Status.PERMANENT_REDIRECT); | ||
if (StringUtils.isNotBlank(query)) { | ||
response = | ||
response.header( | ||
HttpHeaders.CONTENT_DISPOSITION, | ||
String.format( | ||
"attachment; filename=\"%s\"", | ||
new String(base64Url.decode(query), StandardCharsets.UTF_8))); | ||
} | ||
return response.location(uri).build(); | ||
} | ||
|
||
@Path("targets/{targetId}/gc") | ||
@RolesAllowed("write") | ||
@Blocking | ||
@POST | ||
|
@@ -41,4 +204,14 @@ public void gc(@RestPath long targetId) { | |
conn.invokeMBeanOperation( | ||
"java.lang:type=Memory", "gc", null, null, Void.class)); | ||
} | ||
|
||
public record ThreadDump(String content, String jvmId, String downloadUrl, String uuid) { | ||
|
||
public ThreadDump { | ||
Objects.requireNonNull(content); | ||
Objects.requireNonNull(jvmId); | ||
Objects.requireNonNull(downloadUrl); | ||
Objects.requireNonNull(uuid); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Best to use the
log.tracev("message {0} {1}", obj1, obj2)
format specifier style rather than direct string concatenation. Using concatenation will result in an eager.toString()
conversion being called on the operands, even if the message is logged at a lower level than currently configured.