2828import org .slf4j .Logger ;
2929import org .slf4j .LoggerFactory ;
3030import org .springframework .beans .factory .annotation .Autowired ;
31+ import org .springframework .beans .factory .annotation .Value ;
3132import org .springframework .stereotype .Service ;
3233
3334/**
@@ -49,6 +50,9 @@ public class MetricsService {
4950 @ Autowired
5051 private MeterRegistry meterRegistry ;
5152
53+ @ Value ("${app.telemetry.enabled:false}" )
54+ private boolean telemetryEnabled ;
55+
5256 // Metric names as constants to avoid typos
5357 private static final String USER_LOGINS = "wisemapping.api.user.auth" ;
5458 private static final String USER_LOGOUTS = "wisemapping.api.user.logouts" ;
@@ -74,6 +78,7 @@ public class MetricsService {
7478 * @param authType The authentication type used (e.g., "database", "oauth", "google")
7579 */
7680 public void trackUserLogin (@ NotNull Account user , @ NotNull String authType ) {
81+ if (!telemetryEnabled ) return ;
7782 try {
7883 Counter .builder (USER_LOGINS )
7984 .description ("Total number of user logins" )
@@ -94,6 +99,7 @@ public void trackUserLogin(@NotNull Account user, @NotNull String authType) {
9499 * @param logoutType The logout type (e.g., "manual", "session_expired", "admin")
95100 */
96101 public void trackUserLogout (@ NotNull Account user , @ NotNull String logoutType ) {
102+ if (!telemetryEnabled ) return ;
97103 try {
98104 Counter .builder (USER_LOGOUTS )
99105 .description ("Total number of user logouts" )
@@ -113,13 +119,13 @@ public void trackUserLogout(@NotNull Account user, @NotNull String logoutType) {
113119 * @param user The newly registered user
114120 */
115121 public void trackUserRegistration (@ NotNull Account user ) {
122+ if (!telemetryEnabled ) return ;
116123 try {
117124 String authType = user .getAuthenticationType () != null ?
118125 String .valueOf (user .getAuthenticationType ().getCode ()) : "unknown" ;
119126
120127 Counter .builder (USER_REGISTRATIONS )
121128 .description ("Total number of user registrations" )
122- .tag ("email_provider" , emailProvider )
123129 .tag ("auth_type" , authType )
124130 .register (meterRegistry )
125131 .increment ();
@@ -135,6 +141,7 @@ public void trackUserRegistration(@NotNull Account user) {
135141 * @param user The user whose account was activated
136142 */
137143 public void trackUserActivation (@ NotNull Account user ) {
144+ if (!telemetryEnabled ) return ;
138145 try {
139146 String authType = user .getAuthenticationType () != null ?
140147 String .valueOf (user .getAuthenticationType ().getCode ()) : "unknown" ;
@@ -158,6 +165,7 @@ public void trackUserActivation(@NotNull Account user) {
158165 * @param creationType The type of creation (e.g., "new", "duplicate", "tutorial", "import")
159166 */
160167 public void trackMindmapCreation (@ NotNull Mindmap mindmap , @ NotNull Account user , @ NotNull String creationType ) {
168+ if (!telemetryEnabled ) return ;
161169 try {
162170 String visibility = mindmap .isPublic () ? "public" : "private" ;
163171
@@ -180,26 +188,29 @@ public void trackMindmapCreation(@NotNull Mindmap mindmap, @NotNull Account user
180188 * @param user The user who was suspended
181189 * @param reason The suspension reason
182190 */
183- public void trackUserSuspension (@ NotNull Account user , @ NotNull String reason ) {
184- Counter .builder (USER_SUSPENSIONS )
185- .description ("Total number of users suspended" )
186- .tag ("reason" , reason .toLowerCase ())
187- .tag ("user_type" , String .valueOf (user .getAuthenticationType ().getCode ()))
188- .register (meterRegistry )
189- .increment ();
190-
191- logger .debug ("Tracked user suspension metric" );
192- } catch (Exception e ) {
193- logger .warn ("Failed to track user suspension metric: {}" , e .getMessage ());
194- }
195- }
191+ public void trackUserSuspension (@ NotNull Account user , @ NotNull String reason ) {
192+ if (!telemetryEnabled ) return ;
193+ try {
194+ Counter .builder (USER_SUSPENSIONS )
195+ .description ("Total number of users suspended" )
196+ .tag ("reason" , reason .toLowerCase ())
197+ .tag ("user_type" , String .valueOf (user .getAuthenticationType ().getCode ()))
198+ .register (meterRegistry )
199+ .increment ();
200+
201+ logger .debug ("Tracked user suspension metric" );
202+ } catch (Exception e ) {
203+ logger .warn ("Failed to track user suspension metric: {}" , e .getMessage ());
204+ }
205+ }
196206
197207 /**
198208 * Track when a mindmap is made public
199209 * @param mindmap The mindmap that was made public
200210 * @param user The user who made it public
201211 */
202212 public void trackMindmapMadePublic (@ NotNull Mindmap mindmap , @ NotNull Account user ) {
213+ if (!telemetryEnabled ) return ;
203214 try {
204215 Counter .builder (MINDMAPS_MADE_PUBLIC )
205216 .description ("Total number of mindmaps made public" )
@@ -221,6 +232,7 @@ public void trackMindmapMadePublic(@NotNull Mindmap mindmap, @NotNull Account us
221232 * @param sharedBy The user who shared the mindmap
222233 */
223234 public void trackMindmapShared (@ NotNull Mindmap mindmap , @ NotNull String role , @ NotNull Account sharedBy ) {
235+ if (!telemetryEnabled ) return ;
224236 try {
225237 Counter .builder (MINDMAPS_SHARED )
226238 .description ("Total number of mindmaps shared with collaborators" )
@@ -243,6 +255,7 @@ public void trackMindmapShared(@NotNull Mindmap mindmap, @NotNull String role, @
243255 * @param context The context where spam analysis was performed (e.g., "creation", "update", "batch_scan")
244256 */
245257 public void trackSpamAnalysis (@ NotNull Mindmap mindmap , @ NotNull SpamDetectionResult spamResult , @ NotNull String context ) {
258+ if (!telemetryEnabled ) return ;
246259 try {
247260 String isSpam = spamResult .isSpam () ? "yes" : "no" ;
248261 String spamType = spamResult .isSpam () && spamResult .getStrategyType () != null ?
@@ -271,6 +284,7 @@ public void trackSpamAnalysis(@NotNull Mindmap mindmap, @NotNull SpamDetectionRe
271284 * @param context The context where spam was detected (e.g., "creation", "update", "batch_scan")
272285 */
273286 public void trackSpamDetection (@ NotNull Mindmap mindmap , @ NotNull SpamDetectionResult spamResult , @ NotNull String context ) {
287+ if (!telemetryEnabled ) return ;
274288 try {
275289 if (spamResult .isSpam ()) {
276290 Counter .builder (SPAM_DETECTED )
@@ -295,6 +309,7 @@ public void trackSpamDetection(@NotNull Mindmap mindmap, @NotNull SpamDetectionR
295309 * @param action The action that was prevented (e.g., "publish", "share")
296310 */
297311 public void trackSpamPrevention (@ NotNull Mindmap mindmap , @ NotNull String action ) {
312+ if (!telemetryEnabled ) return ;
298313 try {
299314 Counter .builder (SPAM_PREVENTED )
300315 .description ("Total number of actions prevented due to spam detection" )
@@ -315,6 +330,7 @@ public void trackSpamPrevention(@NotNull Mindmap mindmap, @NotNull String action
315330 * @param suspended Total number of users suspended in the batch
316331 */
317332 public void trackInactiveUserProcessing (int processed , int suspended ) {
333+ if (!telemetryEnabled ) return ;
318334 try {
319335 Counter .builder (INACTIVE_USERS_PROCESSED )
320336 .description ("Total number of inactive users processed" )
@@ -339,6 +355,7 @@ public void trackInactiveUserProcessing(int processed, int suspended) {
339355 * @param suspended Number of users suspended in this batch
340356 */
341357 public void trackInactiveUserBatchSuspension (int suspended ) {
358+ if (!telemetryEnabled ) return ;
342359 try {
343360 if (suspended > 0 ) {
344361 Counter .builder (INACTIVE_USERS_BATCH_SUSPENDED )
@@ -358,6 +375,7 @@ public void trackInactiveUserBatchSuspension(int suspended) {
358375 * @param candidates Number of users identified as suspension candidates in dry run
359376 */
360377 public void trackInactiveUserDryRunCandidates (int candidates ) {
378+ if (!telemetryEnabled ) return ;
361379 try {
362380 if (candidates > 0 ) {
363381 Counter .builder (INACTIVE_USERS_DRY_RUN_CANDIDATES )
@@ -378,6 +396,7 @@ public void trackInactiveUserDryRunCandidates(int candidates) {
378396 * @param mindmapsMigrated Total number of mindmaps migrated to inactive table
379397 */
380398 public void trackInactiveMindmapMigration (int usersProcessed , int mindmapsMigrated ) {
399+ if (!telemetryEnabled ) return ;
381400 try {
382401 Counter .builder (INACTIVE_MINDMAPS_USERS_PROCESSED )
383402 .description ("Total number of inactive users processed for mindmap migration" )
0 commit comments