未验证 提交 e76110f4 编写于 作者: R R. Tyler Croy 提交者: GitHub

Merge pull request #3698 from daniel-beck/JENKINS-54136

[JENKINS-54136] Use per-trial correlation IDs
......@@ -23,6 +23,7 @@
*/
package jenkins.telemetry;
import com.google.common.annotations.VisibleForTesting;
import hudson.Extension;
import hudson.model.Describable;
import hudson.model.Descriptor;
......@@ -56,6 +57,12 @@ public class Correlator extends Descriptor<Correlator> implements Describable<Co
return correlationId;
}
@Restricted(NoExternalUse.class)
@VisibleForTesting
void setCorrelationId(String correlationId) {
this.correlationId = correlationId;
}
@Override
public Descriptor<Correlator> getDescriptor() {
return this;
......
......@@ -34,6 +34,7 @@ import hudson.model.UsageStatistics;
import jenkins.model.Jenkins;
import jenkins.util.SystemProperties;
import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
......@@ -178,7 +179,8 @@ public abstract class Telemetry implements ExtensionPoint {
JSONObject wrappedData = new JSONObject();
wrappedData.put("type", telemetry.getId());
wrappedData.put("payload", data);
wrappedData.put("correlator", ExtensionList.lookupSingleton(Correlator.class).getCorrelationId());
String correlationId = ExtensionList.lookupSingleton(Correlator.class).getCorrelationId();
wrappedData.put("correlator", DigestUtils.sha256Hex(correlationId + telemetry.getId()));
try {
URL url = new URL(ENDPOINT);
......
......@@ -7,7 +7,9 @@ import static org.junit.Assert.*;
import hudson.security.csrf.CrumbExclusion;
import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
......@@ -29,7 +31,11 @@ import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.UUID;
import java.util.logging.Level;
import java.util.regex.Pattern;
public class TelemetryTest {
@Rule
......@@ -40,11 +46,19 @@ public class TelemetryTest {
private static int counter = 0;
@Before
public void prepare() throws Exception {
correlators.clear();
types.clear();
counter = 0;
j.jenkins.setNoUsageStatistics(false); // tests usually don't submit this, but we need this
Telemetry.ENDPOINT = j.getURL().toString() + "uplink/events";
}
@Test
public void testSubmission() throws Exception {
j.jenkins.setNoUsageStatistics(false); // tests usually don't submit this, but we need this
assertEquals("no requests received", 0, counter);
Telemetry.ENDPOINT = j.getURL().toString() + "uplink/events";
ExtensionList.lookupSingleton(Telemetry.TelemetryReporter.class).doRun();
do {
Thread.sleep(250);
......@@ -56,11 +70,28 @@ public class TelemetryTest {
assertThat(types, hasItem("test-data"));
assertThat(types, not(hasItem("future")));
assertThat(types, not(hasItem("past")));
assertThat(correlators.size(), is(counter));
assertTrue(Pattern.compile("[0-9a-f]+").matcher(correlators.first()).matches());
assertThat(types, not(hasItem("empty")));
assertThat(correlators.size(), is(1));
assertTrue("at least one request received", counter > 0); // TestTelemetry plus whatever real impls exist
}
@Test
public void testPerTrialCorrelator() throws Exception {
Correlator correlator = ExtensionList.lookupSingleton(Correlator.class);
String correlationId = "00000000-0000-0000-0000-000000000000";
correlator.setCorrelationId(correlationId);
ExtensionList.lookupSingleton(Telemetry.TelemetryReporter.class).doRun();
do {
Thread.sleep(250);
} while (counter == 0); // this might end up being flaky due to 1 to many active telemetry trials
assertThat(types, hasItem("test-data"));
//90ecf3ce1cd5ba1e5ad3cde7ad08a941e884f2e4d9bd463361715abab8efedc5
assertThat(correlators, hasItem(DigestUtils.sha256Hex(correlationId + "test-data")));
}
@TestExtension
public static class EmptyTelemetry extends Telemetry {
......@@ -209,7 +240,7 @@ public class TelemetryTest {
}
}
private static Set<String> correlators = new HashSet<>();
private static SortedSet<String> correlators = new TreeSet<>();
private static Set<String> types = new HashSet<>();
@TestExtension
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册