Skip to content

Commit

Permalink
Fixed style
Browse files Browse the repository at this point in the history
  • Loading branch information
intuibase committed Oct 31, 2024
1 parent 4baa15e commit 6b2a1af
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 31 deletions.
19 changes: 15 additions & 4 deletions src/Instrumentation/Curl/src/CurlHandleMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace OpenTelemetry\Contrib\Instrumentation\Curl;

use OpenTelemetry\SemConv\TraceAttributes;
use CurlHandle;
use OpenTelemetry\SemConv\TraceAttributes;

class CurlHandleMetadata
{
Expand Down Expand Up @@ -43,7 +43,8 @@ public function setAttribute(string $key, mixed $value)

public function setHeaderToPropagate(string $key, $value): CurlHandleMetadata
{
$this->headersToPropagate[] = $key . ": " . $value;
$this->headersToPropagate[] = $key . ': ' . $value;

return $this;
}

Expand All @@ -54,6 +55,7 @@ public function getRequestHeadersToSend(): ?array
}
$headers = array_merge($this->headersToPropagate, $this->headers);
$this->headersToPropagate = [];

return $headers;
}

Expand All @@ -69,8 +71,8 @@ public function getResponseHeaderCaptureFunction()

$header = trim($headerLine, "\n\r");
if (strlen($header) > 0) {
if (strpos($header, ": ") !== false) {
list($key, $value) = explode(": ", $header, 2);
if (strpos($header, ': ') !== false) {
list($key, $value) = explode(': ', $header, 2);
$this->responseHeaders[strtolower($key)] = $value;
}
}
Expand All @@ -90,36 +92,45 @@ public function updateFromCurlOption(int $option, mixed $value)
switch ($option) {
case CURLOPT_CUSTOMREQUEST:
$this->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, $value);

break;
case CURLOPT_HTTPGET:
// Based on https://github.com/curl/curl/blob/curl-7_73_0/lib/setopt.c#L841
$this->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, 'GET');

break;
case CURLOPT_POST:
$this->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, ($value == 1 ? 'POST' : 'GET'));

break;
case CURLOPT_POSTFIELDS:
// Based on https://github.com/curl/curl/blob/curl-7_73_0/lib/setopt.c#L269
$this->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, 'POST');

break;
case CURLOPT_PUT:
$this->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, ($value == 1 ? 'PUT' : 'GET'));

break;
case CURLOPT_NOBODY:
// Based on https://github.com/curl/curl/blob/curl-7_73_0/lib/setopt.c#L269
$this->setAttribute(TraceAttributes::HTTP_REQUEST_METHOD, ($value == 1 ? 'HEAD' : 'GET'));

break;
case CURLOPT_URL:
// $this->setAttribute(TraceAttributes::URL_FULL, self::redactUrlString($value));
break;
case CURLOPT_USERAGENT:
$this->setAttribute(TraceAttributes::USER_AGENT_ORIGINAL, $value);

break;
case CURLOPT_HTTPHEADER:
$this->headers = $value;

break;
case CURLOPT_HEADERFUNCTION:
$this->originalHeaderFunction = $value;
// no break
case CURLOPT_VERBOSE:
$this->verboseEnabled = false;
}
Expand Down
22 changes: 12 additions & 10 deletions src/Instrumentation/Curl/src/CurlInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\SDK\Common\Configuration\Configuration;
use OpenTelemetry\Context\Context;
use function OpenTelemetry\Instrumentation\hook;
use OpenTelemetry\SDK\Common\Configuration\Configuration;
use OpenTelemetry\SemConv\TraceAttributes;
use Phan\Daemon\Transport\CapturerResponder;
use WeakMap;
use WeakReference;


class CurlInstrumentation
{
public const NAME = 'curl';
Expand Down Expand Up @@ -167,9 +165,9 @@ public static function register(): void
if (self::isRequestHeadersCapturingEnabled()) {
if (!$curlHandleToAttributes[$params[0]]->isVerboseEnabled()) { // we let go of captuing request headers because CURLINFO_HEADER_OUT is disabling CURLOPT_VERBOSE
curl_setopt($params[0], CURLINFO_HEADER_OUT, true);
} else {
//TODO log?
}
//TODO log?
}
$curlSetOptInstrumentationSuppressed = false;

Expand Down Expand Up @@ -297,9 +295,9 @@ public static function register(): void
if (self::isRequestHeadersCapturingEnabled()) {
if (!$curlHandleToAttributes[$cHandle]->isVerboseEnabled()) { // we let go of captuing request headers because CURLINFO_HEADER_OUT is disabling CURLOPT_VERBOSE
curl_setopt($cHandle, CURLINFO_HEADER_OUT, true);
} else {
//TODO log?
}
//TODO log?
}
$curlSetOptInstrumentationSuppressed = false;

Expand Down Expand Up @@ -403,13 +401,16 @@ private static function transformHeaderStringToArray(string $header): array
$headersResult = [];
foreach ($lines as $line) {
$line = trim($line, "\r");
if (empty($line)) continue;
if (empty($line)) {
continue;
}

if (strpos($line, ": ") !== false) {
list($key, $value) = explode(": ", $line, 2);
if (strpos($line, ': ') !== false) {
list($key, $value) = explode(': ', $line, 2);
$headersResult[strtolower($key)] = $value;
}
}

return $headersResult;
}

Expand Down Expand Up @@ -455,6 +456,7 @@ private static function isRequestHeadersCapturingEnabled(): bool
if (class_exists('OpenTelemetry\SDK\Common\Configuration\Configuration') && count(Configuration::getList('OTEL_PHP_INSTRUMENTATION_HTTP_REQUEST_HEADERS', [])) > 0) {
return true;
}

return get_cfg_var('otel.instrumentation.http.request_headers') !== false;
}

Expand Down
1 change: 0 additions & 1 deletion src/Instrumentation/Curl/src/HeadersPropagator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use function assert;
use OpenTelemetry\Context\Propagation\PropagationSetterInterface;
use CurlHandle;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace OpenTelemetry\Tests\Instrumentation\Curl\Integration;

use ArrayObject;
use LanguageServerProtocol\PublishDiagnosticsClientCapabilities;
use OpenTelemetry\API\Instrumentation\Configurator;
use OpenTelemetry\Context\ScopeInterface;
use OpenTelemetry\SDK\Trace\ImmutableSpan;
Expand Down Expand Up @@ -132,8 +131,8 @@ public function test_curl_exec_calls_user_defined_headerfunc(): void
{
// test if response header capturing is not breaking user header func invocation

putenv("OTEL_PHP_INSTRUMENTATION_HTTP_RESPONSE_HEADERS=server");
putenv("OTEL_PHP_INSTRUMENTATION_HTTP_REQUEST_HEADERS=host");
putenv('OTEL_PHP_INSTRUMENTATION_HTTP_RESPONSE_HEADERS=server');
putenv('OTEL_PHP_INSTRUMENTATION_HTTP_REQUEST_HEADERS=host');

$ch = curl_init('http://example.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Expand Down Expand Up @@ -163,8 +162,8 @@ public function test_curl_exec_calls_user_defined_headerfunc(): void

public function test_curl_exec_headers_captuing(): void
{
putenv("OTEL_PHP_INSTRUMENTATION_HTTP_RESPONSE_HEADERS=content-type");
putenv("OTEL_PHP_INSTRUMENTATION_HTTP_REQUEST_HEADERS=host");
putenv('OTEL_PHP_INSTRUMENTATION_HTTP_RESPONSE_HEADERS=content-type');
putenv('OTEL_PHP_INSTRUMENTATION_HTTP_REQUEST_HEADERS=host');

$ch = curl_init('http://example.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Expand All @@ -176,13 +175,13 @@ public function test_curl_exec_headers_captuing(): void
$this->assertSame('GET', $span->getName());
$this->assertEquals(200, $span->getAttributes()->get(TraceAttributes::HTTP_RESPONSE_STATUS_CODE));
$this->assertEqualsIgnoringCase('http', $span->getAttributes()->get(TraceAttributes::URL_SCHEME));
$this->assertStringContainsStringIgnoringCase("text/html", $span->getAttributes()->get('http.response.header.content-type'));
$this->assertEquals("example.com", $span->getAttributes()->get('http.request.header.host'));
$this->assertStringContainsStringIgnoringCase('text/html', $span->getAttributes()->get('http.response.header.content-type'));
$this->assertEquals('example.com', $span->getAttributes()->get('http.request.header.host'));
}

public function test_curl_exec_sets_traceparent(): void
{
putenv("OTEL_PHP_INSTRUMENTATION_HTTP_REQUEST_HEADERS=traceparent");
putenv('OTEL_PHP_INSTRUMENTATION_HTTP_REQUEST_HEADERS=traceparent');

$ch = curl_init('http://example.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,13 @@ public function test_curl_multi_remove_handle()

public function test_curl_multi_exec_calls_user_defined_headerfunc(): void
{
putenv("OTEL_PHP_INSTRUMENTATION_HTTP_RESPONSE_HEADERS=content-type");
putenv("OTEL_PHP_INSTRUMENTATION_HTTP_REQUEST_HEADERS=host");
putenv('OTEL_PHP_INSTRUMENTATION_HTTP_RESPONSE_HEADERS=content-type');
putenv('OTEL_PHP_INSTRUMENTATION_HTTP_REQUEST_HEADERS=host');

$mh = curl_multi_init();
$ch1 = curl_init('http://example.com/');
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);


$func = function (\CurlHandle $ch, string $headerLine) {
return strlen($headerLine);
};
Expand Down Expand Up @@ -175,8 +174,8 @@ public function test_curl_multi_exec_calls_user_defined_headerfunc(): void

public function test_curl_multi_exec_headers_captuing(): void
{
putenv("OTEL_PHP_INSTRUMENTATION_HTTP_RESPONSE_HEADERS=content-type");
putenv("OTEL_PHP_INSTRUMENTATION_HTTP_REQUEST_HEADERS=host");
putenv('OTEL_PHP_INSTRUMENTATION_HTTP_RESPONSE_HEADERS=content-type');
putenv('OTEL_PHP_INSTRUMENTATION_HTTP_REQUEST_HEADERS=host');

$mh = curl_multi_init();
$ch1 = curl_init('http://example.com/');
Expand Down Expand Up @@ -206,14 +205,14 @@ public function test_curl_multi_exec_headers_captuing(): void
$this->assertEquals(200, $span->getAttributes()->get(TraceAttributes::HTTP_RESPONSE_STATUS_CODE));
$this->assertEqualsIgnoringCase('http', $span->getAttributes()->get(TraceAttributes::URL_SCHEME));
$this->assertEquals(80, $span->getAttributes()->get(TraceAttributes::SERVER_PORT));
$this->assertStringContainsStringIgnoringCase("text/html", $span->getAttributes()->get('http.response.header.content-type'));
$this->assertEquals("example.com", $span->getAttributes()->get('http.request.header.host'));
$this->assertStringContainsStringIgnoringCase('text/html', $span->getAttributes()->get('http.response.header.content-type'));
$this->assertEquals('example.com', $span->getAttributes()->get('http.request.header.host'));
}
}

public function test_curl_multi_exec_sets_traceparent(): void
{
putenv("OTEL_PHP_INSTRUMENTATION_HTTP_REQUEST_HEADERS=traceparent");
putenv('OTEL_PHP_INSTRUMENTATION_HTTP_REQUEST_HEADERS=traceparent');

$mh = curl_multi_init();
$ch1 = curl_init('http://example.com/');
Expand Down

0 comments on commit 6b2a1af

Please sign in to comment.