Skip to content

Commit 3bf1f7d

Browse files
committed
first commit
1 parent 45245f9 commit 3bf1f7d

File tree

86 files changed

+6927
-0
lines changed

Some content is hidden

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

86 files changed

+6927
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.cache
2+
tests/reports
3+
vendor/
4+
.idea/
5+
.phpunit.result.cache
6+
.phpunit.cache

.php-cs-fixer.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return (new PhpCsFixer\Config())
6+
->setRiskyAllowed(true)
7+
->setRules([
8+
'@PSR2' => true,
9+
'@Symfony' => true,
10+
'@DoctrineAnnotation' => true,
11+
'@PhpCsFixer' => true,
12+
'header_comment' => [
13+
'comment_type' => 'PHPDoc',
14+
'header' => '',
15+
'separate' => 'none',
16+
'location' => 'after_declare_strict',
17+
],
18+
'array_syntax' => [
19+
'syntax' => 'short',
20+
],
21+
'list_syntax' => [
22+
'syntax' => 'short',
23+
],
24+
'concat_space' => [
25+
'spacing' => 'one',
26+
],
27+
'blank_line_before_statement' => [
28+
'statements' => [
29+
'declare',
30+
],
31+
],
32+
'general_phpdoc_annotation_remove' => [
33+
'annotations' => [
34+
'author',
35+
],
36+
],
37+
'ordered_imports' => [
38+
'imports_order' => [
39+
'class', 'function', 'const',
40+
],
41+
'sort_algorithm' => 'alpha',
42+
],
43+
'single_line_comment_style' => [
44+
'comment_types' => [
45+
],
46+
],
47+
'yoda_style' => [
48+
'always_move_variable' => false,
49+
'equal' => false,
50+
'identical' => false,
51+
],
52+
'phpdoc_align' => [
53+
'align' => 'left',
54+
],
55+
'multiline_whitespace_before_semicolons' => [
56+
'strategy' => 'no_multi_line',
57+
],
58+
'constant_case' => [
59+
'case' => 'lower',
60+
],
61+
'class_attributes_separation' => true,
62+
'combine_consecutive_unsets' => true,
63+
'declare_strict_types' => true,
64+
'linebreak_after_opening_tag' => true,
65+
'lowercase_static_reference' => true,
66+
'no_useless_else' => true,
67+
'no_unused_imports' => true,
68+
'not_operator_with_successor_space' => true,
69+
'not_operator_with_space' => false,
70+
'ordered_class_elements' => true,
71+
'php_unit_strict' => false,
72+
'phpdoc_separation' => false,
73+
'single_quote' => true,
74+
'standardize_not_equals' => true,
75+
'multiline_comment_opening_closing' => true,
76+
'php_unit_test_class_requires_covers' => false,
77+
'global_namespace_import' => [
78+
'import_classes' => true,
79+
'import_constants' => true,
80+
'import_functions' => true,
81+
],
82+
'single_line_empty_body' => false,
83+
'class_definition' => [
84+
'multi_line_extends_each_single_line' => true,
85+
],
86+
'types_spaces' => [
87+
'space' => 'single',
88+
],
89+
])
90+
->setFinder(
91+
PhpCsFixer\Finder::create()
92+
->exclude('public')
93+
->exclude('runtime')
94+
->exclude('vendor')
95+
->exclude('bin')
96+
->in(__DIR__)
97+
)
98+
->setUsingCache(false);

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM hyperf/hyperf:8.3-alpine-v3.20-swoole
2+
3+
WORKDIR /opt/www
4+
5+
COPY ./composer.* /opt/www
6+
RUN composer install --prefer-dist
7+
COPY . /opt/www
8+
9+
ENTRYPOINT [ "sh" ]

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
build:
2+
@docker-compose build hyperf-opentelemetry
3+
4+
sh:
5+
@docker-compose run --rm hyperf-opentelemetry
6+
7+
install:
8+
@docker-compose run --rm hyperf-opentelemetry -c "composer install"
9+
10+
update:
11+
@docker-compose run --rm hyperf-opentelemetry -c "composer update"
12+
13+
test:
14+
@docker-compose run --rm hyperf-opentelemetry sh -c "composer test"
15+

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# hyperf-opentelemetry
2+
3+
[![Status](https://img.shields.io/badge/status-beta-yellow)]() [![License](https://img.shields.io/badge/license-MIT-blue.svg)]() [![PHP](https://img.shields.io/badge/php-%3E%3D8.1-777bb4.svg?logo=php&logoColor=white)]() [![Hyperf](https://img.shields.io/badge/framework-Hyperf-green)]() [![OpenTelemetry](https://img.shields.io/badge/observability-OpenTelemetry-orange)]()
4+
5+
Instrumentation library for Hyperf applications with OpenTelemetry support.
6+
7+
This library enables instrumentation of Hyperf-based applications for exporting metrics, traces, and logs compatible with the OpenTelemetry standard.
8+
9+
---
10+
11+
## ✨ Features
12+
13+
- 📦 Ready-to-use with Swoole and Coroutine
14+
- 📊 Custom metrics support via Meter
15+
- 📈 Trace instrumentation for:
16+
- HTTP requests (Hyperf\HttpServer)
17+
- Redis
18+
- Guzzle
19+
- SQL queries (Hyperf\Database)
20+
- ♻️ Integration with Swoole ContextStorage
21+
22+
---
23+
24+
## 📦 Installation
25+
26+
```shell
27+
composer require opencodeco/hyperf-opentelemetry
28+
```
29+
30+
## ⚙️ Configuration
31+
1. Publish the configuration file
32+
```shell
33+
php bin/hyperf.php vendor:publish opencodeco/hyperf-opentelemetry
34+
```
35+
36+
Edit the file config/autoload/open-telemetry.php to adjust settings (enable/disable features, OTLP endpoints, resource attributes, etc).
37+
38+
2. Configure environment variables
39+
40+
Example .env:
41+
42+
```shell
43+
OTEL_TRACES_ENDPOINT=http://otelcol:4318/v1/traces
44+
OTEL_METRICS_ENDPOINT=http://otelcol:4318/v1/metrics
45+
```
46+
47+
3. Add instrumentation middlewares
48+
49+
config/autoload/middlewares.php:
50+
51+
```php
52+
<?php
53+
54+
declare(strict_types=1);
55+
56+
use Hyperf\OpenTelemetry\Middleware\MetricMiddleware;
57+
use Hyperf\OpenTelemetry\Middleware\TraceMiddleware;
58+
59+
return [
60+
'http' => [
61+
MetricMiddleware::class,
62+
TraceMiddleware::class,
63+
],
64+
];
65+
```
66+
67+
## 👨‍💻 Development
68+
Build the image
69+
```shell
70+
make build
71+
```
72+
73+
Install dependencies
74+
```shell
75+
make install
76+
```
77+
78+
Run tests
79+
```shell
80+
make test
81+
```

composer.json

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"name": "opencodeco/hyperf-opentelemetry",
3+
"description": "OpenTelemetry instrumentation for Hyperf framework",
4+
"type": "library",
5+
"license": "MIT",
6+
"version": "0.1.0",
7+
"keywords": [
8+
"php",
9+
"hyperf",
10+
"opentelemetry",
11+
"tracing",
12+
"metrics",
13+
"logs"
14+
],
15+
"authors": [
16+
{
17+
"name": "Thiago Faino",
18+
"email": "[email protected]"
19+
},
20+
{
21+
"name": "Lucas Angeli",
22+
"email": "[email protected]"
23+
}
24+
],
25+
"minimum-stability": "dev",
26+
"require": {
27+
"php": ">=8.1",
28+
"hyperf/command": "~3.1.0",
29+
"hyperf/crontab": "~3.1.0",
30+
"hyperf/database": "~3.1.0",
31+
"hyperf/di": "~3.1.0",
32+
"hyperf/event": "~3.1.0",
33+
"hyperf/guzzle": "~3.1.0",
34+
"hyperf/http-server": "~3.1.0",
35+
"open-telemetry/api": "^1.1",
36+
"open-telemetry/context-swoole": "^1.0",
37+
"open-telemetry/exporter-otlp": "^1.1",
38+
"open-telemetry/sdk": "^1.1",
39+
"open-telemetry/sem-conv": "^1.27"
40+
},
41+
"require-dev": {
42+
"friendsofphp/php-cs-fixer": "^3.40",
43+
"hyperf/testing": "^3.0",
44+
"mockery/mockery": "^1.6",
45+
"phpmd/phpmd": "^2.14",
46+
"rector/rector": "^1.0",
47+
"roave/security-advisories": "dev-latest",
48+
"squizlabs/php_codesniffer": "^3.7",
49+
"swoole/ide-helper": "^5.1"
50+
},
51+
"autoload": {
52+
"psr-4": {
53+
"Hyperf\\OpenTelemetry\\" : "src/"
54+
}
55+
},
56+
"autoload-dev": {
57+
"psr-4": {
58+
"Tests\\": "tests/"
59+
}
60+
},
61+
"extra": {
62+
"hyperf": {
63+
"config": "Hyperf\\OpenTelemetry\\ConfigProvider"
64+
}
65+
},
66+
"scripts": {
67+
"test": "phpunit",
68+
"lint:fix": [
69+
"composer lint:cs:fix",
70+
"composer lint:update"
71+
],
72+
"lint:cs:fix": ["php-cs-fixer fix $1", "phpcbf --standard=PSR12 src"],
73+
"lint:cs:check": ["php-cs-fixer fix -v --diff --dry-run $1", "phpcs --standard=PSR12,phpcs.xml -s src"],
74+
"lint:mess": "phpmd src ansi phpmd.xml",
75+
"lint:updated": "rector --dry-run",
76+
"lint:update": "rector",
77+
"lint": [
78+
"composer lint:cs:check",
79+
"composer lint:mess",
80+
"composer lint:updated"
81+
],
82+
"ci": [
83+
"composer lint",
84+
"composer test"
85+
]
86+
},
87+
"config": {
88+
"sort-packages": true,
89+
"allow-plugins": {
90+
"infection/extension-installer": true,
91+
"php-http/discovery": true,
92+
"tbachert/spi": true
93+
}
94+
}
95+
}

docker-compose.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
3+
hyperf-opentelemetry:
4+
container_name: hyperf-opentelemetry
5+
image: hyperf-opentelemetry
6+
build: .
7+
volumes:
8+
- .:/opt/www

phpcs.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="opencodeco/hyperf-template">
3+
<rule ref="PSR1">
4+
<!-- False positive from class readonly with php 8.2 -->
5+
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols"/>
6+
</rule>
7+
</ruleset>

phpmd.xml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="opencodeco/hyperf-template"
3+
xmlns="http://pmd.sf.net/ruleset/1.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
6+
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
7+
<rule ref="rulesets/cleancode.xml">
8+
<exclude name="StaticAccess" />
9+
</rule>
10+
<rule ref="rulesets/codesize.xml">
11+
<exclude name="TooManyPublicMethods" />
12+
<exclude name="ExcessiveParameterList" />
13+
<exclude name="TooManyMethods" />
14+
</rule>
15+
<rule ref="rulesets/design.xml">
16+
<exclude name="CouplingBetweenObjects" />
17+
<exclude name="NumberOfChildren" />
18+
</rule>
19+
<rule ref="rulesets/naming.xml">
20+
<exclude name="ShortVariable" />
21+
<exclude name="LongVariable" />
22+
<exclude name="ShortMethodName" />
23+
</rule>
24+
<rule ref="rulesets/naming.xml/ShortVariable">
25+
<properties>
26+
<property name="exceptions" value="id,db,iv" />
27+
</properties>
28+
</rule>
29+
<rule ref="rulesets/naming.xml/ShortMethodName">
30+
<properties>
31+
<property name="exceptions" value="id" />
32+
</properties>
33+
</rule>
34+
<rule ref="rulesets/naming.xml/LongVariable">
35+
<properties>
36+
<property name="maximum" value="25" />
37+
</properties>
38+
</rule>
39+
</ruleset>

0 commit comments

Comments
 (0)