Skip to content
This repository was archived by the owner on Dec 22, 2025. It is now read-only.

Commit 54955b7

Browse files
authored
Merge pull request #1 from hellofresh/feature/initial-version
Initial version
2 parents 8c4b32c + 6a24af6 commit 54955b7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2822
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/*
2+
composer.lock
3+
.php_cs.cache

.php_cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->exclude('vendor')
5+
->in(__DIR__);
6+
7+
return PhpCsFixer\Config::create()
8+
->setRiskyAllowed(true)
9+
->setRules([
10+
'@PSR2' => true,
11+
'array_syntax' => ['syntax' => 'short'],
12+
13+
'blank_line_after_namespace' => true,
14+
'blank_line_before_return' => true,
15+
'concat_space' => ['spacing' => 'one'],
16+
'function_typehint_space' => true,
17+
'hash_to_slash_comment' => true,
18+
'include' => true,
19+
'lowercase_cast' => true,
20+
'new_with_braces' => true,
21+
'no_blank_lines_before_namespace' => true,
22+
'no_empty_statement' => true,
23+
'no_extra_consecutive_blank_lines' => ['use'],
24+
'no_leading_import_slash' => true,
25+
'no_unused_imports' => true,
26+
'no_whitespace_in_blank_line' => true,
27+
'object_operator_without_whitespace' => true,
28+
'phpdoc_align' => true,
29+
'phpdoc_scalar' => true,
30+
'phpdoc_types' => true,
31+
'short_scalar_cast' => true,
32+
'single_blank_line_at_eof' => true,
33+
'single_quote' => true,
34+
'trailing_comma_in_multiline_array' => true,
35+
'ordered_imports' => true,
36+
'phpdoc_order' => true,
37+
])
38+
->setFinder($finder);

.travis.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
3+
php:
4+
- '5.6'
5+
- '7.0'
6+
- '7.1'
7+
8+
env:
9+
- COMPOSER_UPDATE=
10+
- COMPOSER_UPDATE=--prefer-lowest
11+
12+
script:
13+
- composer update $COMPOSER_UPDATE -n
14+
- vendor/bin/php-cs-fixer fix --dry-run -v
15+
- vendor/bin/phpunit --coverage-clover=coverage.xml
16+
17+
after_success:
18+
- if [ "$TRAVIS_SECURE_ENV_VARS" == "true" ]; then bash <(curl -s https://codecov.io/bash); fi

CODE_OF_CONDUCT.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
11+
* Using welcoming and inclusive language
12+
* Being respectful of differing viewpoints and experiences
13+
* Gracefully accepting constructive criticism
14+
* Focusing on what is best for the community
15+
* Showing empathy towards other community members
16+
17+
Examples of unacceptable behavior by participants include:
18+
19+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
20+
* Trolling, insulting/derogatory comments, and personal or political attacks
21+
* Public or private harassment
22+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
23+
* Other conduct which could reasonably be considered inappropriate in a professional setting
24+
25+
## Our Responsibilities
26+
27+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28+
29+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30+
31+
## Scope
32+
33+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34+
35+
## Enforcement
36+
37+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [[email protected]](mailto:[email protected]). All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38+
39+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40+
41+
## Attribution
42+
43+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44+
45+
[homepage]: http://contributor-covenant.org
46+
[version]: http://contributor-covenant.org/version/1/4/

CONTRIBUTING.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Contributing to hellofresh/stats-php
2+
3+
:+1::tada: First off, thanks for taking the time to contribute! :tada::+1:
4+
5+
The following is a set of guidelines for contributing to `hellofresh/stats-php`,
6+
which are hosted in the [HelloFresh Organization](https://github.com/hellofresh) on GitHub.
7+
These are just guidelines, not rules. Use your best judgment, and feel free to propose changes
8+
to this document in a pull request.
9+
10+
## Code of Conduct
11+
12+
This project adheres to the Contributor Covenant [code of conduct](CODE_OF_CONDUCT.md).
13+
By participating, you are expected to uphold this code.
14+
Please report unacceptable behavior to [[email protected]](mailto:[email protected]).
15+
16+
We accept contributions via Pull Requests on [Github](https://github.com/hellofresh/stats-php).
17+
18+
## How Can I Contribute?
19+
20+
### Reporting Bugs
21+
22+
This section guides you through submitting a bug report for `hellofresh/stats-php`. Following these guidelines helps maintainers
23+
and the community understand your report :pencil:, reproduce the behavior :computer: :computer:, and find related
24+
reports :mag_right:.
25+
26+
Before creating bug reports, please check if the bug was already reported before as you might find out that you don't
27+
need to create one. When you are creating a bug report, please [include as many details as possible](#how-do-i-submit-a-good-bug-report).
28+
29+
#### How Do I Submit A (Good) Bug Report?
30+
31+
Bugs are tracked as [GitHub issues](https://guides.github.com/features/issues/). Create an issue on provide the following information.
32+
33+
Explain the problem and include additional details to help maintainers reproduce the problem:
34+
35+
* **Use a clear and descriptive title** for the issue to identify the problem.
36+
* **Describe the exact steps which reproduce the problem** in as many details as possible. For example, start by explaining how you started `hellofresh/stats-php`,
37+
e.g. which command exactly you used in the terminal. When listing steps, **don't just say what you did, but explain how you did it**.
38+
* **Provide specific examples to demonstrate the steps**. Include links to files or GitHub projects, or copy/pasteable snippets, which you use in those examples.
39+
If you're providing snippets in the issue, use [Markdown code blocks](https://help.github.com/articles/markdown-basics/#multiple-lines).
40+
* **Describe the behavior you observed after following the steps** and point out what exactly is the problem with that behavior.
41+
* **Explain which behavior you expected to see instead and why.**
42+
43+
Include details about your configuration and environment:
44+
45+
* **Which version of `hellofresh/stats-php` are you using?**
46+
* **What's the name and version of the OS you're using**?
47+
48+
### Your First Code Contribution
49+
50+
Unsure where to begin contributing to `hellofresh/stats-php`? You can start by looking through these `beginner` and `help-wanted` issues:
51+
52+
* [Beginner issues][beginner] - issues which should only require a few lines of code, and a test or two.
53+
* [Help wanted issues][help-wanted] - issues which should be a bit more involved than `beginner` issues.
54+
55+
Both issue lists are sorted by total number of comments. While not perfect, number of comments is a reasonable proxy for impact a given change will have.
56+
57+
### Pull Requests
58+
59+
* Include screenshots and animated GIFs in your pull request whenever possible.
60+
* Include thoughtfully-worded, well-structured tests.
61+
* Document new code
62+
* End files with a newline.
63+
64+
65+
Happy Coding from the HelloFresh Engineering team!

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 HelloFresh SE <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<p align="center">
2+
<a href="https://hellofresh.com">
3+
<img width="120" src="https://www.hellofresh.de/images/hellofresh/press/HelloFresh_Logo.png">
4+
</a>
5+
</p>
6+
7+
# hellofresh/stats-php
8+
9+
[![Build Status](https://travis-ci.org/hellofresh/stats-php.svg?branch=master)](https://travis-ci.org/hellofresh/stats-php)
10+
[![codecov](https://codecov.io/gh/hellofresh/stats-php/branch/master/graph/badge.svg)](https://codecov.io/gh/hellofresh/stats-php)
11+
12+
> Generic Stats library written in PHP
13+
14+
This is generic stats library that we at HelloFresh use in our projects to collect services' stats and then create monitoring
15+
dashboards to track activity and problems.
16+
17+
## Key Features
18+
19+
* Several stats backends:
20+
* `log` for development environment
21+
* `statsd` for production
22+
* `memory` for testing purpose, to track stats operations in unit tests
23+
* `noop` for environments that do not require any stats gathering
24+
* Fixed metric sections count for all metrics to allow easy monitoring/alerting setup in `grafana`
25+
* Easy to build HTTP requests metrics - timing and count
26+
27+
## Installation
28+
29+
```sh
30+
composer require hellofresh/stats-php
31+
```
32+
33+
## Usage
34+
35+
### Instance creation
36+
37+
Connection DSN has the following format: `<type>://<connection params>/<connection path>?<connection options>`.
38+
39+
* `<type>` - one of supported backends: `log`, `statsd`, `memory`, `noop`
40+
* `<connection params>` - used for `statsd` backend only, to defining host and port
41+
* `<connection path>` - used for `statsd` backend only, to define prefix/namespace
42+
* `<connection options>` - user for `statsd` backend only:
43+
* `timeout` - statsd request timeout in seconds, if not set `ini_get('default_socket_timeout')` is used
44+
* `error` - throw connection error exception, default value is `true`
45+
46+
```php
47+
<?php
48+
49+
use HelloFresh\Stats\Factory;
50+
51+
$statsdClient = Factory::build('statsd://statsd-host:8125/prefix?timeout=2.5&error=1', $logger);
52+
53+
// php parse_url does not support url with only schema part set
54+
$logClient = Factory::build('log://log', $logger);
55+
56+
// php parse_url does not support url with only schema part set
57+
$noopClient = Factory::build('noop://noop', $logger);
58+
59+
// php parse_url does not support url with only schema part set
60+
$memoryClient = Factory::build('memory://memory', $logger);
61+
62+
// php parse_url does not support url with only schema part set
63+
$statsClient = Factory::build(getenv('STATS_DSN'), $logger);
64+
```
65+
66+
### Count metrics manually
67+
68+
```php
69+
<?php
70+
71+
use HelloFresh\Stats\Bucket\MetricOperation;
72+
use HelloFresh\Stats\Factory;
73+
74+
$statsClient = Factory::build(getenv('STATS_DSN'), $logger);
75+
76+
$section = 'ordering';
77+
$timer = $statsClient->buildTimer()->start();
78+
$operation = new MetricOperation(['orders', 'order', 'create']);
79+
80+
try {
81+
OrdersService::create(...);
82+
$statsClient->trackOperation($section, $operation, true, $timer);
83+
} catch (\Exception $e) {
84+
$statsClient->trackOperation($section, $operation, false, $timer);
85+
}
86+
87+
$statsClient->trackMetric('requests', $operation);
88+
89+
$ordersInTheLast24Hours = OrdersService::count(60 * 60 * 24);
90+
$statsClient->trackState($section, $operation, $ordersInTheLast24Hours);
91+
```
92+
93+
## TODO
94+
95+
* [ ] Generalise or modify HTTP Requests metric - e.g. skip ID part

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "hellofresh/stats-php",
3+
"keywords": [
4+
"logging",
5+
"stats"
6+
],
7+
"homepage": "https://github.com/hellofresh/stats-php",
8+
"license": "MIT",
9+
"authors": [
10+
{
11+
"name": "Vladimir Garvardt",
12+
"email": "[email protected]"
13+
}
14+
],
15+
"support": {
16+
"email": "[email protected]"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"HelloFresh\\Stats\\": "src/"
21+
}
22+
},
23+
"require": {
24+
"php": ">= 5.6",
25+
"psr/http-message": "^1.0",
26+
"behat/transliterator": "^1.2",
27+
"psr/log": "^1.0"
28+
},
29+
"require-dev": {
30+
"friendsofphp/php-cs-fixer": "^2.3",
31+
"phpunit/phpunit": "^5.7",
32+
"league/statsd": "^1.4"
33+
},
34+
"suggest": {
35+
"league/statsd": "Required for statsd backend"
36+
}
37+
}

docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: '3'
2+
services:
3+
app:
4+
image: quay.io/hellofresh/php56
5+
volumes:
6+
- ".:/app"

phpunit.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="tests/bootstrap.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="true"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
verbose="true"
13+
>
14+
<logging>
15+
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
16+
</logging>
17+
18+
<testsuites>
19+
<testsuite name="main">
20+
<directory>./tests/</directory>
21+
</testsuite>
22+
</testsuites>
23+
24+
<filter>
25+
<whitelist>
26+
<directory>./src</directory>
27+
</whitelist>
28+
</filter>
29+
</phpunit>

0 commit comments

Comments
 (0)