Skip to content

Commit

Permalink
[JENKINS-54029] Add telemetry for Stapler dispatches (#3688)
Browse files Browse the repository at this point in the history
* Add telemetry for Stapler dispatches

* Add TODO

* Released version of Stapler

(cherry picked from commit d1cbb20)
  • Loading branch information
daniel-beck authored and olivergondza committed Oct 23, 2018
1 parent 9da2717 commit b9725c1
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
120 changes: 120 additions & 0 deletions core/src/main/java/jenkins/telemetry/impl/StaplerDispatches.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* The MIT License
*
* Copyright (c) 2018, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package jenkins.telemetry.impl;

import hudson.Extension;
import hudson.PluginWrapper;
import hudson.model.UsageStatistics;
import hudson.util.VersionNumber;
import jenkins.model.Jenkins;
import jenkins.telemetry.Telemetry;
import jenkins.util.SystemProperties;
import net.sf.json.JSONObject;
import org.kohsuke.MetaInfServices;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.EvaluationTrace;
import org.kohsuke.stapler.StaplerRequest;

import javax.annotation.Nonnull;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.TreeSet;

/**
* Telemetry implementation gathering information about Stapler dispatch routes.
*/
@Extension
@Restricted(NoExternalUse.class)
public class StaplerDispatches extends Telemetry {
@Nonnull
@Override
public LocalDate getStart() {
return LocalDate.of(2018, 10, 10);
}

@Nonnull
@Override
public LocalDate getEnd() {
return LocalDate.of(2019, 2, 1);
}

@Nonnull
@Override
public String getDisplayName() {
return "Stapler request handling";
}

@Nonnull
@Override
public JSONObject createContent() {
Map<String, Object> info = new TreeMap<>();
info.put("components", buildComponentInformation());
info.put("dispatches", buildDispatches());

return JSONObject.fromObject(info);
}

private Object buildDispatches() {
Set<String> currentTraces = traces;
traces = new TreeSet<>();
return currentTraces;
}

private Object buildComponentInformation() {
Map<String, String> components = new TreeMap<>();
VersionNumber core = Jenkins.getVersion();
components.put("jenkins-core", core == null ? "" : core.toString());

for (PluginWrapper plugin : Jenkins.get().pluginManager.getPlugins()) {
if (plugin.isActive()) {
components.put(plugin.getShortName(), plugin.getVersion());
}
}
return components;
}

@MetaInfServices
public static class StaplerTrace extends EvaluationTrace.ApplicationTracer {

@Override
protected void record(StaplerRequest staplerRequest, String s) {
if (UsageStatistics.DISABLED || !Jenkins.get().isUsageStatisticsCollected()) {
// TODO use new API after jenkinsci/jenkins#3687 is merged
// do not collect traces while usage statistics are disabled
return;
}
traces.add(s);
}
}

private static volatile Set<String> traces = new TreeSet<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core">
Stapler is the web framework used by Jenkins.
To help develop an upcoming major revision of the framework, this trial collects information about how Stapler processes HTTP requests.
In addition to references to classes, methods, and Stapler-related resource files involved in Stapler request processing, this trial collects the names of installed plugins, their versions, and the version of Jenkins core.
It does <strong>not</strong> collect URLs or any user data.
</j:jelly>

0 comments on commit b9725c1

Please sign in to comment.