<?php

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Http\Middleware;

use ChaseConey\LaravelDatadogHelper\Middleware\LaravelDatadogMiddleware;
use Datadog;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class DatadogMetrics extends LaravelDatadogMiddleware
{
    /**
     * Logs the duration of a specific request through the application
     *
     * @param Request $request
     * @param Response $response
     * @param float $startTime
     */
    protected static function logDuration(Request $request, Response $response, $startTime)
    {
        $duration = microtime(true) - $startTime;
        $tags = [
            'action' => 'error_page',
            'api' => $request->is('api/*') ? 'true' : 'false',
            'controller' => 'error',
            'namespace' => 'error',
            'section' => 'error',
            'status_code' => $response->getStatusCode(),
            'status_code_extra' => $request->attributes->get('status_code_extra'),
        ];

        $tags = array_merge($tags, app('route-section')->getCurrent());

        Datadog::timing(config('datadog-helper.prefix_web').'.request_time', $duration, 1, $tags);
    }
}

<?php

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Http\Middleware;

use ChaseConey\LaravelDatadogHelper\Middleware\LaravelDatadogMiddleware;
use Datadog;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class DatadogMetrics extends LaravelDatadogMiddleware
{
    /**
     * Logs the duration of a specific request through the application
     *
     * @param Request $request
     * @param Response $response
     * @param float $startTime
     */
    protected static function logDuration(Request $request, Response $response, $startTime)
    {
        static $hostname;
        if (!isset($hostname)) {
            $hostname = gethostname();
            if (!is_string($hostname)) {
                $hostname = 'unknown';
            }
        }

        $duration = microtime(true) - $startTime;
        $tags = [
            'action' => 'error_page',
            'api' => $request->is('api/*') ? 'true' : 'false',
            'controller' => 'error',
            'namespace' => 'error',
            'pod_name' => $hostname,
            'section' => 'error',
            'status_code' => $response->getStatusCode(),
            'status_code_extra' => $request->attributes->get('status_code_extra'),
        ];

        $tags = array_merge($tags, app('route-section')->getCurrent());

        Datadog::timing(config('datadog-helper.prefix_web').'.request_time', $duration, 1, $tags);
    }
}

Add actual hostname tag to datadog
Add actual hostname tag to datadog

The datadog daemon may run on different host so the host tag will be
incorrect (mainly on k8s deployment).
