Skip to content

Commit 0065cd0

Browse files
SONARJAVA-5673 Create a proxy class for accessing telemetry API. (#5248)
1 parent 769e10f commit 0065cd0

File tree

5 files changed

+128
-1
lines changed

5 files changed

+128
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
package org.sonar.java;
18+
19+
/**
20+
* Provides access to the APIs for reporting telemetry.
21+
*/
22+
public interface Telemetry {
23+
// `addMetric` will forward the call to `addTelemetryProperty`.
24+
// We chose a different name to make textual search for the real thing easier.
25+
void addMetric(TelemetryKey key, String value);
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
package org.sonar.java;
18+
19+
/**
20+
* Telemetry keys used by the Java analyzer.
21+
*/
22+
public enum TelemetryKey {
23+
JAVA_LANGUAGE_VERSION("java.language.version");
24+
25+
private final String key;
26+
27+
TelemetryKey(String key) {
28+
this.key = key;
29+
}
30+
31+
public String key() {
32+
return key;
33+
}
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
package org.sonar.java;
18+
19+
import org.junit.jupiter.api.Test;
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
class TelemetryKeyTest {
23+
@Test
24+
void test() {
25+
assertThat(TelemetryKey.JAVA_LANGUAGE_VERSION.key()).isEqualTo("java.language.version");
26+
}
27+
}

sonar-java-plugin/src/main/java/org/sonar/plugins/java/JavaSensor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.sonar.java.JavaFrontend;
4242
import org.sonar.java.Measurer;
4343
import org.sonar.java.SonarComponents;
44+
import org.sonar.java.Telemetry;
4445
import org.sonar.java.filters.PostAnalysisIssueFilter;
4546
import org.sonar.java.jsp.Jasper;
4647
import org.sonar.java.model.GeneratedFile;
@@ -51,6 +52,7 @@
5152
import org.sonarsource.performance.measure.PerformanceMeasure;
5253

5354
import static org.sonar.api.rules.RuleAnnotationUtils.getRuleKey;
55+
import static org.sonar.java.TelemetryKey.JAVA_LANGUAGE_VERSION;
5456

5557
@Phase(name = Phase.Name.PRE)
5658
@DependedUpon("org.sonar.plugins.java.JavaSensor")
@@ -103,9 +105,10 @@ public void execute(SensorContext context) {
103105
sonarComponents.setCheckFilter(createCheckFilter(sonarComponents.isAutoScanCheckFiltering()));
104106

105107
Measurer measurer = new Measurer(context, noSonarFilter);
108+
Telemetry telemetry = new SensorTelemetry(context);
106109

107110
JavaVersion javaVersion = getJavaVersion();
108-
context.addTelemetryProperty("java.language.version", javaVersion.toString());
111+
telemetry.addMetric(JAVA_LANGUAGE_VERSION, javaVersion.toString());
109112

110113
JavaFrontend frontend = new JavaFrontend(javaVersion, sonarComponents, measurer, javaResourceLocator, postAnalysisIssueFilter,
111114
sonarComponents.mainChecks().toArray(new JavaCheck[0]));
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
package org.sonar.plugins.java;
18+
19+
import org.sonar.api.batch.sensor.SensorContext;
20+
import org.sonar.java.Telemetry;
21+
import org.sonar.java.TelemetryKey;
22+
23+
/**
24+
* Wraps up {@link SensorContext} to allow passing it around without exposing other APIs.
25+
*/
26+
public class SensorTelemetry implements Telemetry {
27+
private final SensorContext context;
28+
29+
public SensorTelemetry(SensorContext context) {
30+
this.context = context;
31+
}
32+
33+
@Override
34+
public void addMetric(TelemetryKey key, String value) {
35+
this.context.addTelemetryProperty(key.key(), value);
36+
}
37+
}

0 commit comments

Comments
 (0)