Skip to content

Commit

Permalink
Pulled it out
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryangr0 committed Oct 12, 2024
1 parent 3ff6f89 commit 47478a6
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 252 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,54 @@
- Composer


# How to use

- Monolog
### Serviceprovider
```php
TelemetryServiceProvider::class
```

### Using TelemetryService to log
```php
use \Webgrip\TelemetryService\Core\Domain\Services\TelemetryServiceInterface;

class Foo
{
public function __construct(private TelemetryServiceInterface $telemetryService) {
}

public function bar()
{
$this->telemetryService->logger()->debug('Hello World');
$this->telemetryService->logger()->info('Hello World');
$this->telemetryService->logger()->warning('Hello World');
// ...
}
}
```


### Using TelemetryService to trace
```php
use \Webgrip\TelemetryService\Core\Domain\Services\TelemetryServiceInterface;

class Foo
{
public function __construct(private TelemetryServiceInterface $telemetryService) {
}

public function bar()
{
$tracer = $this->telemetryService->tracer();
$span = $tracer->spanBuilder('foo')->startSpan();

$span->addEvent('bar');
$span->setAttributes(['foo' => 'bar']);
$span->recordException(new \Exception('Hello World'));
$span->addLink('foo', 'bar');
// ...

$span->end();
}
}
```
38 changes: 0 additions & 38 deletions src/Core/Application/Application.php

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

namespace Webgrip\TelemetryService\Core\Application\Factories;

use OpenTelemetry\SDK\Resource\ResourceInfo;
use Monolog\Logger;
use Psr\Container\ContainerInterface;
use Webgrip\TelemetryService\Infrastructure\Services\TelemetryService;

interface TelemetryServiceFactoryInterface
{
public function create(ResourceInfo $resourceInfo): TelemetryService;
/**
* @param ContainerInterface $configuration
* @param Logger $logger
* @return TelemetryService
*/
public function create(ContainerInterface $configuration, Logger $logger): TelemetryService;
}
13 changes: 0 additions & 13 deletions src/Core/Application/Telemetry/OpenTelemetryCollectorInterface.php

This file was deleted.

78 changes: 78 additions & 0 deletions src/Core/Application/TelemetryServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Webgrip\TelemetryService\Core\Application;

use DI\Container;
use GuzzleHttp\Client;
use OpenTelemetry\SDK\Common\Attribute\Attributes;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use OpenTelemetry\SemConv\ResourceAttributes;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Webgrip\TelemetryService\Core\Application\Factories\LoggerProviderFactoryInterface;
use Webgrip\TelemetryService\Core\Application\Factories\TelemetryServiceFactoryInterface;
use Webgrip\TelemetryService\Core\Application\Factories\TracerProviderFactoryInterface;
use Webgrip\TelemetryService\Core\Domain\Services\TelemetryServiceInterface;
use Webgrip\TelemetryService\Infrastructure\Factories\LoggerProviderFactory;
use Webgrip\TelemetryService\Infrastructure\Factories\TelemetryServiceFactory;
use Webgrip\TelemetryService\Infrastructure\Factories\TracerProviderFactory;

readonly class TelemetryServiceProvider
{
/**
* @param ContainerInterface $configuration
*/
public function __construct(
private ContainerInterface $configuration
)
{
}

/**
* @param Container $container
* @return void
*/
public function register(Container $container): void
{
$configuration = $this->configuration;

$container->set(ResourceInfo::class, function (Container $container) use ($configuration) {
return ResourceInfo::create(
Attributes::create([
ResourceAttributes::DEPLOYMENT_ENVIRONMENT_NAME => $configuration->get('applicationEnvironmentName'),
ResourceAttributes::SERVICE_NAMESPACE => $configuration->get('applicationNamespace'),
ResourceAttributes::SERVICE_NAME => $configuration->get('applicationName'),
ResourceAttributes::SERVICE_VERSION => $configuration->get('applicationVersion'),
])
);
});

$container->set(LoggerProviderFactoryInterface::class, function (Container $container) use ($configuration) {
return new LoggerProviderFactory(
$configuration
);
});

$container->set(TracerProviderFactoryInterface::class, function (Container $container) use ($configuration) {
return new TracerProviderFactory(
$configuration
);
});

$container->set(TelemetryServiceFactoryInterface::class, function (Container $container) {
return new TelemetryServiceFactory(
$container->get(LoggerProviderFactoryInterface::class),
$container->get(TracerProviderFactoryInterface::class),
$container->get(Client::class)
);
});

$container->set(TelemetryServiceInterface::class, function (Container $container) use ($configuration) {
return $container->get(TelemetryServiceFactoryInterface::class)->create(
$configuration,
$container->get(LoggerInterface::class)
);
});
}

}
2 changes: 2 additions & 0 deletions src/Core/Domain/Services/TelemetryServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Webgrip\TelemetryService\Core\Domain\Services;

use OpenTelemetry\API\Trace\TracerInterface;
use Psr\Log\LoggerInterface;

interface TelemetryServiceInterface
{
public function logger(): LoggerInterface;
public function tracer(): TracerInterface;
}
28 changes: 0 additions & 28 deletions src/Infrastructure/Configuration/Configuration.php

This file was deleted.

15 changes: 12 additions & 3 deletions src/Infrastructure/Factories/LoggerProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,30 @@
use OpenTelemetry\SDK\Logs\LoggerProvider;
use OpenTelemetry\SDK\Logs\Processor\SimpleLogRecordProcessor;
use OpenTelemetry\SDK\Resource\ResourceInfo;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Webgrip\TelemetryService\Core\Application\Factories\LoggerProviderFactoryInterface;
use Webgrip\TelemetryService\Infrastructure\Configuration\Configuration;

final class LoggerProviderFactory implements LoggerProviderFactoryInterface
{
/**
* @param ContainerInterface $configuration
*/
public function __construct(
private readonly Configuration $configuration
private readonly ContainerInterface $configuration
)
{
}

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function create(ResourceInfo $resourceInfo): LoggerProviderInterface
{
$transport = (new OtlpHttpTransportFactory())->create(
'http://' . $this->configuration->otelCollectorHost . ':4318' . '/v1/logs',
'http://' . $this->configuration->get('otelCollectorHost') . ':4318' . '/v1/logs',
'application/json'
);

Expand Down
45 changes: 0 additions & 45 deletions src/Infrastructure/Factories/OpenTelemetryCollectorFactory.php

This file was deleted.

Loading

0 comments on commit 47478a6

Please sign in to comment.