Skip to content
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
32 changes: 32 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,44 @@
<jetty.version>11.0.26</jetty.version>
<modernizer.version>3.2.0</modernizer.version>
<njord.version>0.7.5</njord.version>
<opentelemetry.version>1.57.0</opentelemetry.version>
<opentelemetry-semconv.version>1.25.0-alpha</opentelemetry-semconv.version>
<slf4j.version>2.0.17</slf4j.version>
<shade.prefix>${project.groupId}.shaded</shade.prefix>
<surefire.version>3.5.2</surefire.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-bom</artifactId>
<version>${opentelemetry.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-prometheus</artifactId>
<version>1.45.0-alpha</version>
</dependency>
<dependency>
<groupId>io.opentelemetry.semconv</groupId>
<artifactId>opentelemetry-semconv</artifactId>
<version>${opentelemetry-semconv.version}</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/gaul/s3proxy/MetricsHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2014-2025 Andrew Gaul <andrew@gaul.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gaul.s3proxy;

import java.io.IOException;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

/** Jetty handler that serves Prometheus metrics at /metrics endpoint. */
public final class MetricsHandler extends AbstractHandler {
private final S3ProxyMetrics metrics;

public MetricsHandler(S3ProxyMetrics metrics) {
this.metrics = metrics;
}

@Override
public void handle(String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
if (!"/metrics".equals(target)) {
return;
}

response.setContentType("text/plain; version=0.0.4; charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().write(metrics.scrape());
baseRequest.setHandled(true);
}
}
57 changes: 57 additions & 0 deletions src/main/java/org/gaul/s3proxy/S3Operation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2014-2025 Andrew Gaul <andrew@gaul.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gaul.s3proxy;

/** Enumeration of S3 operations for metrics tracking. */
public enum S3Operation {
LIST_BUCKETS("ListBuckets"),
LIST_OBJECTS_V2("ListObjectsV2"),
GET_OBJECT("GetObject"),
PUT_OBJECT("PutObject"),
DELETE_OBJECT("DeleteObject"),
DELETE_OBJECTS("DeleteObjects"),
CREATE_BUCKET("CreateBucket"),
DELETE_BUCKET("DeleteBucket"),
HEAD_BUCKET("HeadBucket"),
HEAD_OBJECT("HeadObject"),
COPY_OBJECT("CopyObject"),
CREATE_MULTIPART_UPLOAD("CreateMultipartUpload"),
UPLOAD_PART("UploadPart"),
UPLOAD_PART_COPY("UploadPartCopy"),
COMPLETE_MULTIPART_UPLOAD("CompleteMultipartUpload"),
ABORT_MULTIPART_UPLOAD("AbortMultipartUpload"),
LIST_MULTIPART_UPLOADS("ListMultipartUploads"),
LIST_PARTS("ListParts"),
GET_OBJECT_ACL("GetObjectAcl"),
PUT_OBJECT_ACL("PutObjectAcl"),
GET_BUCKET_ACL("GetBucketAcl"),
PUT_BUCKET_ACL("PutBucketAcl"),
GET_BUCKET_LOCATION("GetBucketLocation"),
GET_BUCKET_POLICY("GetBucketPolicy"),
OPTIONS_OBJECT("OptionsObject"),
UNKNOWN("Unknown");

private final String value;

S3Operation(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
59 changes: 57 additions & 2 deletions src/main/java/org/gaul/s3proxy/S3Proxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.jclouds.blobstore.BlobStore;
Expand All @@ -53,6 +54,7 @@
public final class S3Proxy {
private final Server server;
private final S3ProxyHandlerJetty handler;
private final S3ProxyMetrics metrics;
private final boolean listenHTTP;
private final boolean listenHTTPS;

Expand Down Expand Up @@ -128,14 +130,28 @@ public final class S3Proxy {
} else {
listenHTTPS = false;
}
if (builder.metricsEnabled) {
this.metrics = new S3ProxyMetrics(
builder.metricsHost, builder.metricsPort);
} else {
this.metrics = null;
}

handler = new S3ProxyHandlerJetty(builder.blobStore,
builder.authenticationType, builder.identity,
builder.credential, builder.virtualHost,
builder.maxSinglePartObjectSize,
builder.v4MaxNonChunkedRequestSize,
builder.ignoreUnknownHeaders, builder.corsRules,
builder.servicePath, builder.maximumTimeSkew);
server.setHandler(handler);
builder.servicePath, builder.maximumTimeSkew, metrics);

if (metrics != null) {
var metricsHandler = new MetricsHandler(metrics);
var handlerList = new HandlerList(metricsHandler, handler);
server.setHandler(handlerList);
} else {
server.setHandler(handler);
}
}

public static final class Builder {
Expand All @@ -157,6 +173,9 @@ public static final class Builder {
private CrossOriginResourceSharing corsRules;
private int jettyMaxThreads = 200; // sourced from QueuedThreadPool()
private int maximumTimeSkew = 15 * 60;
private boolean metricsEnabled;
private int metricsPort = S3ProxyMetrics.DEFAULT_METRICS_PORT;
private String metricsHost = S3ProxyMetrics.DEFAULT_METRICS_HOST;

Builder() {
}
Expand Down Expand Up @@ -321,6 +340,24 @@ public static Builder fromProperties(Properties properties)
builder.maximumTimeSkew(Integer.parseInt(maximumTimeSkew));
}

String metricsEnabled = properties.getProperty(
S3ProxyConstants.PROPERTY_METRICS_ENABLED);
if (!Strings.isNullOrEmpty(metricsEnabled)) {
builder.metricsEnabled(Boolean.parseBoolean(metricsEnabled));
}

String metricsPort = properties.getProperty(
S3ProxyConstants.PROPERTY_METRICS_PORT);
if (!Strings.isNullOrEmpty(metricsPort)) {
builder.metricsPort(Integer.parseInt(metricsPort));
}

String metricsHost = properties.getProperty(
S3ProxyConstants.PROPERTY_METRICS_HOST);
if (!Strings.isNullOrEmpty(metricsHost)) {
builder.metricsHost(metricsHost);
}

return builder;
}

Expand Down Expand Up @@ -409,6 +446,21 @@ public Builder maximumTimeSkew(int maximumTimeSkew) {
return this;
}

public Builder metricsEnabled(boolean metricsEnabled) {
this.metricsEnabled = metricsEnabled;
return this;
}

public Builder metricsPort(int metricsPort) {
this.metricsPort = metricsPort;
return this;
}

public Builder metricsHost(String metricsHost) {
this.metricsHost = requireNonNull(metricsHost);
return this;
}

public Builder servicePath(String s3ProxyServicePath) {
String path = Strings.nullToEmpty(s3ProxyServicePath);

Expand Down Expand Up @@ -487,6 +539,9 @@ public void start() throws Exception {

public void stop() throws Exception {
server.stop();
if (metrics != null) {
metrics.close();
}
}

public int getPort() {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/gaul/s3proxy/S3ProxyConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ public final class S3ProxyConstants {
public static final String PROPERTY_NO_CACHE_BLOBSTORE =
"s3proxy.no-cache-blobstore";

/** Enable Prometheus metrics endpoint at /metrics. */
public static final String PROPERTY_METRICS_ENABLED =
"s3proxy.metrics.enabled";

public static final String PROPERTY_METRICS_PORT =
"s3proxy.metrics.port";
public static final String PROPERTY_METRICS_HOST =
"s3proxy.metrics.host";

static final String PROPERTY_ALT_JCLOUDS_PREFIX = "alt.";

private S3ProxyConstants() {
Expand Down
Loading
Loading