33namespace Zoon \PyroSpy \Commands ;
44
55use InvalidArgumentException ;
6+ use Monolog \Formatter \JsonFormatter ;
7+ use Monolog \Handler \StreamHandler ;
8+ use Monolog \Level ;
9+ use Monolog \Logger ;
610use Symfony \Component \Console \Command \Command ;
711use Symfony \Component \Console \Input \InputDefinition ;
812use Symfony \Component \Console \Input \InputInterface ;
@@ -41,7 +45,7 @@ protected function configure(): void
4145 'memory ' ,
4246 'm ' ,
4347 InputOption::VALUE_NONE ,
44- 'Process memory traces instead of CPU traces '
48+ 'Process memory traces instead of CPU traces ' ,
4549 ),
4650 new InputOption (
4751 'app ' ,
@@ -98,6 +102,13 @@ protected function configure(): void
98102 'Process trace and phpspy comments/tags with custom class. Can be class or folder with classes ' ,
99103 [],
100104 ),
105+ new InputOption (
106+ 'sendTimeout ' ,
107+ null ,
108+ InputOption::VALUE_OPTIONAL ,
109+ 'TCP connect timeout in seconds ' ,
110+ 10.0 ,
111+ ),
101112 ]))
102113 ;
103114 }
@@ -134,6 +145,31 @@ protected function execute(InputInterface $input, OutputInterface $output): int
134145 throw new InvalidArgumentException ('sendSampleFutureLimit must be positive value ' );
135146 }
136147
148+ $ sendTimeout = (float ) $ input ->getOption ('sendTimeout ' );
149+ if ($ sendTimeout <= 0 ) {
150+ throw new InvalidArgumentException ('sendTimeout must be positive value ' );
151+ }
152+
153+ $ verbosity = $ output ->getVerbosity ();
154+ $ logLevel = match ($ verbosity ) {
155+ OutputInterface::VERBOSITY_SILENT => Level::Emergency,
156+ OutputInterface::VERBOSITY_QUIET => Level::Alert,
157+ OutputInterface::VERBOSITY_NORMAL => Level::Error,
158+ OutputInterface::VERBOSITY_VERBOSE => Level::Warning,
159+ OutputInterface::VERBOSITY_VERY_VERBOSE => Level::Info,
160+ OutputInterface::VERBOSITY_DEBUG => Level::Debug,
161+ };
162+ $ handler = new StreamHandler (STDERR , $ logLevel );
163+ $ formatter = new \Monolog \Formatter \LineFormatter (
164+ ignoreEmptyContextAndExtra: true ,
165+ );
166+ $ formatter ->includeStacktraces (
167+ true ,
168+ static fn (string $ line ): string => $ logLevel ->toRFC5424Level () >= Level::Notice->toRFC5424Level () ? '' : $ line ,
169+ );
170+ $ handler ->setFormatter ($ formatter );
171+ $ logger = new Logger ('pyrospy ' , [$ handler ]);
172+
137173 $ pyroscopeAuthToken = (string ) $ input ->getOption ('pyroscopeAuthToken ' );
138174
139175 $ tags = [];
@@ -170,11 +206,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
170206 $ tags ,
171207 $ pyroscopeAuthToken ,
172208 SenderUnitsEnum::Bytes,
173- SenderAggregationEnum::Average
209+ SenderAggregationEnum::Average,
210+ sendTimeout: $ sendTimeout ,
211+ logger: $ logger ,
174212 );
175213 } else {
176214 $ aggregator = new CpuTraceAggregator ();
177- $ sender = new SampleSender ($ pyroscope , $ app , $ rateHz , $ tags , $ pyroscopeAuthToken );
215+ $ sender = new SampleSender (
216+ $ pyroscope ,
217+ $ app ,
218+ $ rateHz ,
219+ $ tags ,
220+ $ pyroscopeAuthToken ,
221+ logger: $ logger ,
222+ sendTimeout: $ sendTimeout ,
223+ );
178224 }
179225
180226 $ processor = new Processor (
@@ -185,8 +231,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
185231 array_values (array_filter ($ plugins )),
186232 $ sendSampleFutureLimit ,
187233 $ concurrentRequestLimit ,
234+ $ logger ,
188235 );
236+ $ logger ->info ('pyrospy started ' );
189237 $ processor ->process ();
238+ $ logger ->info ('pyrospy shutdown ' );
190239 return Command::SUCCESS ;
191240 }
192241
0 commit comments