generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from 090809/feat-injecting-current-span-to-outg…
…oing-requests feat(http-client/injecting): adding 'withTrace' macro for Http Client
- Loading branch information
Showing
8 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Spatie\OpenTelemetry\Http\Client; | ||
|
||
use Illuminate\Http\Client\PendingRequest; | ||
use Spatie\OpenTelemetry\Facades\Measure as MeasureFacade; | ||
use Spatie\OpenTelemetry\Support\Injectors\TextInjector; | ||
|
||
class Macro | ||
{ | ||
public static function apply() { | ||
PendingRequest::macro('withTrace', function () { | ||
$headers = []; | ||
|
||
if ($span = MeasureFacade::currentSpan()) { | ||
$headers['traceparent'] = ""; | ||
|
||
TextInjector::Inject($headers['traceparent'], $span); | ||
} | ||
|
||
return PendingRequest::withHeaders($headers); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Spatie\OpenTelemetry\Support\Injectors; | ||
|
||
use Spatie\OpenTelemetry\Support\Span; | ||
|
||
class TextInjector | ||
{ | ||
static public function Inject(string &$data, Span $span): void | ||
{ | ||
$data = sprintf( | ||
'%s-%s-%s-%02x', | ||
'00', | ||
$span->trace()->id(), | ||
$span->id(), | ||
$span->flags(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
use Spatie\OpenTelemetry\Facades\Measure; | ||
use Spatie\OpenTelemetry\Support\Injectors\TextInjector; | ||
|
||
it('injects current span context name to Laravel HTTP (outgoing) requests', function () { | ||
Http::fake(); | ||
|
||
$parentSpan = Measure::start('parent'); | ||
|
||
$parentSpanTraceContextID = ""; | ||
TextInjector::Inject($parentSpanTraceContextID, $parentSpan); | ||
|
||
Http::withTrace()->post('http://example.com/first'); | ||
|
||
Http::assertSent(function (\Illuminate\Http\Client\Request $request) use ($parentSpanTraceContextID) { | ||
return $request | ||
->hasHeader('traceparent', $parentSpanTraceContextID); | ||
}); | ||
|
||
$childSpan = Measure::start('second'); | ||
$childSpanTraceContextID = ""; | ||
TextInjector::Inject($childSpanTraceContextID, $childSpan); | ||
|
||
Http::withTrace()->post('http://example.com/second'); | ||
|
||
Http::assertSent(function (\Illuminate\Http\Client\Request $request) use ($childSpanTraceContextID) { | ||
return $request | ||
->hasHeader('traceparent', $childSpanTraceContextID); | ||
}); | ||
|
||
Measure::stop('second'); | ||
|
||
Http::withTrace()->post('http://example.com/third'); | ||
|
||
Http::assertSent(function (\Illuminate\Http\Client\Request $request) use ($parentSpanTraceContextID) { | ||
return $request | ||
->hasHeader('traceparent', $parentSpanTraceContextID); | ||
}); | ||
|
||
Measure::stop('first'); | ||
}); | ||
|
||
it('wil not fall, if no spans present', function () { | ||
Http::fake(); | ||
|
||
Http::withTrace()->post('http://example.com/first'); | ||
|
||
Http::assertSent(function (\Illuminate\Http\Client\Request $request) { | ||
return true; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters