Skip to content

Commit

Permalink
adds DatadogLoggingClient
Browse files Browse the repository at this point in the history
Datadog log client
  • Loading branch information
cosmastech authored Jul 4, 2024
2 parents 35f8e2f + 067a230 commit cc50d4e
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 7 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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
13 changes: 13 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2004 Sam Hocevar <[email protected]>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.
18 changes: 11 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"name": "cosmastech/statsd-client",
"description": "A StatsD client adapter",
"license": "WTFPL",
"authors": [
{
"name": "Luke Kuzmish",
Expand All @@ -9,20 +11,21 @@
],
"require": {
"php": "^8.2",
"datadog/php-datadogstatsd": "*",
"psr/clock-implementation": "1.0",
"nesbot/carbon": "^3.6"
"datadog/php-datadogstatsd": "^1.6.1",
"psr/clock-implementation": "1.0"
},
"require-dev": {
"phpunit/phpunit": "^11.2.5",
"nesbot/carbon": "^3.6.0",
"friendsofphp/php-cs-fixer": "^3.59",
"league/statsd": "^2.0.0"
"league/statsd": "^2.0.0",
"cosmastech/psr-logger-spy": "^0.0.1",
"psr/log": "3.0.0"
},
"suggest": {
"datadog/php-datadogstatsd": "For DataDog stats",
"nesbot/carbon": "For using Carbon as psr/clock-implementation",
"league/statsd": "For generic statsd clients"
"league/statsd": "For generic statsd clients",
"monolog/monolog": "For log client"
},
"autoload": {
"psr-4": {
Expand All @@ -36,6 +39,7 @@
},
"scripts": {
"test": "phpunit tests",
"php-cs-fixer": "./vendor/bin/php-cs-fixer fix ./"
"php-cs-fixer": "./vendor/bin/php-cs-fixer fix ./",
"php-cs-fixer-check": "./vendor/bin/php-cs-fixer check ./"
}
}
23 changes: 23 additions & 0 deletions src/Datadog/DatadogLoggingClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Cosmastech\StatsDClient\Datadog;

use DataDog\DogStatsd;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

class DatadogLoggingClient extends DogStatsd
{
public function __construct(
protected readonly LoggerInterface $logger,
protected readonly string $logLevel = LogLevel::DEBUG,
array $datadogConfig = [],
) {
parent::__construct($datadogConfig);
}

public function flush($message)
{
$this->logger->log($this->logLevel, $message);
}
}
52 changes: 52 additions & 0 deletions tests/Datadog/DatadogLoggingClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Cosmastech\StatsDClient\Tests\Datadog;

use Cosmastech\PsrLoggerSpy\LogFactory;
use Cosmastech\PsrLoggerSpy\LoggerSpy;
use Cosmastech\StatsDClient\Datadog\DatadogLoggingClient;
use Cosmastech\StatsDClient\Tests\BaseTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Psr\Log\LogLevel;

class DatadogLoggingClientTest extends BaseTestCase
{
private LoggerSpy $loggerSpy;

public function setUp(): void
{
$this->loggerSpy = new LoggerSpy(new LogFactory());
}

#[Test]
#[DataProvider("logLevelsDataProvider")]
public function respectsLogLevel(string $logLevel)
{
// Given
$datadogLoggingClient = new DatadogLoggingClient($this->loggerSpy, $logLevel);

// When
$datadogLoggingClient->increment("some-stat");

// Then
$logs = $this->loggerSpy->getLogs();

self::assertCount(1, $logs);
self::assertEquals($logLevel, $logs[0]->getLevel()->value);
}

public static function logLevelsDataProvider(): array
{
return [
"debug" => [LogLevel::DEBUG],
"info" => [LogLevel::INFO],
"notice" => [LogLevel::NOTICE],
"warning" => [LogLevel::WARNING],
"error" => [LogLevel::ERROR],
"critical" => [LogLevel::CRITICAL],
"alert" => [LogLevel::ALERT],
"emergency" => [LogLevel::EMERGENCY],
];
}
}

0 comments on commit cc50d4e

Please sign in to comment.