Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: feat: add trace handling #114

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/Builder/DefaultBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ trait DefaultBuilderTrait
/** @var ProcessorInterface<mixed>|null */
private ProcessorInterface|null $processor;

/**
* @var \Closure(): string
*/
private \Closure $traceGenerator;

public function setLogger(LoggerInterface|null $logger): void
{
$this->logger = $logger;
Expand Down Expand Up @@ -210,17 +215,39 @@ private function convertToMultipartItems(string $key, array|string|\Stringable|i

public function generate(): GotenbergFileResult
{
$this->logger?->debug('Processing file using {sensiolabs_gotenberg.builder} builder.', [
$this->traceGenerator ??= $this::defaultTraceGenerator(...);
$trace = ($this->traceGenerator)();
$headers = ['Gotenberg-Trace' => $trace];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trace should be optional to allow keeping default behavior (generated UUID).

We can make the trace nullable and call it only if set. If so, we should add an "unset" method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder. Wouldn't it be better to always set this ourselves ?


$this->logger?->debug('Processing file with trace "{sensiolabs_gotenberg.trace}" using {sensiolabs_gotenberg.builder} builder.', [
'sensiolabs_gotenberg.trace' => $trace,
'sensiolabs_gotenberg.builder' => $this::class,
]);

$processor = $this->processor ?? new NullProcessor();

return new GotenbergFileResult(
$this->client->call($this->getEndpoint(), $this->getMultipartFormData()),
$this->client->call($this->getEndpoint(), $this->getMultipartFormData(), $headers),
$processor($this->fileName),
$this->headerDisposition,
$this->fileName,
);
}

/**
* Sets the callable that will generate the trace ID for each operation.
*
* @param \Closure(): string $traceGenerator
*/
public function traceGenerator(\Closure $traceGenerator): static
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function traceGenerator(\Closure $traceGenerator): static
public function trace(?\Closure $traceGenerator = null): static

If set, use the \Closure, if not, use a fn(): string => bin2hex(random_bytes(16)).microtime(true);.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+ function noTrace(): static to remove this behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the value. There will be a trace no matter what. Either explicitly set from our component or automaticcaly create by gotenberg. But for webhooks we do need this feature. Withotu trace, webhook won't be possible. Trace act as a token per request.

Also I think noTrace is misleading. There will be a trace. just not an epxlicit one. So I think null is not needed or act as what you intended for noTrace ==> fallback to default behaviour

{
$this->traceGenerator = $traceGenerator;

return $this;
}

protected static function defaultTraceGenerator(): string
{
return bin2hex(random_bytes(16)).microtime(true);
}
}
5 changes: 5 additions & 0 deletions src/Builder/GotenbergFileResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public function getContentLength(): int|null
return $this->response->getContentLength();
}

public function getTrace(): string
{
return $this->response->getHeaders()->get('Gotenberg-Trace', '');
}

public function process(): mixed
{
if (!$this->response->getStream()->valid()) {
Expand Down
Loading