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

DDBTEAM-657: Added request id to messages #33

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
33 changes: 33 additions & 0 deletions .docker/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
user nginx;
worker_processes 1;

error_log /dev/stderr warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

# Detect if a client provides a X-Request-ID header (proxy before this), and pass it on to the backend
# server. If no such header is provided, it can provide a random value.
map $http_x_request_id $reqid {
default $http_x_request_id;
"" $request_id;
}

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$reqid"';

access_log /dev/stdout main;

sendfile on;
keepalive_timeout 65;
gzip on;

include /etc/nginx/conf.d/*.conf;
}
9 changes: 1 addition & 8 deletions .docker/vhost.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ server {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;

# optionally set the value of the environment variables used in the application
# fastcgi_param APP_ENV prod;
# fastcgi_param APP_SECRET <app-secret-id>;
# fastcgi_param DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name";

# When you are using symlinks to link the document root to the
# current version of your application, you should pass the real
# application path instead of the path to the symlink to PHP
Expand All @@ -28,6 +23,7 @@ server {
# for more information).
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param HTTP_X_REQUEST_ID $reqid;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/index.php/some-path
# Remove the internal directive to allow URIs like this
Expand All @@ -39,7 +35,4 @@ server {
location ~ \.php$ {
return 404;
}

error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
19 changes: 0 additions & 19 deletions config/packages/dev/monolog.yaml

This file was deleted.

10 changes: 10 additions & 0 deletions config/packages/monolog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:

monolog:
channels: ["information"]
handlers:
information:
type: stream
path: "php://stdout"
formatter: monolog.formatter.json
channels: ["information"]
18 changes: 0 additions & 18 deletions config/packages/prod/monolog.yaml

This file was deleted.

8 changes: 8 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ services:
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$bindCoverStoreRemoteURL: '%env(COVERSTORE_REMOTE_URL)%'
$bindTraceId: '%env(traceId:HTTP_X_REQUEST_ID)%'

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

monolog.formatter.json:
class: Monolog\Formatter\JsonFormatter

App\Logger\TraceIdProcessor:
tags:
- { name: monolog.processor }

# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ services:
- '80'
volumes:
- ${PWD}/.docker/vhost.conf:/etc/nginx/conf.d/default.conf:ro
- ${PWD}/.docker/nginx.conf:/etc/nginx/nginx.conf:ro
- ./:/app:delegated
labels:
- "traefik.enable=true"
Expand Down
53 changes: 53 additions & 0 deletions src/EnvVarProcessor/TraceIdEnvVarProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* @file
* EnvProcessor to generate trace id.
*/

namespace App\EnvVarProcessor;

use Symfony\Component\DependencyInjection\EnvVarProcessorInterface;
use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;

class TraceIdEnvVarProcessor implements EnvVarProcessorInterface
{
private static $id;

/**
* {@inheritdoc}
*/
public function getEnv($prefix, $name, $getEnv)
{
try {
$this::$id = $getEnv($name);
} catch (EnvNotFoundException $exception) {
// Do not do anything here as the id will fallback to be generated.
}

if (empty($this::$id)) {
$this->generate();
}

return $this::$id;
}

/**
* {@inheritdoc}
*/
public static function getProvidedTypes()
{
return [
'traceId' => 'string',
];
}

/**
* Generate new unique id.
*
* @throws \Exception
*/
private function generate() {
$this::$id = bin2hex(random_bytes(16));
}
}
6 changes: 5 additions & 1 deletion src/EventSubscriber/MaterialPostWriteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ final class MaterialPostWriteSubscriber implements EventSubscriberInterface
{
private $bus;
private $storage;
private $requestId;

/**
* MaterialPostWriteSubscriber constructor.
*
* @param string $bindRequestId
* @param MessageBusInterface $bus
* @param StorageInterface $storage
*/
public function __construct(MessageBusInterface $bus, StorageInterface $storage)
public function __construct(string $bindRequestId, MessageBusInterface $bus, StorageInterface $storage)
{
$this->requestId = $bindRequestId;
$this->bus = $bus;
$this->storage = $storage;
}
Expand Down Expand Up @@ -79,6 +82,7 @@ public function materialPostWrite(ViewEvent $event)
$message->setOperation(VendorState::INSERT);
$message->setImageUrl($url);
$message->setAccrediting($material->getAgencyId());
$message->setRequestId($this->requestId);
$this->bus->dispatch($message);
break;
}
Expand Down
6 changes: 5 additions & 1 deletion src/EventSubscriber/MaterialPreWriteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ final class MaterialPreWriteSubscriber implements EventSubscriberInterface
{
private $bus;
private $storage;
private $requestId;

/** @var User */
private $user;

/**
* MaterialPreWriteSubscriber constructor.
*
* @param string $bindRequestId
* @param MessageBusInterface $bus
* @param StorageInterface $storage
* @param Security $security
*/
public function __construct(MessageBusInterface $bus, StorageInterface $storage, Security $security)
public function __construct(string $bindRequestId, MessageBusInterface $bus, StorageInterface $storage, Security $security)
{
$this->requestId = $bindRequestId;
$this->bus = $bus;
$this->storage = $storage;

Expand Down Expand Up @@ -79,6 +82,7 @@ public function materialPreWrite(ViewEvent $event)
$message->setIdentifierType($item->getIsType());
$message->setIdentifier($item->getIsIdentifier());
$message->setOperation(VendorState::DELETE);
$message->setRequestId($this->requestId);

$this->bus->dispatch($message);
break;
Expand Down
42 changes: 42 additions & 0 deletions src/Logger/TraceIdProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @file
* Logger processor adding trace id to log requests
*/

namespace App\Logger;

/**
* Class TraceIdProcessor.
*/
class TraceIdProcessor
{
private $traceId;

/**
* TraceIdProcessor constructor.
*
* @param string $bindTraceId
*/
public function __construct(string $bindTraceId)
{
$this->traceId = $bindTraceId;
}

/**
* Magic invoke function.
*
* @param array $record
* Log record
*
* @return array
* The record added require id in extras
*/
public function __invoke(array $record)
{
$record['extra']['traceId'] = $this->traceId;

return $record;
}
}
27 changes: 27 additions & 0 deletions src/Message/CoverUserUploadMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CoverUserUploadMessage
private $imageUrl;
private $accrediting;
private $vendorId;
private $requestId;

/**
* @return string
Expand Down Expand Up @@ -140,4 +141,30 @@ public function setVendorId($vendorId): self

return $this;
}

/**
* Get request id (which is unique for the whole request).
*
* @return string
* The request id
*/
public function getRequestId()
{
return $this->requestId;
}

/**
* Set request id (which is unique for the whole request).
*
* @param string $requestId
* The request id (normally found in HTTP_X_REQUEST_ID)
*
* @return CoverUserUploadMessage
*/
public function setRequestId(string $requestId): self
{
$this->requestId = $requestId;

return $this;
}
}