PluginProbe
Search Atlas SEO – OTTO AI SEO Automation for WordPress / 2.5.23
Search Atlas SEO – OTTO AI SEO Automation for WordPress v2.5.23
2.6.26 2.6.25 2.6.24 2.6.23 2.6.22 2.6.21 2.6.20 2.6.19 2.6.18 2.6.17 2.6.16 2.6.15 2.6.14 2.6.13 2.6.12 2.6.11 2.6.10 2.6.9 2.6.8 2.6.7 2.6.6 2.6.5 2.6.4 2.6.3 2.5.23 All 138 releases
metasync / telemetry / telemetry-init.php

telemetry-init.php in Search Atlas SEO – OTTO AI SEO Automation for WordPress 2.5.23, at telemetry/telemetry-init.php

164 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Telemetry System Initialization
4 *
5 * Initializes the telemetry system and hooks into WordPress plugin lifecycle
6 * Compatible with PHP 7.1+
7 *
8 * @package Search Engine Labs SEO
9 * @subpackage Telemetry
10 * @since 1.0.0
11 */
12
13 // If this file is called directly, abort.
14 if (!defined('WPINC')) {
15 die;
16 }
17
18 // Include telemetry classes (Sentry-only)
19 require_once plugin_dir_path(__FILE__) . 'config.php';
20 require_once plugin_dir_path(__FILE__) . 'class-sentry-telemetry.php';
21 require_once plugin_dir_path(__FILE__) . 'sentry-wordpress-integration.php';
22 require_once plugin_dir_path(__FILE__) . 'wordpress-error-handler.php';
23 require_once plugin_dir_path(__FILE__) . 'class-telemetry-manager.php';
24 require_once plugin_dir_path(__FILE__) . 'class-request_monitor.php';
25 require_once plugin_dir_path(__FILE__) . 'sentry-helper.php';
26
27
28 /**
29 * Initialize telemetry system
30 */
31 function init_metasync_telemetry() {
32 // Optimized memory check with caching
33 static $memory_check_done = false;
34 static $memory_safe = true;
35
36 // Only check memory once per request to reduce overhead
37 if (!$memory_check_done) {
38 $current_memory = memory_get_usage(true);
39 $memory_limit_str = ini_get('memory_limit');
40
41 // Parse memory limit manually since wp_convert_hr_to_bytes might not be available
42 if ($memory_limit_str === '-1' || $memory_limit_str === -1) {
43 $memory_limit = PHP_INT_MAX; // Unlimited
44 } else {
45 $memory_limit = trim($memory_limit_str);
46 $last = strtolower($memory_limit[strlen($memory_limit) - 1]);
47 $value = (int) $memory_limit;
48
49 switch ($last) {
50 case 'g':
51 $value *= 1024;
52 case 'm':
53 $value *= 1024;
54 case 'k':
55 $value *= 1024;
56 }
57 $memory_limit = $value;
58 }
59
60 // If we're using more than 70% of memory, skip telemetry entirely
61 $memory_safe = $current_memory <= ($memory_limit * 0.7);
62 $memory_check_done = true;
63
64 if (!$memory_safe) {
65 // error_log('MetaSync Telemetry: Disabled due to high memory usage (' . round($current_memory / 1024 / 1024, 2) . 'MB / ' . round($memory_limit / 1024 / 1024, 2) . 'MB)');
66 return;
67 }
68 } else if (!$memory_safe) {
69 return; // Skip if memory was already determined to be unsafe
70 }
71
72 // Check if telemetry is explicitly disabled
73 if (defined('METASYNC_DISABLE_TELEMETRY') && constant('METASYNC_DISABLE_TELEMETRY')) {
74 return;
75 }
76
77 try {
78 // Clean up old database options if they exist
79 delete_option('metasync_sentry_dsn');
80 delete_option('metasync_sentry_project_id');
81 delete_option('metasync_sentry_environment');
82 delete_option('metasync_sentry_release');
83 delete_option('metasync_sentry_sample_rate');
84
85 // Telemetry configuration is now handled by constants defined in metasync.php
86 // METASYNC_SENTRY_PROJECT_ID, METASYNC_SENTRY_ENVIRONMENT, etc.
87
88 // Only initialize if memory is still safe
89 $memory_after_options = memory_get_usage(true);
90 if ($memory_after_options > ($memory_limit * 0.8)) {
91 // NEW: Structured error logging with category and code
92 if (class_exists('Metasync_Error_Logger')) {
93 $memory_used_mb = round($memory_after_options / 1024 / 1024, 2);
94 $memory_limit_mb = round($memory_limit / 1024 / 1024, 2);
95 $memory_percent = round(($memory_after_options / $memory_limit) * 100, 1);
96
97 Metasync_Error_Logger::log(
98 Metasync_Error_Logger::CATEGORY_MEMORY_EXHAUSTED,
99 Metasync_Error_Logger::SEVERITY_CRITICAL,
100 'Memory limit nearly exhausted - telemetry initialization skipped',
101 [
102 'memory_used_mb' => $memory_used_mb,
103 'memory_limit_mb' => $memory_limit_mb,
104 'memory_percent' => $memory_percent,
105 'threshold' => '80%',
106 'operation' => 'telemetry_init',
107 'action' => 'skipped_initialization'
108 ]
109 );
110 }
111
112 // error_log('MetaSync Telemetry: Skipping manager initialization due to memory usage');
113 return;
114 }
115
116 Metasync_Telemetry_Manager::get_instance();
117
118 // API request monitoring removed - creates excessive overhead
119 // Class kept for backward compatibility but not instantiated
120 // Already handled by log-sync.php
121
122 } catch (Exception $e) {
123 // Fail silently to prevent site crashes
124 // error_log('MetaSync Telemetry: Initialization failed: ' . $e->getMessage());
125 }
126 }
127
128 // Only initialize telemetry if not explicitly disabled
129 if (!defined('METASYNC_DISABLE_TELEMETRY') || !constant('METASYNC_DISABLE_TELEMETRY')) {
130 // Add hook - memory check is now handled inside the function for efficiency
131 add_action('plugins_loaded', 'init_metasync_telemetry', 5);
132 }
133
134 /**
135 * Helper function to get telemetry manager instance
136 *
137 * @return Metasync_Telemetry_Manager
138 */
139 function metasync_telemetry() {
140 return Metasync_Telemetry_Manager::get_instance();
141 }
142
143 /**
144 * Cleanup scheduled cron jobs from old telemetry system
145 */
146 function cleanup_metasync_legacy_cron_jobs() {
147 // Remove old cron jobs
148 $timestamp = wp_next_scheduled('metasync_process_background_telemetry');
149 if ($timestamp) {
150 wp_unschedule_event($timestamp, 'metasync_process_background_telemetry');
151 }
152
153 $timestamp = wp_next_scheduled('metasync_process_file_queue');
154 if ($timestamp) {
155 wp_unschedule_event($timestamp, 'metasync_process_file_queue');
156 }
157
158 $timestamp = wp_next_scheduled('metasync_telemetry_queue_flush');
159 if ($timestamp) {
160 wp_unschedule_event($timestamp, 'metasync_telemetry_queue_flush');
161 }
162 }
163 add_action('init', 'cleanup_metasync_legacy_cron_jobs', 1);
164