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

Add lumen example app #3

Merged
merged 6 commits into from
Apr 15, 2019
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/examples export-ignore
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ vendor
bin
coverage
coverage.xml
.php_cs.cache
.php_cs.cache
examples/lumen-app/vendor
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ Prometheus is a time-series database with a UI and sophisticated querying langua

This package is a wrapper bridging [thebeatapp/prometheus_client_php](https://github.com/thebeatapp/prometheus_client_php) (a fork of [jimdo/prometheus_client_php](https://github.com/jimdo/prometheus_client_php)) into Lumen and Laravel.

## Example

Head to [examples/lumen-app](https://github.com/thebeatapp/pyr/tree/example-application/examples/lumen-app)
to check out our awesome example application.
To get it you'll have to clone the [Pyr](https://github.com/thebeatapp/pyr/) repo, as the example
is not included when downloaded from composer.

The example is a full project containing it's own `README.md` so you can check the
library's functionality and the way it's intended to be used.


## Installation

Add the repository to composer.json
Expand Down
8 changes: 8 additions & 0 deletions examples/lumen-app/.env.docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
EXAMPLE_APP_NGINX_PORT=8080
EXAMPLE_APP_REDIS_PORT=6479
EXAMPLE_APP_MYSQL_PORT=3326
EXAMPLE_APP_PROMETHEUS_PORT=9290
EXAMPLE_APP_PUSH_GATEWAY_PORT=9291
EXAMPLE_APP_GRAFANA_PORT=3001

TIMES_TO_CALL_EXAMPLE_ROUTE=100
21 changes: 21 additions & 0 deletions examples/lumen-app/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
APP_NAME=Lumen
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost:8080
APP_TIMEZONE=UTC

LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=

DB_CONNECTION=mysql
DB_HOST=my-sql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
QUEUE_CONNECTION=sync

PYR_STORAGE_ADAPTER=apc
5 changes: 5 additions & 0 deletions examples/lumen-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor
/.idea
Homestead.json
Homestead.yaml
.env
83 changes: 83 additions & 0 deletions examples/lumen-app/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
help:
@echo "Please use 'make <target>' where <target> is one of the following:"
@echo " up to create and start project services."
@echo " start to start project services."
@echo " down to stop and remove all project services."
@echo " stop to stop project services."
@echo " rebuild to destroy the example app container and rebuild from Dockerfile."
@echo " provision to provision Grafana datasources and dashboards"
@echo " generate-links to generate the possible services links"
@echo " create-db to create the database."
@echo " create-db to create the database."
@echo " migrate to run db migrations."
@echo " update to perform Composer update."
@echo " call to perform a number of requests to a default test route."

SHELL := /bin/bash
up:
set -a ;\
source .env.docker ;\
docker-compose up -d ;\
make setup-env ;\
make install ;\
make create-db ;\
make migrate ;\
make provision ;\
make generate-links ;

SHELL := /bin/bash
start:
set -a ;\
source .env.docker ;\
docker-compose start
make setup-env
make create-db
make migrate
make generate-links

SHELL := /bin/bash
stop:
set -a ;\
source .env.docker ;\
docker-compose stop

SHELL := /bin/bash
down:
set -a ;\
source .env.docker ;\
docker-compose down

SHELL := /bin/bash
rebuild:
make down
docker rmi -f lumen-example-app_app
set -a ;\
source .env.docker ;\
docker-compose up -d ;\

provision:
docker exec -it lumen-example-app scripts/grafana_provision.sh

generate-links:
docker exec -it lumen-example-app scripts/url_print.sh

create-db:
docker exec lumen-example-app php artisan db:create

setup-env:
docker exec lumen-example-app cp .env.example .env

migrate:
docker exec -it lumen-example-app php artisan migrate

update:
docker exec -it lumen-example-app composer update

install:
docker exec -it lumen-example-app composer install

SHELL := /bin/bash
call:
set -a ;\
source .env.docker ;\
sh scripts/call_test_route.sh ;\
125 changes: 125 additions & 0 deletions examples/lumen-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Test Application
This application was created in order to be able to test various aspects
and functionality of the prometheus exporter project. It may also act as
a dockerized test-bed installation of Lumen.

![test application diagram](docs/example_app.png)

## Installation
In order to run this application you'll need [Docker](https://docs.docker.com/install/)
along with [docker-compose](https://docs.docker.com/compose/install/).
For some functions you will also need cURL.

## Usage
The most commands needed exist in the `Makefile`. This includes
common commands for Docker, as well as commands for extra functionality,
like printing url of the available services to stdout.

### Basic usage

- To run this project and test the results you simply need to run `$ make up`,

- head to `localhost:8080/test` (default port) make a few requests or use:

```bash
$ make call
```

`call` will perform a number of requests via curl. Default is 100 requests.
If you wish to change the number head to `.env.docker` and set `TIMES_TO_CALL_EXAMPLE_ROUTE`
to your preference.

- The last step is to open Grafana and see your metrics!
Grafana listens to `localhost:3326` (default port).

### Useful commands

- To print all the available commands
```bash
$ make help
```

- To create and start the application services run:
```bash
$ make up
```

- To start the application services run:
```bash
$ make start
```

- To stop the application services run:
```bash
$ make stop
```

- To completely remove the application services run:
```bash
$ make down
```

### Change application ports

The ports registered in the example application might be already
taken. If you happen to have conflicts you need to change the port having
conflicts in `.env.docker`.

E.g.
The Grafana port is registered at `3326`

```dotenv
EXAMPLE_APP_GRAFANA_PORT=3326
```

You'll need to change it as:

```dotenv
EXAMPLE_APP_GRAFANA_PORT=3111
```

### Rebuild the example app container

To remove the built image and create it again:
```bash
$ make rebuild
```

### Calling the route

The example route for testing is at `localhost:8080/test`

Each time you perform `make up` all the services get printed.
To print all the available services use:

```bash
$ make generate-links
```

### Updating Grafana

Each time you perform `make up` the `dashboard.json` Grafana dashboards along with
the available data-sources, get updated.
To perform the update by yourself use:

```bash
$ make provision
```

The default Grafana credentials are:
```
username: admin
password: admin
```

To check the diagrams containing metrics, head to your
given Grafana URL and click on `Home -> App Overview`.

Prometheus scrapes every 30 seconds so be patient and
make enough calls to see your metrics.

An example view if you have completed the process successfully
should be:

![grafana example](docs/grafana_example.png)

Empty file.
88 changes: 88 additions & 0 deletions examples/lumen-app/app/Console/Commands/DatabaseCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;
use PDO;
use PDOException;

class DatabaseCreate extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'db:create';

/**
* The console command description.
*
* @var string
*/
protected $description = 'This command creates a new database';

/**
* The console command signature.
*
* @var string
*/
protected $signature = 'db:create';

/**
* Execute the console command.
*/
public function handle()
{
$defaultConnection = config('database.default');
$database = config("database.connections.{$defaultConnection}.database");
$host = config("database.connections.{$defaultConnection}.host");
$port = config("database.connections.{$defaultConnection}.port");
$username = config("database.connections.{$defaultConnection}.username");
$password = config("database.connections.{$defaultConnection}.password");

if (App::environment() === 'production') {
$this->error('Command supports only local and testing environments!');

return;
}

if (!$database) {
$this->info('Skipping creation of database as env(DB_DATABASE) is empty');

return;
}

try {
$pdo = $this->getPDOConnection($host, $port, $username, $password);

$res = $pdo->exec(sprintf(
'CREATE DATABASE IF NOT EXISTS %s',
$database
));

if ($res) {
$this->info(sprintf('Successfully created %s database', $database));

return;
}
throw new PDOException;
} catch (PDOException $exception) {
$this->error(sprintf('Failed to create %s database, %s', $database, $exception->getMessage()));
}
}

/**
* @param string $host
* @param int $port
* @param string $username
* @param string $password
*
* @return PDO
*/
private function getPDOConnection($host, $port, $username, $password)
{
return new PDO(sprintf('mysql:host=%s;port=%d;', $host, $port), $username, $password);
}
}
29 changes: 29 additions & 0 deletions examples/lumen-app/app/Console/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\DatabaseCreate::class,
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
//
}
}
Loading