Allow plugins to set a custom metrics prefix

By specifying the prefix in the gerrit.config, i.e.

   [plugin "my-plugin"]
       metricsPrefix = "my-root"

the metrics emitted by the plugin will be recorded under:

   /gerrit/my-root/metric-name

rather than the default:

   /gerrit/plugins/my-plugin/metric-name

Change-Id: Id45682c30e66e266ff5360f8f3fa4fa636151b1d
Signed-off-by: David Pursehouse <dpursehouse@collab.net>
This commit is contained in:
David Pursehouse
2017-02-06 20:25:29 +09:00
committed by Jacek Centkowski
parent 085a3089c5
commit c3bbd56a24
5 changed files with 47 additions and 8 deletions

View File

@@ -2220,7 +2220,17 @@ Metric recording statistical distribution (rate) of values.
Note that metrics cannot be recorded from plugin init steps that
are run during site initialization.
Plugin metrics are recorded under `plugins/${plugin-name}/${metric-name}`.
By default, plugin metrics are recorded under
`plugins/${plugin-name}/${metric-name}`. This can be changed by
setting `plugins.${plugin-name}.metricsPrefix` in the `gerrit.config`
file. For example:
----
[plugin "my-plugin"]
metricsPrefix = my-metrics
----
will cause the metrics to be recorded under `my-metrics/${metric-name}`.
See the replication metrics in the
link:https://gerrit.googlesource.com/plugins/replication/+/master/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationMetrics.java[

View File

@@ -17,6 +17,8 @@ package com.google.gerrit.server.plugins;
import static com.google.gerrit.server.plugins.PluginLoader.asTemp;
import com.google.common.base.MoreObjects;
import com.google.gerrit.server.config.PluginConfig;
import com.google.gerrit.server.config.PluginConfigFactory;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Inject;
@@ -45,10 +47,13 @@ public class JarPluginProvider implements ServerPluginProvider {
static final Logger log = LoggerFactory.getLogger(JarPluginProvider.class);
private final Path tmpDir;
private final PluginConfigFactory configFactory;
@Inject
JarPluginProvider(SitePaths sitePaths) {
tmpDir = sitePaths.tmp_dir;
JarPluginProvider(SitePaths sitePaths,
PluginConfigFactory configFactory) {
this.tmpDir = sitePaths.tmp_dir;
this.configFactory = configFactory;
}
@Override
@@ -143,9 +148,12 @@ public class JarPluginProvider implements ServerPluginProvider {
PluginLoader.parentFor(type));
JarScanner jarScanner = createJarScanner(tmp);
PluginConfig pluginConfig = configFactory.getFromGerritConfig(name);
ServerPlugin plugin = new ServerPlugin(name, description.canonicalUrl,
description.user, srcJar, snapshot, jarScanner,
description.dataDir, pluginLoader);
description.dataDir, pluginLoader,
pluginConfig.getString("metricsPrefix", null));
plugin.setCleanupHandle(new CleanupHandle(tmp, jarFile));
keep = true;
return plugin;

View File

@@ -45,9 +45,9 @@ public class PluginMetricMaker extends MetricMaker implements LifecycleListener
private final String prefix;
private final Set<RegistrationHandle> cleanup;
public PluginMetricMaker(MetricMaker root, String pluginName) {
public PluginMetricMaker(MetricMaker root, String prefix) {
this.root = root;
this.prefix = String.format("plugins/%s/", pluginName);
this.prefix = prefix.endsWith("/") ? prefix : prefix + "/";
cleanup = Collections.synchronizedSet(new HashSet<RegistrationHandle>());
}

View File

@@ -42,6 +42,7 @@ public class ServerPlugin extends Plugin {
private final Path dataDir;
private final String pluginCanonicalWebUrl;
private final ClassLoader classLoader;
private final String metricsPrefix;
private Class<? extends Module> sysModule;
private Class<? extends Module> sshModule;
private Class<? extends Module> httpModule;
@@ -59,7 +60,8 @@ public class ServerPlugin extends Plugin {
FileSnapshot snapshot,
PluginContentScanner scanner,
Path dataDir,
ClassLoader classLoader) throws InvalidPluginException {
ClassLoader classLoader,
String metricsPrefix) throws InvalidPluginException {
super(name, srcJar, pluginUser, snapshot,
Plugin.getApiType(getPluginManifest(scanner)));
this.pluginCanonicalWebUrl = pluginCanonicalWebUrl;
@@ -67,9 +69,22 @@ public class ServerPlugin extends Plugin {
this.dataDir = dataDir;
this.classLoader = classLoader;
this.manifest = getPluginManifest(scanner);
this.metricsPrefix = metricsPrefix;
loadGuiceModules(manifest, classLoader);
}
public ServerPlugin(String name,
String pluginCanonicalWebUrl,
PluginUser pluginUser,
Path srcJar,
FileSnapshot snapshot,
PluginContentScanner scanner,
Path dataDir,
ClassLoader classLoader) throws InvalidPluginException {
this(name, pluginCanonicalWebUrl, pluginUser, srcJar, snapshot, scanner,
dataDir, classLoader, null);
}
private void loadGuiceModules(Manifest manifest, ClassLoader classLoader) throws InvalidPluginException {
Attributes main = manifest.getMainAttributes();
String sysName = main.getValue("Gerrit-Module");
@@ -116,6 +131,10 @@ public class ServerPlugin extends Plugin {
return pluginCanonicalWebUrl;
}
String getMetricsPrefix() {
return metricsPrefix;
}
private static Manifest getPluginManifest(PluginContentScanner scanner)
throws InvalidPluginException {
try {

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.server.plugins;
import com.google.common.base.MoreObjects;
import com.google.gerrit.extensions.annotations.PluginCanonicalWebUrl;
import com.google.gerrit.extensions.annotations.PluginData;
import com.google.gerrit.extensions.annotations.PluginName;
@@ -57,7 +58,8 @@ class ServerPluginInfoModule extends AbstractModule {
public void configure() {
PluginMetricMaker metrics = new PluginMetricMaker(
serverMetrics,
plugin.getName());
MoreObjects.firstNonNull(plugin.getMetricsPrefix(),
String.format("plugins/%s/", plugin.getName())));
bind(MetricMaker.class).toInstance(metrics);
listener().toInstance(metrics);
}