Skip to content

Commit

Permalink
Merge pull request #17 from keepsuit/env
Browse files Browse the repository at this point in the history
Follow OTEL env variables specifications
  • Loading branch information
cappuc committed Feb 14, 2024
2 parents 04d2159 + a1c35b2 commit d5d932e
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 92 deletions.
67 changes: 43 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,53 +43,70 @@ return [
/**
* Service name
*/
'service_name' => \Illuminate\Support\Str::slug(env('APP_NAME', 'laravel-app')),
'service_name' => env('OTEL_SERVICE_NAME', \Illuminate\Support\Str::slug(env('APP_NAME', 'laravel-app'))),

/**
* Enable tracing
* Valid values: 'true', 'false', 'parent'
* Traces sampler
*/
'enabled' => env('OT_ENABLED', true),
'sampler' => [
/**
* Wraps the sampler in a parent based sampler
*/
'parent' => env('OTEL_TRACES_SAMPLER_PARENT', true),

/**
* Sampler type
* Supported values: "always_on", "always_off", "traceidratio"
*/
'type' => env('OTEL_TRACES_SAMPLER_TYPE', 'always_on'),

'args' => [
/**
* Sampling ratio for traceidratio sampler
*/
'ratio' => env('OTEL_TRACES_SAMPLER_TRACEIDRATIO_RATIO', 0.05),
],
],

/**
* Exporter to use
* Supported: 'zipkin', 'http', 'grpc', 'console', 'null'
* Traces exporter
* Supported: "zipkin", "http", "grpc", "console", "null"
*/
'exporter' => env('OT_EXPORTER', 'http'),
'exporter' => env('OTEL_TRACES_EXPORTER', 'http'),

/**
* Propagator to use
* Supported: 'b3', 'b3multi', 'tracecontext',
* Comma separated list of propagators to use.
* Supports any otel propagator, for example: "tracecontext", "baggage", "b3", "b3multi", "none"
*/
'propagator' => env('OT_PROPAGATOR', 'tracecontext'),
'propagators' => env('OTEL_PROPAGATORS', 'tracecontext'),

/**
* List of instrumentation used for application tracing
*/
'instrumentation' => [
Instrumentation\HttpServerInstrumentation::class => [
'enabled' => env('OT_INSTRUMENTATION_HTTP_SERVER', true),
'enabled' => env('OTEL_INSTRUMENTATION_HTTP_SERVER', true),
'excluded_paths' => [],
'allowed_headers' => [],
'sensitive_headers' => [],
],

Instrumentation\HttpClientInstrumentation::class => [
'enabled' => env('OT_INSTRUMENTATION_HTTP_CLIENT', true),
'enabled' => env('OTEL_INSTRUMENTATION_HTTP_CLIENT', true),
'allowed_headers' => [],
'sensitive_headers' => [],
],

Instrumentation\QueryInstrumentation::class => env('OT_INSTRUMENTATION_QUERY', true),
Instrumentation\QueryInstrumentation::class => env('OTEL_INSTRUMENTATION_QUERY', true),

Instrumentation\RedisInstrumentation::class => env('OT_INSTRUMENTATION_REDIS', true),
Instrumentation\RedisInstrumentation::class => env('OTEL_INSTRUMENTATION_REDIS', true),

Instrumentation\QueueInstrumentation::class => env('OT_INSTRUMENTATION_QUEUE', true),
Instrumentation\QueueInstrumentation::class => env('OTEL_INSTRUMENTATION_QUEUE', true),

Instrumentation\CacheInstrumentation::class => env('OT_INSTRUMENTATION_CACHE', true),
Instrumentation\CacheInstrumentation::class => env('OTEL_INSTRUMENTATION_CACHE', true),

Instrumentation\EventInstrumentation::class => [
'enabled' => env('OT_INSTRUMENTATION_EVENT', true),
'enabled' => env('OTEL_INSTRUMENTATION_EVENT', true),
'ignored' => [],
],
],
Expand All @@ -98,19 +115,21 @@ return [
* Exporters config
*/
'exporters' => [
'zipkin' => [
'endpoint' => env('OT_ZIPKIN_HTTP_ENDPOINT', 'http://localhost:9411'),
],
'otlp' => [
'endpoint' => env('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', env('OTEL_EXPORTER_OTLP_ENDPOINT', 'http://localhost:4318')),

'http' => [
'endpoint' => env('OT_OTLP_HTTP_ENDPOINT', 'http://localhost:4318'),
// Supported: "grpc", "http/protobuf", "http/json"
'protocol' => env('OTEL_EXPORTER_OTLP_TRACES_PROTOCOL', env('OTEL_EXPORTER_OTLP_PROTOCOL', 'http/protobuf')),
],

'grpc' => [
'endpoint' => env('OT_OTLP_GRPC_ENDPOINT', 'http://localhost:4317'),
'zipkin' => [
'endpoint' => env('OTEL_EXPORTER_ZIPKIN_ENDPOINT', 'http://localhost:9411'),
],
],

/**
* Logs context config
*/
'logs' => [
/**
* Inject active trace id in log context
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10.51",
"phpstan/phpstan-deprecation-rules": "^1.1",
"spatie/invade": "^2.0",
"spatie/test-time": "^1.3",
"spatie/valuestore": "^1.3",
"thecodingmachine/phpstan-safe-rule": "^1.2"
Expand Down
67 changes: 43 additions & 24 deletions config/opentelemetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,70 @@
/**
* Service name
*/
'service_name' => \Illuminate\Support\Str::slug(env('APP_NAME', 'laravel-app')),
'service_name' => env('OTEL_SERVICE_NAME', \Illuminate\Support\Str::slug(env('APP_NAME', 'laravel-app'))),

/**
* Enable tracing
* Valid values: 'true', 'false', 'parent'
* Traces sampler
*/
'enabled' => env('OT_ENABLED', true),
'sampler' => [
/**
* Wraps the sampler in a parent based sampler
*/
'parent' => env('OTEL_TRACES_SAMPLER_PARENT', true),

/**
* Sampler type
* Supported values: "always_on", "always_off", "traceidratio"
*/
'type' => env('OTEL_TRACES_SAMPLER_TYPE', 'always_on'),

'args' => [
/**
* Sampling ratio for traceidratio sampler
*/
'ratio' => env('OTEL_TRACES_SAMPLER_TRACEIDRATIO_RATIO', 0.05),
],
],

/**
* Exporter to use
* Supported: 'zipkin', 'http', 'grpc', 'console', 'null'
* Traces exporter
* Supported: "zipkin", "http", "grpc", "console", "null"
*/
'exporter' => env('OT_EXPORTER', 'http'),
'exporter' => env('OTEL_TRACES_EXPORTER', 'http'),

/**
* Propagator to use
* Supported: 'b3', 'b3multi', 'tracecontext',
* Comma separated list of propagators to use.
* Supports any otel propagator, for example: "tracecontext", "baggage", "b3", "b3multi", "none"
*/
'propagator' => env('OT_PROPAGATOR', 'tracecontext'),
'propagators' => env('OTEL_PROPAGATORS', 'tracecontext'),

/**
* List of instrumentation used for application tracing
*/
'instrumentation' => [
Instrumentation\HttpServerInstrumentation::class => [
'enabled' => env('OT_INSTRUMENTATION_HTTP_SERVER', true),
'enabled' => env('OTEL_INSTRUMENTATION_HTTP_SERVER', true),
'excluded_paths' => [],
'allowed_headers' => [],
'sensitive_headers' => [],
],

Instrumentation\HttpClientInstrumentation::class => [
'enabled' => env('OT_INSTRUMENTATION_HTTP_CLIENT', true),
'enabled' => env('OTEL_INSTRUMENTATION_HTTP_CLIENT', true),
'allowed_headers' => [],
'sensitive_headers' => [],
],

Instrumentation\QueryInstrumentation::class => env('OT_INSTRUMENTATION_QUERY', true),
Instrumentation\QueryInstrumentation::class => env('OTEL_INSTRUMENTATION_QUERY', true),

Instrumentation\RedisInstrumentation::class => env('OT_INSTRUMENTATION_REDIS', true),
Instrumentation\RedisInstrumentation::class => env('OTEL_INSTRUMENTATION_REDIS', true),

Instrumentation\QueueInstrumentation::class => env('OT_INSTRUMENTATION_QUEUE', true),
Instrumentation\QueueInstrumentation::class => env('OTEL_INSTRUMENTATION_QUEUE', true),

Instrumentation\CacheInstrumentation::class => env('OT_INSTRUMENTATION_CACHE', true),
Instrumentation\CacheInstrumentation::class => env('OTEL_INSTRUMENTATION_CACHE', true),

Instrumentation\EventInstrumentation::class => [
'enabled' => env('OT_INSTRUMENTATION_EVENT', true),
'enabled' => env('OTEL_INSTRUMENTATION_EVENT', true),
'ignored' => [],
],
],
Expand All @@ -61,19 +78,21 @@
* Exporters config
*/
'exporters' => [
'zipkin' => [
'endpoint' => env('OT_ZIPKIN_HTTP_ENDPOINT', 'http://localhost:9411'),
],
'otlp' => [
'endpoint' => env('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', env('OTEL_EXPORTER_OTLP_ENDPOINT', 'http://localhost:4318')),

'http' => [
'endpoint' => env('OT_OTLP_HTTP_ENDPOINT', 'http://localhost:4318'),
// Supported: "grpc", "http/protobuf", "http/json"
'protocol' => env('OTEL_EXPORTER_OTLP_TRACES_PROTOCOL', env('OTEL_EXPORTER_OTLP_PROTOCOL', 'http/protobuf')),
],

'grpc' => [
'endpoint' => env('OT_OTLP_GRPC_ENDPOINT', 'http://localhost:4317'),
'zipkin' => [
'endpoint' => env('OTEL_EXPORTER_ZIPKIN_ENDPOINT', 'http://localhost:9411'),
],
],

/**
* Logs context config
*/
'logs' => [
/**
* Inject active trace id in log context
Expand Down
1 change: 0 additions & 1 deletion src/Facades/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use OpenTelemetry\Context\ScopeInterface;

/**
* @method static bool isRecording()
* @method static string traceId()
* @method static SpanInterface activeSpan()
* @method static ScopeInterface|null activeScope()
Expand Down
55 changes: 28 additions & 27 deletions src/LaravelOpenTelemetryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
use Illuminate\Support\Str;
use Keepsuit\LaravelOpenTelemetry\Instrumentation\Instrumentation;
use Keepsuit\LaravelOpenTelemetry\Support\CarbonClock;
use Keepsuit\LaravelOpenTelemetry\Support\PropagatorBuilder;
use Keepsuit\LaravelOpenTelemetry\Support\SamplerBuilder;
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;
use OpenTelemetry\API\Signals;
use OpenTelemetry\API\Trace\Propagation\TraceContextPropagator;
use OpenTelemetry\API\Trace\TracerInterface;
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
use OpenTelemetry\Contrib\Grpc\GrpcTransportFactory;
Expand All @@ -18,18 +19,13 @@
use OpenTelemetry\Contrib\Otlp\OtlpUtil;
use OpenTelemetry\Contrib\Otlp\SpanExporter as OtlpSpanExporter;
use OpenTelemetry\Contrib\Zipkin\Exporter as ZipkinExporter;
use OpenTelemetry\Extension\Propagator\B3\B3MultiPropagator;
use OpenTelemetry\Extension\Propagator\B3\B3SinglePropagator;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Common\Configuration\Variables as OTELVariables;
use OpenTelemetry\SDK\Common\Export\Http\PsrTransportFactory;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Sdk;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOffSampler;
use OpenTelemetry\SDK\Trace\Sampler\AlwaysOnSampler;
use OpenTelemetry\SDK\Trace\Sampler\ParentBased;
use OpenTelemetry\SDK\Trace\SpanExporter\ConsoleSpanExporterFactory;
use OpenTelemetry\SDK\Trace\SpanExporter\InMemorySpanExporterFactory;
use OpenTelemetry\SDK\Trace\SpanExporterInterface;
Expand Down Expand Up @@ -73,40 +69,27 @@ protected function initTracer(): void
'application/json'
),
),
'http' => new OtlpSpanExporter(
// @phpstan-ignore-next-line
(new OtlpHttpTransportFactory())->create(
(new HttpEndpointResolver())->resolveToString(config('opentelemetry.exporters.http.endpoint'), Signals::TRACE),
'application/x-protobuf'
)
),
'grpc' => new OtlpSpanExporter(
(new GrpcTransportFactory())->create(config('opentelemetry.exporters.grpc.endpoint').OtlpUtil::method(Signals::TRACE))
),
'otlp' => $this->buildOtlpExporter(),
'console' => (new ConsoleSpanExporterFactory())->create(),
default => (new InMemorySpanExporterFactory())->create(),
};

$spanProcessor = (new BatchSpanProcessorBuilder($spanExporter))
->build();

$sampler = match (config('opentelemetry.enabled', true)) {
'parent' => new ParentBased(new AlwaysOffSampler()),
true => new AlwaysOnSampler(),
default => new AlwaysOffSampler(),
};
$sampler = SamplerBuilder::new()->build(
config('opentelemetry.sampler.type'),
config('opentelemetry.sampler.parent'),
config('opentelemetry.sampler.args', [])
);

$tracerProvider = TracerProvider::builder()
->addSpanProcessor($spanProcessor)
->setResource($resource)
->setSampler($sampler)
->build();

$propagator = match (config('opentelemetry.propagator')) {
'b3' => B3SinglePropagator::getInstance(),
'b3multi' => B3MultiPropagator::getInstance(),
default => TraceContextPropagator::getInstance(),
};
$propagator = PropagatorBuilder::new()->build(config('opentelemetry.propagators'));

Sdk::builder()
->setTracerProvider($tracerProvider)
Expand All @@ -129,7 +112,7 @@ protected function initTracer(): void

protected function registerInstrumentation(): void
{
if (config('opentelemetry.enabled') === false) {
if (Sdk::isDisabled()) {
return;
}

Expand Down Expand Up @@ -159,4 +142,22 @@ private function configureEnvironmentVariables(): void
// Disable debug scopes wrapping
$envRepository->set('OTEL_PHP_DEBUG_SCOPES_DISABLED', '1');
}

protected function buildOtlpExporter(): OtlpSpanExporter
{
$transport = match (config('opentelemetry.exporters.otlp.protocol')) {
'grpc' => (new GrpcTransportFactory())->create(config('opentelemetry.exporters.otlp.endpoint').OtlpUtil::method(Signals::TRACE)),
'http/json', 'json' => (new OtlpHttpTransportFactory())->create(
(new HttpEndpointResolver())->resolveToString(config('opentelemetry.exporters.otlp.endpoint'), Signals::TRACE),
'application/json'
),
default => (new OtlpHttpTransportFactory())->create(
(new HttpEndpointResolver())->resolveToString(config('opentelemetry.exporters.otlp.endpoint'), Signals::TRACE),
'application/x-protobuf'
),
};

// @phpstan-ignore-next-line
return new OtlpSpanExporter($transport);
}
}
Loading

0 comments on commit d5d932e

Please sign in to comment.