Skip to content

Commit

Permalink
ADD: Initial commit (v1)
Browse files Browse the repository at this point in the history
  • Loading branch information
poespas committed Oct 26, 2024
0 parents commit 3de2e59
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 0 deletions.
Binary file added .github/images/grafana-tempo-screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/images/jaeger-screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Jonathan Visser

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
76 changes: 76 additions & 0 deletions Profiler/Driver/TracingDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Poespas\OpentelemetryTracing\Profiler\Driver;

use Magento\Framework\Profiler\DriverInterface;
use Jaeger\Config;
use Jaeger\Tracer;
use OpenTracing\GlobalTracer;
use const Jaeger\SAMPLER_TYPE_CONST;

class TracingDriver implements DriverInterface
{
const DEFAULT_APPLICATION_NAME = 'magento2';

public Tracer $tracer;

public array $scopes = [];

public function __construct(array $config = null)
{
$appName = $config['application_name'] ?? self::DEFAULT_APPLICATION_NAME;

$config = new Config(
[
'sampler' => [
'type' => SAMPLER_TYPE_CONST,
'param' => true,
],

...$config['config'],
'tags' => [
'http.method' => $_SERVER['REQUEST_METHOD'],
'http.host' => $_SERVER['HTTP_HOST'],
'http.path' => $_SERVER['REQUEST_URI'],
'http.user_agent' => $_SERVER['HTTP_USER_AGENT'],
...($config['config']['tags'] ?? []),
]
],
$appName
);

$config->initializeTracer();

$this->tracer = GlobalTracer::get();

register_shutdown_function([$this, 'finalize']);
}

public function start($timerId, array $attributes = null)
{
if (!is_array($attributes)) {
$attributes = [];
}

$this->scopes[$timerId] = $this->tracer->startActiveSpan($timerId);
$this->scopes[$timerId]->getSpan()->log($attributes);
}

public function stop($timerId)
{
$this->scopes[$timerId]->close();
}

public function clear($timerId = null)
{
if ($timerId) {
unset($this->scopes[$timerId]);
} else {
$this->scopes = [];
}
}

public function finalize() {
$this->tracer->flush();
}
}
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# OpenTelemetry Tracing for Magento 2

## Overview

**OpenTelemetry Tracing** is a Magento 2 module that integrates seamlessly with Magento's built-in Profiling functionality to provide advanced tracing capabilities. By adding a custom tracing driver, this module enables detailed performance monitoring and analysis, helping developers optimize their Magento applications effectively and for free.

## Features

- **Enhanced Profiling:** Extends Magento's native profiler with advanced tracing capabilities.
- **Jaeger Compatibility:** Integrates with Jaeger for distributed tracing using the [jonahgeorge/jaeger-client-php](https://github.com/jonahgeorge/jaeger-client-php) library.
- **Grafana Tempo Support:** Compatible with Grafana Tempo through the OpenTelemetry endpoint, allowing for comprehensive monitoring and visualization.
- **Flexible Configuration:** Easily configure tracing settings via JSON files or Magento CLI commands.

## Compatibility

### Jaeger

OpenTelemetry Tracing integrates with [Jaeger](https://www.jaegertracing.io/), a popular open-source distributed tracing system. This integration is powered by the [jonahgeorge/jaeger-client-php](https://github.com/jonahgeorge/jaeger-client-php) library, ensuring reliable and efficient tracing capabilities.

### Grafana Tempo

The module also supports [Grafana Tempo](https://grafana.com/oss/tempo/), enabling users to leverage Grafana's powerful visualization tools through the OpenTelemetry endpoint. This compatibility allows for seamless tracing data collection and analysis within the Grafana ecosystem.

## Installation

1. **Clone the Repository:**

```bash
composer require poespas/opentelemetry-tracing-magento2
```

2. **Enable the Module:**

```bash
bin/magento module:enable Poespas_OpentelemetryTracing
bin/magento setup:upgrade
```

## Configuration

You can configure the tracing driver using a JSON configuration file or via the Magento CLI command.

### Using `var/profiler.flag` File

Create or update the `var/profiler.flag` file in the root of the magento installation with your JSON configuration:

```
{
"drivers": [
{
"type": "\\Poespas\\OpentelemetryTracing\\Profiler\\Driver\\TracingDriver",
"application_name": "my-magento-app",
"config": {
"local_agent": {
"reporting_host": "127.0.0.1",
"reporting_port": 14268
},
"dispatch_mode": "jaeger_over_binary_http"
}
}
]
}
```

### Using Magento CLI Command

Alternatively, enable the profiler with a minified JSON configuration using the following command:

```
bin/magento dev:profiler:enable '{"drivers":[{"type":"\\Poespas\\OpentelemetryTracing\\Profiler\\Driver\\TracingDriver","application_name":"my-magento-app","config":{"local_agent":{"reporting_host":"127.0.0.1","reporting_port":14268},"dispatch_mode":"jaeger_over_binary_http"}}]}'
```

## Screenshots

### Jaeger Integration

![Jaeger Dashboard](.github/images/jaeger-screenshot.png)

*Example of tracing data visualized in Jaeger.*

### Grafana Tempo Integration

![Grafana Tempo Dashboard](.github/images/grafana-tempo-screenshot.png)

*Example of tracing data visualized in Grafana Tempo.*

## Inspiration

OpenTelemetry Tracing was inspired by the [sitewards/Magento2-OpenCensus](https://github.com/sitewards/Magento2-OpenCensus) project.

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

## Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

## Support

For support and questions, please open an issue on the [GitHub repository](https://github.com/poespas/opentelemetry-tracing-magento2/issues).
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "poespas/opentelemetry-tracing-magento2",
"description": "Magento 2 OpenTelemetry Tracing",
"version": "1.0.0",
"license": "MIT",
"authors": [
{
"name": "Jonathan Visser",
"homepage": "https://poespas.me"
}
],
"require": {
"php": "^8.1 || ^8.2 || ^8.3",
"jonahgeorge/jaeger-client-php":"^1.5"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Poespas\\OpentelemetryTracing\\": ""
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
7 changes: 7 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<config
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"
>
<module name="Poespas_OpentelemetryTracing" />
</config>
9 changes: 9 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Poespas_OpentelemetryTracing',
__DIR__
);

0 comments on commit 3de2e59

Please sign in to comment.