Skip to content

Commit 7f438fa

Browse files
authored
Telemetry logger interface (#2211)
* refactor: expand logger methods allowing to pass more optional context - fixed regex rules in symfony telemetry bundle * fix: failing symfony telemetry bundle tests
1 parent f252c49 commit 7f438fa

8 files changed

Lines changed: 246 additions & 50 deletions

File tree

src/bridge/symfony/telemetry-bundle/src/Flow/Bridge/Symfony/TelemetryBundle/Instrumentation/Console/ConsoleSpanSubscriber.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ public function onTerminate(ConsoleTerminateEvent $event) : void
102102

103103
private function matchesPattern(string $command, string $pattern) : bool
104104
{
105-
if (\str_starts_with($pattern, '/') && \str_ends_with($pattern, '/')) {
106-
return (bool) \preg_match($pattern, $command);
105+
$result = @\preg_match($pattern, $command);
106+
107+
if ($result !== false) {
108+
return (bool) $result;
107109
}
108110

109111
return $command === $pattern;

src/bridge/symfony/telemetry-bundle/src/Flow/Bridge/Symfony/TelemetryBundle/Instrumentation/Twig/TracingTwigExtension.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ private function isTemplateExcluded(string $template) : bool
121121

122122
private function matchesPattern(string $template, string $pattern) : bool
123123
{
124-
if (\str_starts_with($pattern, '/') && \str_ends_with($pattern, '/')) {
125-
return (bool) \preg_match($pattern, $template);
124+
$result = @\preg_match($pattern, $template);
125+
126+
if ($result !== false) {
127+
return (bool) $result;
126128
}
127129

128130
return $template === $pattern;

src/bridge/symfony/telemetry-bundle/tests/Flow/Bridge/Symfony/TelemetryBundle/Tests/Integration/Instrumentation/Console/ConsoleSpanSubscriberTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function test_does_not_trace_when_disabled() : void
5757
]);
5858

5959
$application = new Application($kernel);
60-
$application->add(new TestCommand());
60+
$this->addCommand($application, new TestCommand());
6161
$application->setAutoExit(false);
6262
$application->setCatchExceptions(false);
6363

@@ -108,7 +108,7 @@ public function test_excludes_command_with_exact_match() : void
108108
]);
109109

110110
$application = new Application($kernel);
111-
$application->add(new TestCommand());
111+
$this->addCommand($application, new TestCommand());
112112
$application->setAutoExit(false);
113113
$application->setCatchExceptions(false);
114114

@@ -159,8 +159,8 @@ public function test_excludes_command_with_regex_pattern() : void
159159
]);
160160

161161
$application = new Application($kernel);
162-
$application->add(new TestCommand());
163-
$application->add(new FailingCommand());
162+
$this->addCommand($application, new TestCommand());
163+
$this->addCommand($application, new FailingCommand());
164164
$application->setAutoExit(false);
165165
$application->setCatchExceptions(false);
166166

@@ -211,7 +211,7 @@ public function test_traces_failing_console_command() : void
211211
]);
212212

213213
$application = new Application($kernel);
214-
$application->add(new FailingCommand());
214+
$this->addCommand($application, new FailingCommand());
215215
$application->setAutoExit(false);
216216
$application->setCatchExceptions(false);
217217

@@ -271,7 +271,7 @@ public function test_traces_successful_console_command() : void
271271
]);
272272

273273
$application = new Application($kernel);
274-
$application->add(new TestCommand());
274+
$this->addCommand($application, new TestCommand());
275275
$application->setAutoExit(false);
276276
$application->setCatchExceptions(false);
277277

src/bridge/symfony/telemetry-bundle/tests/Flow/Bridge/Symfony/TelemetryBundle/Tests/Integration/KernelTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Flow\Bridge\Symfony\TelemetryBundle\Tests\Context\SymfonyContext;
88
use Flow\Bridge\Symfony\TelemetryBundle\Tests\Fixtures\TestKernel;
99
use PHPUnit\Framework\TestCase;
10+
use Symfony\Component\Console\Application;
11+
use Symfony\Component\Console\Command\Command;
1012
use Symfony\Component\DependencyInjection\ContainerInterface;
1113

1214
abstract class KernelTestCase extends TestCase
@@ -23,6 +25,16 @@ protected function tearDown() : void
2325
$this->context->shutdown();
2426
}
2527

28+
protected function addCommand(Application $application, Command $command) : void
29+
{
30+
/** @phpstan-ignore function.alreadyNarrowedType */
31+
if (\method_exists($application, 'addCommand')) {
32+
$application->addCommand($command);
33+
} else {
34+
$application->add($command);
35+
}
36+
}
37+
2638
/**
2739
* @param array{config?: callable(TestKernel): void} $options
2840
*/

src/bridge/symfony/telemetry-bundle/tests/Flow/Bridge/Symfony/TelemetryBundle/Tests/Unit/Instrumentation/Doctrine/DBAL/TracingConnectionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ public function rowCount() : int
328328
};
329329
}
330330

331-
public function quote(string $value) : string
331+
/** @phpstan-ignore missingType.parameter, missingType.parameter */
332+
public function quote($value, $type = ParameterType::STRING) : string
332333
{
333334
return "'{$value}'";
334335
}

src/core/etl/src/Flow/ETL/Config/Telemetry/TelemetryContext.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,16 @@ public function dataFrameCompleted(FlowContext $context, array $attributes = [])
6969
return;
7070
}
7171

72-
$this->logger()->debug('Data frame processing completed', [
73-
'dataframe_id' => $context->config->id(),
74-
'total_rows_processed' => $this->totalRowsProcessed,
75-
'memory_min_mb' => $this->memory->min()->inMb(),
76-
'memory_max_mb' => $this->memory->max()->inMb(),
77-
]);
72+
$this->logger()->debug(
73+
'Data frame processing completed',
74+
[
75+
'dataframe_id' => $context->config->id(),
76+
'total_rows_processed' => $this->totalRowsProcessed,
77+
'memory_min_mb' => $this->memory->min()->inMb(),
78+
'memory_max_mb' => $this->memory->max()->inMb(),
79+
],
80+
spanContext: $this->dataFrameSpan->context()
81+
);
7882

7983
$throughput = 0.0;
8084

@@ -118,18 +122,22 @@ public function dataFrameStarted(FlowContext $context) : void
118122
{
119123
$this->dataFrameSpan = $this->tracer->span(DataFrame::class);
120124

121-
$this->logger()->debug('Data frame processing started', [
122-
'dataframe_id' => $context->config->id(),
123-
'cache' => $context->cache()::class,
124-
'serializer' => $context->config->serializer()::class,
125-
'optimizers' => \array_map(static fn (Optimization $optimization) => $optimization::class, $context->config->optimizer()->optimizations()),
126-
'telemetry' => [
127-
'trace_loading' => $this->options->traceLoading,
128-
'trace_transformations' => $this->options->traceTransformations,
129-
'collect_metrics' => $this->options->collectMetrics,
125+
$this->logger()->debug(
126+
'Data frame processing started',
127+
[
128+
'dataframe_id' => $context->config->id(),
129+
'cache' => $context->cache()::class,
130+
'serializer' => $context->config->serializer()::class,
131+
'optimizers' => \array_map(static fn (Optimization $optimization) => $optimization::class, $context->config->optimizer()->optimizations()),
132+
'telemetry' => [
133+
'trace_loading' => $this->options->traceLoading,
134+
'trace_transformations' => $this->options->traceTransformations,
135+
'collect_metrics' => $this->options->collectMetrics,
136+
],
137+
'fstab' => \array_map(static fn (Filesystem $filesystem) => $filesystem::class, $context->config->fstab()->filesystems()),
130138
],
131-
'fstab' => \array_map(static fn (Filesystem $filesystem) => $filesystem::class, $context->config->fstab()->filesystems()),
132-
]);
139+
spanContext: $this->dataFrameSpan->context()
140+
);
133141

134142
if ($this->options->collectMetrics) {
135143
$this->counterProcessedRows = $this->meter->createCounter('rows.processed.total', 'Rows Processed');

src/lib/telemetry/src/Flow/Telemetry/Logger/Logger.php

Lines changed: 124 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,27 @@ public function __construct(
4949
*
5050
* @param string $body The log message
5151
* @param Attributes|TAttributeValueMap $attributes Optional attributes
52+
* @param null|\DateTimeImmutable $timestamp When the event occurred (defaults to current time)
53+
* @param null|\DateTimeImmutable $observedTimestamp When the log was observed by collection
54+
* @param null|SpanContext $spanContext Span context for trace correlation (defaults to current active span)
5255
*/
53-
public function debug(string $body, array|Attributes $attributes = []) : void
54-
{
55-
$this->emit(new LogRecord(Severity::DEBUG, $body, $attributes instanceof Attributes ? $attributes : Attributes::create($attributes)));
56+
public function debug(
57+
string $body,
58+
array|Attributes $attributes = [],
59+
?\DateTimeImmutable $timestamp = null,
60+
?\DateTimeImmutable $observedTimestamp = null,
61+
?SpanContext $spanContext = null,
62+
) : void {
63+
$this->emit(
64+
new LogRecord(
65+
Severity::DEBUG,
66+
$body,
67+
$attributes instanceof Attributes ? $attributes : Attributes::create($attributes),
68+
$timestamp,
69+
$observedTimestamp,
70+
),
71+
$spanContext,
72+
);
5673
}
5774

5875
/**
@@ -62,15 +79,16 @@ public function debug(string $body, array|Attributes $attributes = []) : void
6279
* such as setting a custom timestamp or recording an exception.
6380
*
6481
* @param LogRecord $record The log record to emit
82+
* @param null|SpanContext $spanContext Span context for trace correlation (defaults to current active span)
6583
*/
66-
public function emit(LogRecord $record) : void
84+
public function emit(LogRecord $record, ?SpanContext $spanContext = null) : void
6785
{
6886
$entry = new LogEntry(
6987
$record,
7088
$this->resource,
7189
$this->scope,
7290
$record->timestamp ?? $this->clock->now(),
73-
$this->resolveSpanContext(),
91+
$spanContext ?? $this->resolveSpanContext(),
7492
);
7593

7694
$this->processor->process($entry);
@@ -84,10 +102,27 @@ public function emit(LogRecord $record) : void
84102
*
85103
* @param string $body The log message
86104
* @param Attributes|TAttributeValueMap $attributes Optional attributes
105+
* @param null|\DateTimeImmutable $timestamp When the event occurred (defaults to current time)
106+
* @param null|\DateTimeImmutable $observedTimestamp When the log was observed by collection
107+
* @param null|SpanContext $spanContext Span context for trace correlation (defaults to current active span)
87108
*/
88-
public function error(string $body, array|Attributes $attributes = []) : void
89-
{
90-
$this->emit(new LogRecord(Severity::ERROR, $body, $attributes instanceof Attributes ? $attributes : Attributes::create($attributes)));
109+
public function error(
110+
string $body,
111+
array|Attributes $attributes = [],
112+
?\DateTimeImmutable $timestamp = null,
113+
?\DateTimeImmutable $observedTimestamp = null,
114+
?SpanContext $spanContext = null,
115+
) : void {
116+
$this->emit(
117+
new LogRecord(
118+
Severity::ERROR,
119+
$body,
120+
$attributes instanceof Attributes ? $attributes : Attributes::create($attributes),
121+
$timestamp,
122+
$observedTimestamp,
123+
),
124+
$spanContext,
125+
);
91126
}
92127

93128
/**
@@ -98,10 +133,27 @@ public function error(string $body, array|Attributes $attributes = []) : void
98133
*
99134
* @param string $body The log message
100135
* @param Attributes|TAttributeValueMap $attributes Optional attributes
136+
* @param null|\DateTimeImmutable $timestamp When the event occurred (defaults to current time)
137+
* @param null|\DateTimeImmutable $observedTimestamp When the log was observed by collection
138+
* @param null|SpanContext $spanContext Span context for trace correlation (defaults to current active span)
101139
*/
102-
public function fatal(string $body, array|Attributes $attributes = []) : void
103-
{
104-
$this->emit(new LogRecord(Severity::FATAL, $body, $attributes instanceof Attributes ? $attributes : Attributes::create($attributes)));
140+
public function fatal(
141+
string $body,
142+
array|Attributes $attributes = [],
143+
?\DateTimeImmutable $timestamp = null,
144+
?\DateTimeImmutable $observedTimestamp = null,
145+
?SpanContext $spanContext = null,
146+
) : void {
147+
$this->emit(
148+
new LogRecord(
149+
Severity::FATAL,
150+
$body,
151+
$attributes instanceof Attributes ? $attributes : Attributes::create($attributes),
152+
$timestamp,
153+
$observedTimestamp,
154+
),
155+
$spanContext,
156+
);
105157
}
106158

107159
/**
@@ -120,10 +172,27 @@ public function flush() : bool
120172
*
121173
* @param string $body The log message
122174
* @param Attributes|TAttributeValueMap $attributes Optional attributes
175+
* @param null|\DateTimeImmutable $timestamp When the event occurred (defaults to current time)
176+
* @param null|\DateTimeImmutable $observedTimestamp When the log was observed by collection
177+
* @param null|SpanContext $spanContext Span context for trace correlation (defaults to current active span)
123178
*/
124-
public function info(string $body, array|Attributes $attributes = []) : void
125-
{
126-
$this->emit(new LogRecord(Severity::INFO, $body, $attributes instanceof Attributes ? $attributes : Attributes::create($attributes)));
179+
public function info(
180+
string $body,
181+
array|Attributes $attributes = [],
182+
?\DateTimeImmutable $timestamp = null,
183+
?\DateTimeImmutable $observedTimestamp = null,
184+
?SpanContext $spanContext = null,
185+
) : void {
186+
$this->emit(
187+
new LogRecord(
188+
Severity::INFO,
189+
$body,
190+
$attributes instanceof Attributes ? $attributes : Attributes::create($attributes),
191+
$timestamp,
192+
$observedTimestamp,
193+
),
194+
$spanContext,
195+
);
127196
}
128197

129198
/**
@@ -150,10 +219,27 @@ public function processor() : LogProcessor
150219
*
151220
* @param string $body The log message
152221
* @param Attributes|TAttributeValueMap $attributes Optional attributes
222+
* @param null|\DateTimeImmutable $timestamp When the event occurred (defaults to current time)
223+
* @param null|\DateTimeImmutable $observedTimestamp When the log was observed by collection
224+
* @param null|SpanContext $spanContext Span context for trace correlation (defaults to current active span)
153225
*/
154-
public function trace(string $body, array|Attributes $attributes = []) : void
155-
{
156-
$this->emit(new LogRecord(Severity::TRACE, $body, $attributes instanceof Attributes ? $attributes : Attributes::create($attributes)));
226+
public function trace(
227+
string $body,
228+
array|Attributes $attributes = [],
229+
?\DateTimeImmutable $timestamp = null,
230+
?\DateTimeImmutable $observedTimestamp = null,
231+
?SpanContext $spanContext = null,
232+
) : void {
233+
$this->emit(
234+
new LogRecord(
235+
Severity::TRACE,
236+
$body,
237+
$attributes instanceof Attributes ? $attributes : Attributes::create($attributes),
238+
$timestamp,
239+
$observedTimestamp,
240+
),
241+
$spanContext,
242+
);
157243
}
158244

159245
/**
@@ -163,11 +249,28 @@ public function trace(string $body, array|Attributes $attributes = []) : void
163249
* the application from functioning.
164250
*
165251
* @param string $body The log message
166-
* @param array<string, array<bool|float|int|string>|bool|float|int|string>|Attributes $attributes Optional attributes
252+
* @param Attributes|TAttributeValueMap $attributes Optional attributes
253+
* @param null|\DateTimeImmutable $timestamp When the event occurred (defaults to current time)
254+
* @param null|\DateTimeImmutable $observedTimestamp When the log was observed by collection
255+
* @param null|SpanContext $spanContext Span context for trace correlation (defaults to current active span)
167256
*/
168-
public function warn(string $body, array|Attributes $attributes = []) : void
169-
{
170-
$this->emit(new LogRecord(Severity::WARN, $body, $attributes instanceof Attributes ? $attributes : Attributes::create($attributes)));
257+
public function warn(
258+
string $body,
259+
array|Attributes $attributes = [],
260+
?\DateTimeImmutable $timestamp = null,
261+
?\DateTimeImmutable $observedTimestamp = null,
262+
?SpanContext $spanContext = null,
263+
) : void {
264+
$this->emit(
265+
new LogRecord(
266+
Severity::WARN,
267+
$body,
268+
$attributes instanceof Attributes ? $attributes : Attributes::create($attributes),
269+
$timestamp,
270+
$observedTimestamp,
271+
),
272+
$spanContext,
273+
);
171274
}
172275

173276
/**

0 commit comments

Comments
 (0)