Skip to content

Commit

Permalink
Merge pull request #20 from keepsuit/support-logs-and-metrics
Browse files Browse the repository at this point in the history
Add support for logs
  • Loading branch information
cappuc committed Apr 8, 2024
2 parents c72a9a0 + fa5cbf0 commit c598b62
Show file tree
Hide file tree
Showing 16 changed files with 524 additions and 175 deletions.
178 changes: 120 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@
_OpenTelemetry is a collection of tools, APIs, and SDKs. Use it to instrument, generate, collect, and export telemetry data (metrics, logs, and traces) to help you analyze your software’s performance and behavior._

This package allow to integrate OpenTelemetry in a Laravel application.
Right now only tracing is available.

## Provided tracing integrations

- [Http server requests](#http-server-requests)
- [Http client](#http-client)
- [Database](#database)
- [Redis](#redis)
- [Queue jobs](#redis)
- [Logs context](#logs-context)
- [Manual traces](#manual-traces)

## Installation

Expand All @@ -37,6 +26,8 @@ php artisan vendor:publish --provider="Keepsuit\LaravelOpentelemetry\LaravelOpen
This is the contents of the published config file:

```php
<?php

use Keepsuit\LaravelOpenTelemetry\Instrumentation;

return [
Expand All @@ -46,39 +37,92 @@ return [
'service_name' => env('OTEL_SERVICE_NAME', \Illuminate\Support\Str::slug(env('APP_NAME', 'laravel-app'))),

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

/**
* OpenTelemetry Traces configuration
*/
'sampler' => [
'traces' => [
/**
* Wraps the sampler in a parent based sampler
* Traces exporter
* This should be the key of one of the exporters defined in the exporters section
*/
'parent' => env('OTEL_TRACES_SAMPLER_PARENT', true),
'exporter' => env('OTEL_TRACES_EXPORTER', 'otlp'),

/**
* Sampler type
* Supported values: "always_on", "always_off", "traceidratio"
* Traces sampler
*/
'type' => env('OTEL_TRACES_SAMPLER_TYPE', 'always_on'),
'sampler' => [
/**
* Wraps the sampler in a parent based sampler
*/
'parent' => env('OTEL_TRACES_SAMPLER_PARENT', true),

'args' => [
/**
* Sampling ratio for traceidratio sampler
* Sampler type
* Supported values: "always_on", "always_off", "traceidratio"
*/
'ratio' => env('OTEL_TRACES_SAMPLER_TRACEIDRATIO_RATIO', 0.05),
'type' => env('OTEL_TRACES_SAMPLER_TYPE', 'always_on'),

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

/**
* Traces exporter
* Supported: "zipkin", "http", "grpc", "console", "null"
* OpenTelemetry logs configuration
*/
'exporter' => env('OTEL_TRACES_EXPORTER', 'http'),
'logs' => [
/**
* Logs exporter
* This should be the key of one of the exporters defined in the exporters section
* Supported drivers: "otlp", "console", "null"
*/
'exporter' => env('OTEL_LOGS_EXPORTER', 'otlp'),

/**
* Inject active trace id in log context
*
* When using the OpenTelemetry logger, the trace id is always injected in the exported log record.
* This option allows to inject the trace id in the log context for other loggers.
*/
'inject_trace_id' => true,

/**
* Context field name for trace id
*/
'trace_id_field' => 'traceid',
],

/**
* Comma separated list of propagators to use.
* Supports any otel propagator, for example: "tracecontext", "baggage", "b3", "b3multi", "none"
* OpenTelemetry exporters
*
* Here you can configure exports used by traces and logs.
* If you want to use the same protocol with different endpoints,
* you can copy the exporter with a different and change the endpoint
*
* Supported drivers: "otlp", "zipkin", "console", "null"
*/
'propagators' => env('OTEL_PROPAGATORS', 'tracecontext'),
'exporters' => [
'otlp' => [
'driver' => 'otlp',
'endpoint' => env('OTEL_EXPORTER_OTLP_ENDPOINT', 'http://localhost:4318'),
// Supported: "grpc", "http/protobuf", "http/json"
'protocol' => env('OTEL_EXPORTER_OTLP_PROTOCOL', 'http/protobuf'),
],

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

/**
* List of instrumentation used for application tracing
Expand Down Expand Up @@ -110,41 +154,23 @@ return [
'ignored' => [],
],
],
];
```

/**
* Exporters config
*/
'exporters' => [
'otlp' => [
'endpoint' => env('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', env('OTEL_EXPORTER_OTLP_ENDPOINT', 'http://localhost:4318')),

// Supported: "grpc", "http/protobuf", "http/json"
'protocol' => env('OTEL_EXPORTER_OTLP_TRACES_PROTOCOL', env('OTEL_EXPORTER_OTLP_PROTOCOL', 'http/protobuf')),
],
## Traces

'zipkin' => [
'endpoint' => env('OTEL_EXPORTER_ZIPKIN_ENDPOINT', 'http://localhost:9411'),
],
],
This package provides a set of integrations to automatically trace common operations in a Laravel application.
You can disable or customize each integration in the config file in the `instrumentations` section.

/**
* Logs context config
*/
'logs' => [
/**
* Inject active trace id in log context
*/
'inject_trace_id' => true,
### Provided tracing integrations

/**
* Context field name for trace id
*/
'trace_id_field' => 'traceId',
],
];
```

## Usage
- [Http server requests](#http-server-requests)
- [Http client](#http-client)
- [Database](#database)
- [Redis](#redis)
- [Queue jobs](#redis)
- [Logs context](#logs-context)
- [Manual traces](#manual-traces)

### Http server requests

Expand Down Expand Up @@ -196,6 +222,10 @@ This allows to correlate logs with traces.
If you are starting the root trace manually,
you should call `Tracer::updateLogContext()` to inject the trace id in the log context.

> [!NOTE]
> When using the OpenTelemetry logs driver (`otlp`),
> the trace id is automatically injected in the log context without the need to call `Tracer::updateLogContext()`.
### Manual traces

Spans can be manually created with the `newSpan` method on the `Tracer` facade.
Expand Down Expand Up @@ -251,6 +281,38 @@ Tracer::propagationHeaders(); // get the propagation headers required to propaga
Tracer::extractContextFromPropagationHeaders(array $headers); // extract the trace context from propagation headers
```

## Logs

This package provides a custom log channel that allows to process logs with OpenTelemetry instrumentation.
This packages injects a log channel named `otlp` that can be used to send logs to OpenTelemetry using laravel default log system.

```php
// config/logging.php
'channels' => [
// injected channel config, you can override it adding an `otlp` channel in your config
'otlp' => [
'driver' => 'monolog',
'handler' => \Keepsuit\LaravelOpenTelemetry\Support\OpenTelemetryMonologHandler::class,
'level' => 'debug',
]
]
```

As an alternative, you can use the `Logger` facade to send logs directly to OpenTelemetry:

```php
use Keepsuit\LaravelOpenTelemetry\Facades\Logger;

Logger::emergency('my log message');
Logger::alert('my log message');
Logger::critical('my log message');
Logger::error('my log message');
Logger::warning('my log message');
Logger::notice('my log message');
Logger::info('my log message');
Logger::debug('my log message');
```

## Testing

```bash
Expand Down
116 changes: 69 additions & 47 deletions config/opentelemetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,92 @@
'service_name' => env('OTEL_SERVICE_NAME', \Illuminate\Support\Str::slug(env('APP_NAME', 'laravel-app'))),

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

/**
* OpenTelemetry Traces configuration
*/
'sampler' => [
'traces' => [
/**
* Wraps the sampler in a parent based sampler
* Traces exporter
* This should be the key of one of the exporters defined in the exporters section
*/
'parent' => env('OTEL_TRACES_SAMPLER_PARENT', true),
'exporter' => env('OTEL_TRACES_EXPORTER', 'otlp'),

/**
* Sampler type
* Supported values: "always_on", "always_off", "traceidratio"
* Traces sampler
*/
'type' => env('OTEL_TRACES_SAMPLER_TYPE', 'always_on'),
'sampler' => [
/**
* Wraps the sampler in a parent based sampler
*/
'parent' => env('OTEL_TRACES_SAMPLER_PARENT', true),

'args' => [
/**
* Sampling ratio for traceidratio sampler
* Sampler type
* Supported values: "always_on", "always_off", "traceidratio"
*/
'ratio' => env('OTEL_TRACES_SAMPLER_TRACEIDRATIO_RATIO', 0.05),
'type' => env('OTEL_TRACES_SAMPLER_TYPE', 'always_on'),

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

/**
* Traces exporter
* Supported: "zipkin", "http", "grpc", "console", "null"
* OpenTelemetry logs configuration
*/
'exporter' => env('OTEL_TRACES_EXPORTER', 'http'),
'logs' => [
/**
* Logs exporter
* This should be the key of one of the exporters defined in the exporters section
* Supported drivers: "otlp", "console", "null"
*/
'exporter' => env('OTEL_LOGS_EXPORTER', 'otlp'),

/**
* Inject active trace id in log context
*
* When using the OpenTelemetry logger, the trace id is always injected in the exported log record.
* This option allows to inject the trace id in the log context for other loggers.
*/
'inject_trace_id' => true,

/**
* Context field name for trace id
*/
'trace_id_field' => 'traceid',
],

/**
* Comma separated list of propagators to use.
* Supports any otel propagator, for example: "tracecontext", "baggage", "b3", "b3multi", "none"
* OpenTelemetry exporters
*
* Here you can configure exports used by traces and logs.
* If you want to use the same protocol with different endpoints,
* you can copy the exporter with a different and change the endpoint
*
* Supported drivers: "otlp", "zipkin", "console", "null"
*/
'propagators' => env('OTEL_PROPAGATORS', 'tracecontext'),
'exporters' => [
'otlp' => [
'driver' => 'otlp',
'endpoint' => env('OTEL_EXPORTER_OTLP_ENDPOINT', 'http://localhost:4318'),
// Supported: "grpc", "http/protobuf", "http/json"
'protocol' => env('OTEL_EXPORTER_OTLP_PROTOCOL', 'http/protobuf'),
],

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

/**
* List of instrumentation used for application tracing
Expand Down Expand Up @@ -73,35 +126,4 @@
'ignored' => [],
],
],

/**
* Exporters config
*/
'exporters' => [
'otlp' => [
'endpoint' => env('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', env('OTEL_EXPORTER_OTLP_ENDPOINT', 'http://localhost:4318')),

// Supported: "grpc", "http/protobuf", "http/json"
'protocol' => env('OTEL_EXPORTER_OTLP_TRACES_PROTOCOL', env('OTEL_EXPORTER_OTLP_PROTOCOL', 'http/protobuf')),
],

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

/**
* Logs context config
*/
'logs' => [
/**
* Inject active trace id in log context
*/
'inject_trace_id' => true,

/**
* Context field name for trace id
*/
'trace_id_field' => 'traceId',
],
];
Loading

0 comments on commit c598b62

Please sign in to comment.