Skip to content

Commit ac283a1

Browse files
committed
SLCORE-1245 Remove server name from monitoring data
1 parent 2c3eaf7 commit ac283a1

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/monitoring/MonitoringService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
*/
2020
package org.sonarsource.sonarlint.core.commons.monitoring;
2121

22+
import io.sentry.Hint;
2223
import io.sentry.Sentry;
24+
import io.sentry.SentryBaseEvent;
2325
import io.sentry.SentryOptions;
2426
import jakarta.inject.Inject;
2527
import org.apache.commons.lang3.SystemUtils;
@@ -73,9 +75,16 @@ SentryOptions getSentryConfiguration() {
7375
sentryOptions.addInAppInclude("org.sonarsource.sonarlint");
7476
sentryOptions.setTracesSampleRate(getTracesSampleRate());
7577
addCaptureIgnoreRule(sentryOptions, "(?s)com\\.sonar\\.sslr\\.api\\.RecognitionException.*");
78+
sentryOptions.setBeforeSend(MonitoringService::scrubPii);
79+
sentryOptions.setBeforeSendTransaction(MonitoringService::scrubPii);
7680
return sentryOptions;
7781
}
7882

83+
private static <T extends SentryBaseEvent> T scrubPii(T event, Hint hint) {
84+
event.setServerName(null);
85+
return event;
86+
}
87+
7988
private static String getDsn() {
8089
return System.getProperty(DSN_PROPERTY, DSN_DEFAULT);
8190
}

medium-tests/src/test/java/mediumtest/monitoring/MonitoringMediumTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.junit.jupiter.api.BeforeEach;
3030
import org.junit.jupiter.api.extension.ExtendWith;
3131
import org.junit.jupiter.api.io.TempDir;
32+
import org.sonarsource.sonarlint.core.commons.monitoring.DogfoodEnvironmentDetectionService;
3233
import org.sonarsource.sonarlint.core.commons.monitoring.MonitoringService;
3334
import org.sonarsource.sonarlint.core.rpc.protocol.backend.analysis.AnalyzeFilesAndTrackParams;
3435
import org.sonarsource.sonarlint.core.rpc.protocol.backend.file.DidUpdateFileSystemParams;
@@ -37,6 +38,8 @@
3738
import org.sonarsource.sonarlint.core.rpc.protocol.common.Language;
3839
import org.sonarsource.sonarlint.core.test.utils.junit5.SonarLintTest;
3940
import org.sonarsource.sonarlint.core.test.utils.junit5.SonarLintTestHarness;
41+
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
42+
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
4043
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
4144
import utils.TestPlugin;
4245

@@ -57,12 +60,16 @@ class MonitoringMediumTests {
5760
private static final String CONFIGURATION_SCOPE_ID = "configScopeId";
5861
private WireMockServer sentryServer;
5962

63+
@SystemStub
64+
private EnvironmentVariables environmentVariables;
65+
6066
@BeforeEach
6167
void setup() {
6268
sentryServer = new WireMockServer(wireMockConfig().dynamicPort());
6369
sentryServer.start();
6470
System.setProperty(MonitoringService.DSN_PROPERTY, createValidSentryDsn(sentryServer));
6571
System.setProperty(MonitoringService.TRACES_SAMPLE_RATE_PROPERTY, "1");
72+
environmentVariables.set(DogfoodEnvironmentDetectionService.SONARSOURCE_DOGFOODING_ENV_VAR_KEY, "1");
6673
setupSentryStubs();
6774
}
6875

@@ -114,6 +121,9 @@ function writeMsg($fname) {
114121
var issues = analyzeFileAndGetIssues(inputFile.toUri(), client, backend, CONFIGURATION_SCOPE_ID);
115122

116123
assertThat(issues).extracting(RaisedIssueDto::getRuleKey, i -> i.getTextRange().getStartLine()).contains(tuple("php:S1172", 2));
124+
125+
// The mock Sentry server receives 1 event for the analysis trace
126+
await().atMost(1, TimeUnit.SECONDS).untilAsserted(() -> assertThat(sentryServer.getAllServeEvents()).hasSize(1));
117127
}
118128

119129
@SonarLintTest
@@ -148,6 +158,12 @@ function writeMsg($fname) {
148158
.join();
149159
assertThat(analysisResult.getFailedAnalysisFiles()).isEmpty();
150160
await().during(2, TimeUnit.SECONDS).untilAsserted(() -> assertThat(client.getRaisedIssuesForScopeIdAsList(CONFIGURATION_SCOPE_ID)).isEmpty());
161+
// The mock Sentry server receives 2 events: one for the trace, one for the exception
162+
await().atMost(1, TimeUnit.SECONDS).untilAsserted(() -> assertThat(sentryServer.getAllServeEvents()).hasSize(2));
163+
assertThat(sentryServer.getAllServeEvents())
164+
.extracting(e -> e.getRequest().getBodyAsString())
165+
// Server name should be removed from events
166+
.noneMatch(m -> m.contains("server_name"));
151167
}
152168

153169
@SonarLintTest

0 commit comments

Comments
 (0)