Skip to content

Commit

Permalink
GH-2046: Add runtime new datasets to metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Jan 1, 2025
1 parent 4f3ca09 commit 2a52238
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

import io.micrometer.core.instrument.MeterRegistry;
import jakarta.servlet.ServletContext;
import org.apache.jena.atlas.logging.Log;
import org.apache.jena.fuseki.Fuseki;
import org.apache.jena.fuseki.server.DataAccessPoint;
import org.apache.jena.fuseki.server.DataAccessPointRegistry;
import org.apache.jena.fuseki.servlets.HttpAction;

Expand All @@ -33,17 +35,24 @@ public interface MetricsProvider {
/** Bind each data access point in a DataAccessPointRegistry to the system Micrometer {@link MeterRegistry}. */
public default void dataAccessPointMetrics(MetricsProvider metricsProvider, DataAccessPointRegistry dapRegistry) {
try {
MeterRegistry meterRegistry = metricsProvider.getMeterRegistry();
if (meterRegistry != null) {
dapRegistry.accessPoints().forEach(dap->{
new FusekiRequestsMetrics( dap ).bindTo( meterRegistry );
});
}
dapRegistry.accessPoints().forEach(dap->addDataAccessPointMetrics(dap));
} catch (Throwable th) {
Fuseki.configLog.error("Failed to bind all data access points to netrics provider", th);
}
}

public default void addDataAccessPointMetrics(DataAccessPoint dataAccessPoint) {
MeterRegistry meterRegistry = this.getMeterRegistry();
if (meterRegistry != null )
addDataAccessPointMetrics(meterRegistry, dataAccessPoint);
}

private static void addDataAccessPointMetrics(MeterRegistry meterRegistry, DataAccessPoint dataAccessPoint) {
if ( dataAccessPoint == null )
Log.warn(MetricsProvider.class, "addDataAccessPointMetrics: Null DataAccessPoint");
new FusekiRequestsMetrics(dataAccessPoint).bindTo(meterRegistry);
}

public static void setMetricsProvider(ServletContext servletContext, MetricsProvider provider) {
Objects.requireNonNull(servletContext);
if ( provider == null )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.zip.DeflaterInputStream;
import java.util.zip.GZIPInputStream;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -41,6 +42,7 @@
import org.apache.jena.atlas.logging.Log;
import org.apache.jena.fuseki.Fuseki;
import org.apache.jena.fuseki.FusekiException;
import org.apache.jena.fuseki.metrics.MetricsProvider;
import org.apache.jena.fuseki.server.*;
import org.apache.jena.fuseki.system.ActionCategory;
import org.apache.jena.query.ReadWrite;
Expand Down Expand Up @@ -251,6 +253,23 @@ public DataAccessPointRegistry getDataAccessPointRegistry() {
return dataAccessPointRegistry;
}

/**
* Get {@link ServletContext} (may be null).
*/
public ServletContext getServletContext() {
return request.getServletContext();
}

/**
* Get the {@link MetricsProvider} for this action.
*/
public MetricsProvider getMetricsProvider() {
ServletContext servletContext = getServletContext();
if ( servletContext == null )
return null;
return MetricsProvider.getMetricsProvider(servletContext);
}

/**
* Set the endpoint and endpoint name that this is an action for.
* @param endpoint {@link Endpoint}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public class FusekiMain extends CmdARQ {
private static ArgDecl argWithMetrics = new ArgDecl(ArgDecl.NoValue, "withMetrics", "metrics");
private static ArgDecl argWithCompact = new ArgDecl(ArgDecl.NoValue, "withCompact", "compact");

// Default is "true" and use modules found by the ServiceLoader.
private static ArgDecl argEnableModules = new ArgDecl(ArgDecl.HasValue, "modules", "fuseki-modules");
// // Use modules found by the ServiceLoader.
// private static ArgDecl argEnableModules = new ArgDecl(ArgDecl.HasValue, "modules", "fuseki-modules");

private static ArgDecl argAuth = new ArgDecl(ArgDecl.HasValue, "auth");

Expand Down Expand Up @@ -306,7 +306,7 @@ private void argumentsSetup() {
add(argWithMetrics, "--metrics", "Enable /$/metrics");
add(argWithCompact, "--compact", "Enable /$/compact/*");

add(argEnableModules, "--modules=true|false", "Enable Fuseki modules");
//add(argEnableModules, "--modules=true|false", "Enable Fuseki modules");

super.modVersion.addClass("Fuseki", Fuseki.class);

Expand Down Expand Up @@ -575,12 +575,12 @@ private void processStdArguments(Logger log) {
serverArgs.jettyConfigFile = jettyConfigFile;
}

// Allows for external setting of serverArgs.fusekiModules
if ( serverArgs.fusekiModules == null ) {
// Allows for external setting of serverArgs.fusekiModules
boolean withModules = hasValueOfTrue(argEnableModules);
serverArgs.fusekiModules = withModules
? FusekiModules.getSystemModules()
: FusekiModules.empty();
// Get modules from system-wide setup.
// This (Fuseki 5.3.0- defaults to an empty set of modules.
// boolean withModules = hasValueOfTrue(argEnableModules);
serverArgs.fusekiModules = FusekiModules.getSystemModules();
}

if ( contains(argCORS) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@
*/
public class FusekiModules {

private static FusekiModules autoLoadedFusekiModules = FusekiAutoModules.get();
// Never null, maybe empty
private static FusekiModules systemFusekiModules = autoLoadedFusekiModules;
private static FusekiModules systemFusekiModules = FusekiModules.create();

/**
* There is a system wide set of modules used when no other modules are indicated.
Expand All @@ -46,15 +45,12 @@ public static void setSystemDefault(FusekiModules fusekiModules) {

/** Restore the original setting of the system default collection. */
public static void restoreSystemDefault() {
systemFusekiModules = autoLoadedFusekiModules;
systemFusekiModules = FusekiModules.create();
}

public static FusekiModules getSystemModules() {
if ( systemFusekiModules == null ) {
if ( autoLoadedFusekiModules == null )
autoLoadedFusekiModules = FusekiAutoModules.get();
systemFusekiModules = autoLoadedFusekiModules;
}
if ( systemFusekiModules == null )
systemFusekiModules = FusekiAutoModules.get();
return systemFusekiModules;
}

Expand All @@ -69,8 +65,7 @@ public static FusekiModules getSystemModules() {
// Testing.
/*package*/ static void resetSystemDefault() {
// Reload, reset. Fresh objects.
autoLoadedFusekiModules = FusekiAutoModules.load();
systemFusekiModules = autoLoadedFusekiModules;
systemFusekiModules = FusekiAutoModules.get();
}

/** Create a collection of Fuseki modules */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.jena.fuseki.build.FusekiConfig;
import org.apache.jena.fuseki.ctl.ActionContainerItem;
import org.apache.jena.fuseki.ctl.JsonDescription;
import org.apache.jena.fuseki.metrics.MetricsProvider;
import org.apache.jena.fuseki.server.DataAccessPoint;
import org.apache.jena.fuseki.server.DataService;
import org.apache.jena.fuseki.server.FusekiVocab;
Expand Down Expand Up @@ -107,6 +108,7 @@ protected JsonValue execGetItem(HttpAction action) {

// ---- POST

/** Create dataset */
@Override
protected JsonValue execPostContainer(HttpAction action) {
UUID uuid = UUID.randomUUID();
Expand Down Expand Up @@ -208,8 +210,13 @@ else if ( WebContent.isMultiPartForm(ct) )
if ( ! datasetPath.equals(dataAccessPoint.getName()) )
FmtLog.warn(action.log, "Inconsistent names: datasetPath = %s; DataAccessPoint name = %s", datasetPath, dataAccessPoint);
succeeded = true;

action.getDataAccessPointRegistry().register(dataAccessPoint);

// Add to metrics
MetricsProvider metricProvider = action.getMetricsProvider();
if ( metricProvider != null )
action.getMetricsProvider().addDataAccessPointMetrics(dataAccessPoint);

action.setResponseContentType(WebContent.contentTypeTextPlain);
ServletOps.success(action);
} catch (IOException ex) { IO.exception(ex); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public static FusekiServer construct(String... args) {
}

FusekiModules modules = serverModules;
// Set system modules - these are picked up in FusekiMain
FusekiModules.setSystemDefault(modules);
FusekiServer server = FusekiServer.construct(args);
return server;
Expand Down

0 comments on commit 2a52238

Please sign in to comment.