Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
evernat authored and elnggng committed Mar 17, 2022
1 parent ed825f9 commit 5185965
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 188 deletions.
6 changes: 3 additions & 3 deletions javamelody-collector-server/pom.xml
Expand Up @@ -80,9 +80,9 @@
<version>1.11.136</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.136</version>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.136</version>
</dependency>
</dependencies>
<build>
Expand Down
9 changes: 2 additions & 7 deletions javamelody-core/pom.xml
Expand Up @@ -236,14 +236,9 @@
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.136</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.136</version>
<optional>true</optional>
</dependency>
<!-- Dépendance JUnit -->
<dependency>
Expand Down
Expand Up @@ -372,6 +372,11 @@ public enum Parameter {
*/
CLOUDWATCH_NAMESPACE("cloudwatch-namespace"),

/**
* Bucket name to use <a href='https://aws.amazon.com/s3/'>AWS S3</a> to send heap dump files (null by default).
*/
HEAP_DUMP_S3_BUCKETNAME("heap-dump-s3-bucketname"),

/**
* URL of the <a href='https://www.influxdata.com/time-series-platform/'>InfluxDB</a> server to send metrics to,
* for example: http://11.22.33.44:8086/write?db=mydb (null by default).
Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -37,7 +37,6 @@

import net.bull.javamelody.Parameter;
import net.bull.javamelody.SessionListener;
import net.bull.javamelody.internal.aws.s3.util.S3Util;
import net.bull.javamelody.internal.common.I18N;
import net.bull.javamelody.internal.common.InputOutput;
import net.bull.javamelody.internal.common.LOG;
Expand Down Expand Up @@ -213,22 +212,22 @@ public String execute(Collector collector, CollectorServer collectorServer, // N
// heap dump à générer dans le répertoire temporaire sur le serveur
// avec un suffixe contenant le host, la date et l'heure et avec une extension hprof
// (utiliser jvisualvm du jdk ou MAT d'eclipse en standalone ou en plugin)
String message = "";
final File heapDump = heapDump();
final File zipFile = new File(heapDump.getParentFile(),
heapDump.getName() + ".zip");
InputOutput.zipFile(heapDump, zipFile);
InputOutput.deleteFile(heapDump);
final String path = zipFile.getPath();
if (Boolean.valueOf(System.getProperty("enable.s3.heapDump"))) {
String message = "";
if (Parameter.HEAP_DUMP_S3_BUCKETNAME.getValue() != null) {
try {
S3Util.upload(zipFile);
message = "heap dump " + zipFile.getName()
+ " uploaded successfully to s3.";
} catch (Exception e) {
message = "Failed to upload heap to s3 - " + e.getMessage() + "\n";
S3.upload(zipFile, Parameter.HEAP_DUMP_S3_BUCKETNAME.getValue());
message = "Heap dump " + zipFile.getName()
+ " uploaded successfully to S3.";
} catch (final IOException e) {
message = "Failed to upload heap to S3 - " + e.getMessage() + '\n';
}
}
final String path = zipFile.getPath();
messageForReport = message
+ I18N.getFormattedString("heap_dump_genere", path.replace('\\', '/'));
}
Expand Down
@@ -0,0 +1,64 @@
package net.bull.javamelody.internal.model;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.AbortMultipartUploadRequest;
import com.amazonaws.services.s3.model.CompleteMultipartUploadRequest;
import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest;
import com.amazonaws.services.s3.model.InitiateMultipartUploadResult;
import com.amazonaws.services.s3.model.PartETag;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.internal.UploadPartRequestFactory;

import net.bull.javamelody.internal.common.LOG;

/**
* Upload to AWS S3.
* @author Salah Qasem
*/
final class S3 {
private static final long MINIMUM_SIZE_FOR_MULTIPART = 300 * 1024 * 1024; // 300 mb
private static final long PART_SIZE = 200 * 1024 * 1024; // 200 mb

private S3() {
super();
}

static void upload(File file, String bucketName) throws IOException {
final AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
if (file.length() > MINIMUM_SIZE_FOR_MULTIPART) {
// multipart upload
final List<PartETag> partETags = new ArrayList<PartETag>();
final InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(
new InitiateMultipartUploadRequest(bucketName, file.getName()));
final String uploadId = initResponse.getUploadId();
final UploadPartRequestFactory requestFactory = new UploadPartRequestFactory(
new PutObjectRequest(bucketName, file.getName(), file), uploadId, PART_SIZE);
try {
while (requestFactory.hasMoreRequests()) {
partETags.add(s3Client.uploadPart(requestFactory.getNextUploadPartRequest())
.getPartETag());
}
final CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(
bucketName, file.getName(), uploadId, partETags);
s3Client.completeMultipartUpload(compRequest);
} catch (final Exception e) {
s3Client.abortMultipartUpload(
new AbortMultipartUploadRequest(bucketName, file.getName(), uploadId));
throw new IOException(e);
}
} else {
try {
s3Client.putObject(bucketName, file.getName(), file);
} catch (final Exception e) {
throw new IOException(e);
}
}
LOG.info("File " + file.getName() + " uploaded successfully to S3");
}
}

This file was deleted.

5 changes: 0 additions & 5 deletions javamelody-swing/pom.xml
Expand Up @@ -108,11 +108,6 @@
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.136</version>
</dependency>
</dependencies>

<distributionManagement>
Expand Down

0 comments on commit 5185965

Please sign in to comment.