@@ -68,30 +68,32 @@ public final class PythonSensor implements Sensor {
68
68
static final String UNSET_VERSION_WARNING = "Your code is analyzed as compatible with all Python 3 versions by default." +
69
69
" You can get a more precise analysis by setting the exact Python version in your configuration via the parameter \" sonar.python.version\" " ;
70
70
71
+ private final SensorTelemetryStorage sensorTelemetryStorage ;
72
+
71
73
/**
72
74
* Constructor to be used by pico if neither PythonCustomRuleRepository nor PythonIndexer are to be found and injected.
73
75
*/
74
76
public PythonSensor (FileLinesContextFactory fileLinesContextFactory , CheckFactory checkFactory ,
75
77
NoSonarFilter noSonarFilter , AnalysisWarningsWrapper analysisWarnings ) {
76
- this (fileLinesContextFactory , checkFactory , noSonarFilter , null , null , null , analysisWarnings );
78
+ this (fileLinesContextFactory , checkFactory , noSonarFilter , null , null , null , analysisWarnings , new SensorTelemetryStorage () );
77
79
}
78
80
79
81
public PythonSensor (FileLinesContextFactory fileLinesContextFactory , CheckFactory checkFactory , NoSonarFilter noSonarFilter ,
80
82
PythonCustomRuleRepository [] customRuleRepositories , AnalysisWarningsWrapper analysisWarnings ) {
81
- this (fileLinesContextFactory , checkFactory , noSonarFilter , customRuleRepositories , null , null , analysisWarnings );
83
+ this (fileLinesContextFactory , checkFactory , noSonarFilter , customRuleRepositories , null , null , analysisWarnings , new SensorTelemetryStorage () );
82
84
}
83
85
84
86
public PythonSensor (FileLinesContextFactory fileLinesContextFactory , CheckFactory checkFactory , NoSonarFilter noSonarFilter ,
85
87
PythonIndexer indexer , SonarLintCache sonarLintCache , AnalysisWarningsWrapper analysisWarnings ) {
86
88
// ^^ This constructor implicitly assumes that a PythonIndexer and a SonarLintCache are always available at the same time.
87
89
// In practice, this is currently the case, since both are provided by PythonPlugin under the same conditions.
88
90
// See also PythonPlugin::SonarLintPluginAPIManager::addSonarlintPythonIndexer.
89
- this (fileLinesContextFactory , checkFactory , noSonarFilter , null , indexer , sonarLintCache , analysisWarnings );
91
+ this (fileLinesContextFactory , checkFactory , noSonarFilter , null , indexer , sonarLintCache , analysisWarnings , new SensorTelemetryStorage () );
90
92
}
91
93
92
94
public PythonSensor (FileLinesContextFactory fileLinesContextFactory , CheckFactory checkFactory , NoSonarFilter noSonarFilter ,
93
95
@ Nullable PythonCustomRuleRepository [] customRuleRepositories , @ Nullable PythonIndexer indexer ,
94
- @ Nullable SonarLintCache sonarLintCache , AnalysisWarningsWrapper analysisWarnings ) {
96
+ @ Nullable SonarLintCache sonarLintCache , AnalysisWarningsWrapper analysisWarnings , SensorTelemetryStorage sensorTelemetryStorage ) {
95
97
this .checks = new PythonChecks (checkFactory )
96
98
.addChecks (CheckList .REPOSITORY_KEY , CheckList .getChecks ())
97
99
.addCustomChecks (customRuleRepositories );
@@ -100,6 +102,12 @@ public PythonSensor(FileLinesContextFactory fileLinesContextFactory, CheckFactor
100
102
this .indexer = indexer ;
101
103
this .sonarLintCache = sonarLintCache ;
102
104
this .analysisWarnings = analysisWarnings ;
105
+ this .sensorTelemetryStorage = sensorTelemetryStorage ;
106
+ }
107
+
108
+ public PythonSensor (FileLinesContextFactory fileLinesContextFactory , CheckFactory checkFactory , NoSonarFilter mock , PythonCustomRuleRepository [] customRuleRepositories ,
109
+ AnalysisWarningsWrapper analysisWarnings , SensorTelemetryStorage sensorTelemetryStorage ) {
110
+ this (fileLinesContextFactory , checkFactory , mock , customRuleRepositories , null , null , analysisWarnings , sensorTelemetryStorage );
103
111
}
104
112
105
113
@ Override
@@ -121,15 +129,25 @@ public void execute(SensorContext context) {
121
129
if (pythonVersionParameter .length != 0 ){
122
130
ProjectPythonVersion .setCurrentVersions (PythonVersionUtils .fromStringArray (pythonVersionParameter ));
123
131
}
132
+ setTelemetry (context , pythonVersionParameter );
124
133
CacheContext cacheContext = CacheContextImpl .of (context );
125
134
PythonIndexer pythonIndexer = this .indexer != null ? this .indexer : new SonarQubePythonIndexer (pythonFiles , cacheContext , context );
126
135
pythonIndexer .setSonarLintCache (sonarLintCache );
127
136
TypeShed .setProjectLevelSymbolTable (pythonIndexer .projectLevelSymbolTable ());
128
137
PythonScanner scanner = new PythonScanner (context , checks , fileLinesContextFactory , noSonarFilter , PythonParser .create (), pythonIndexer );
129
138
scanner .execute (pythonFiles , context );
139
+ sensorTelemetryStorage .send (context );
130
140
durationReport .stop ();
131
141
}
132
142
143
+ private void setTelemetry (SensorContext context , String [] pythonVersionParameter ) {
144
+ var isVersionSet = pythonVersionParameter .length != 0 || context .runtime ().getProduct () == SonarProduct .SONARLINT ;
145
+ if (pythonVersionParameter .length != 0 ) {
146
+ sensorTelemetryStorage .updateMetric (TelemetryMetricKey .PYTHON_VERSION_KEY , String .join ("," , pythonVersionParameter ));
147
+ }
148
+ sensorTelemetryStorage .updateMetric (TelemetryMetricKey .PYTHON_VERSION_SET_KEY , isVersionSet ? "1" : "0" );
149
+ }
150
+
133
151
private static List <PythonInputFile > getInputFiles (SensorContext context ) {
134
152
FilePredicates p = context .fileSystem ().predicates ();
135
153
Iterable <InputFile > it = context .fileSystem ().inputFiles (p .and (p .hasLanguage (Python .KEY )));
0 commit comments