Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter manager #1

Merged
merged 13 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: PHP Composer

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.2"

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run test suite
run: composer test

- name: Style check
run: composer php-cs-fixer-check

- name: Document facade
run: composer facade-documentor-lint

- name: Static Analysis
run: composer static-analysis
117 changes: 76 additions & 41 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,78 @@
{
"name": "cosmastech/laravel-statsd-adapter",
"description": "Easily use statsd-client-adapter within your Laravel project",
"license": "wtfpl",
"authors": [
{
"name": "Luke Kuzmish",
"email": "luke@kuzmish.com",
"role": "Developer"
"name": "cosmastech/laravel-statsd-adapter",
"description": "Easily use statsd-client-adapter within your Laravel project",
"license": "wtfpl",
"authors": [
{
"name": "Luke Kuzmish",
"email": "luke@kuzmish.com",
"role": "Developer"
}
],
"autoload": {
"psr-4": {
"Cosmastech\\LaravelStatsDAdapter\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Cosmastech\\LaravelStatsDAdapter\\Tests\\": "tests",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
}
},
"require": {
"php": "^8.2",
"cosmastech/statsd-client-adapter": "^0.0.2",
"illuminate/support": "^10.0|^11.0",
"illuminate/contracts": "^10.0|^11.0"
},
"require-dev": {
"phpunit/phpunit": "^11.2.5",
"friendsofphp/php-cs-fixer": "^3.59",
"league/statsd": "^2.0.0",
"orchestra/testbench": "^9.1",
"laravel/facade-documenter": "dev-main",
"phpstan/phpstan": "^1.11"
},
"extra": {
"laravel": {
"providers": [
"Cosmastech\\LaravelStatsDAdapter\\StatsDAdapterServiceProvider"
]
}
},
"scripts": {
"test": "phpunit tests",
"php-cs-fixer": "./vendor/bin/php-cs-fixer fix ./",
"php-cs-fixer-check": "./vendor/bin/php-cs-fixer check ./",
"post-autoload-dump": [
"@clear",
"@prepare"
],
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": "@php vendor/bin/testbench workbench:build --ansi",
"serve": [
"Composer\\Config::disableProcessTimeout",
"@build",
"@php vendor/bin/testbench serve"
],
"static-analysis": [
"@php vendor/bin/phpstan analyse -c phpstan.neon"
],
"facade-documentor": [
"@php -f vendor/bin/facade.php -- \"\\\\Cosmastech\\\\LaravelStatsDAdapter\\\\Stats\""
],
"facade-documentor-lint": [
"@php -f vendor/bin/facade.php -- --lint \"\\\\Cosmastech\\\\LaravelStatsDAdapter\\\\Stats\""
]
},
"repositories": {
"facade-documenter": {
"type": "vcs",
"url": "git@github.com:laravel/facade-documenter.git"
}
}
],
"autoload": {
"psr-4": {
"Cosmastech\\LaravelStatsDAdapter\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Cosmastech\\LaravelStatsDAdapter\\Tests\\": "tests"
}
},
"require": {
"php": "^8.2",
"cosmastech/statsd-client-adapter": "^0.0.1",
"illuminate/support": "^10.0|^11.0"
},
"require-dev": {
"phpunit/phpunit": "^11.2.5",
"friendsofphp/php-cs-fixer": "^3.59"
},
"extra": {
"laravel": {
"providers": [
"Cosmastech\\LaravelStatsDAdapter\\LaravelStatsDAdapterServiceProvider"
]
}
},
"scripts": {
"test": "phpunit tests",
"php-cs-fixer": "./vendor/bin/php-cs-fixer fix ./",
"php-cs-fixer-check": "./vendor/bin/php-cs-fixer check ./"
}
}
}
45 changes: 45 additions & 0 deletions config/statsd-adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

return [
'default' => env("STATSD_ADAPTER_DEFAULT", "memory"),

/**
* You may name your channel anything you wish. Valid drivers are:
* memory
* league
* datadog
* log_datadog
*/
"channels" => [
"memory" => [
"adapter" => "memory",
],
"league" => [
"adapter" => "league",
"instance_id" => null,
'host' => env('STATSD_HOST', '127.0.0.1'),
'port' => env('STATSD_PORT', 8125),
'namespace' => env('STATSD_NAMESPACE', ''),
'throwConnectionExceptions' => true,
],
"datadog" => [
"adapter" => "datadog",
"host" => env("DD_AGENT_HOST"),
"port" => env("DD_DOGSTATSD_PORT"),
"socket_path" => null,
"datadog_host" => null,
"decimal_precision" => null,
"global_tags" => [],
"metric_prefix" => null,
"disable_telemetry" => null,
],
"log_datadog" => [
"adapter" => "log_datadog",
"log_level" => "debug",
"decimal_precision" => null,
"global_tags" => [],
"metric_prefix" => null,
"disable_telemetry" => null,
],
],
];
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 7
paths:
- src
- tests
122 changes: 122 additions & 0 deletions src/AdapterManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter;

use Carbon\FactoryImmutable;
use Carbon\WrapperClock;
use Cosmastech\StatsDClientAdapter\Adapters\Datadog\DatadogStatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\InMemory\InMemoryClientAdapter;
use Cosmastech\StatsDClientAdapter\Adapters\League\LeagueStatsDClientAdapter;
use Cosmastech\StatsDClientAdapter\Clients\Datadog\DatadogLoggingClient;
use Cosmastech\StatsDClientAdapter\Utility\SampleRateDecider\SampleRateSendDecider;
use DataDog\DogStatsd;
use Illuminate\Support\MultipleInstanceManager;
use League\StatsD\Client;
use League\StatsD\Exception\ConfigurationException;

/**
* @mixin \Cosmastech\StatsDClientAdapter\Adapters\StatsDClientAdapter
*/
class AdapterManager extends MultipleInstanceManager
{
protected $driverKey = 'adapter';

protected string $defaultInstanceName;

/**
* The configuration repository instance.
* @todo remove this after laravel/framework release 2024-07-06
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;

/**
* @inheritDoc
*/
public function __construct($app)
{
// @todo remove this after laravel/framework release 2024-07-06
parent::__construct($app);
$this->config = $this->app->get('config');
}

/**
* @inheritDoc
*/
public function getDefaultInstance()
{
return $this->defaultInstanceName ?? $this->config->get("statsd-adapter.default");
}

/**
* @inheritDoc
*/
public function setDefaultInstance($name)
{
$this->defaultInstanceName = $name;
}

/**
* @return array<string, mixed>|null
*/
public function getInstanceConfig($name)
{
return $this->config->get("statsd-adapter.channels.{$name}");
}

/**
* @param array<string, mixed> $config
* @return InMemoryClientAdapter
*/
protected function createMemoryAdapter(array $config): InMemoryClientAdapter
{
$wrapperClock = new WrapperClock(FactoryImmutable::getDefaultInstance());

return new InMemoryClientAdapter($wrapperClock);
}

/**
* @param array<string, mixed> $config
* @return DatadogStatsDClientAdapter
*/
protected function createLog_datadogAdapter(array $config): DatadogStatsDClientAdapter
{
return $this->createLogDatadogAdapter($config);
}

/**
* @param array<string, mixed> $config
* @return DatadogStatsDClientAdapter
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
protected function createLogDatadogAdapter(array $config): DatadogStatsDClientAdapter
{
$logLevel = $config['log_level'] ?? 'debug';

return new DatadogStatsDClientAdapter(
new DatadogLoggingClient($this->app->make('log'), $logLevel, $config)
);
}

/**
* @param array<string, mixed> $config
* @return DatadogStatsDClientAdapter
*/
protected function createDatadogAdapter(array $config): DatadogStatsDClientAdapter
{
return new DatadogStatsDClientAdapter(new DogStatsd($config));
}

/**
* @param array<string, mixed> $config
* @return LeagueStatsDClientAdapter
* @throws ConfigurationException
*/
protected function createLeagueAdapter(array $config): LeagueStatsDClientAdapter
{
$leagueClient = new Client($config['instance_id'] ?? null);
$leagueClient->configure($config);

return new LeagueStatsDClientAdapter($leagueClient, new SampleRateSendDecider());
}
}
33 changes: 33 additions & 0 deletions src/Stats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Cosmastech\LaravelStatsDAdapter;

use Illuminate\Support\Facades\Facade;

/**
* @method static void getDefaultInstance()
* @method static void setDefaultInstance(string $name)
* @method static array|null getInstanceConfig(string $name)
* @method static mixed instance(string|null $name = null)
* @method static \Cosmastech\LaravelStatsDAdapter\AdapterManager forgetInstance(array|string|null $name = null)
* @method static void purge(string|null $name = null)
* @method static \Cosmastech\LaravelStatsDAdapter\AdapterManager extend(string $name, \Closure $callback)
* @method static void timing(string $stat, float $durationMs, float $sampleRate = 1, array $tags = [])
* @method static void gauge(string $stat, float $value, float $sampleRate = 1, array $tags = [])
* @method static void histogram(string $stat, float $value, float $sampleRate = 1, array $tags = [])
* @method static void distribution(string $stat, float $value, float $sampleRate = 1, array $tags = [])
* @method static void set(string $stat, string|float $value, float $sampleRate = 1, array $tags = [])
* @method static void increment(array|string $stats, float $sampleRate = 1, array $tags = [], int $value = 1)
* @method static void decrement(array|string $stats, float $sampleRate = 1, array $tags = [], int $value = 1)
* @method static void updateStats(array|string $stats, int $delta = 1, void $sampleRate = 1, void $tags = null)
* @method static mixed getClient()
*
* @see \Cosmastech\LaravelStatsDAdapter\AdapterManager
*/
class Stats extends Facade
{
protected static function getFacadeAccessor(): string
{
return AdapterManager::class;
}
}
Loading
Loading