Skip to content

Commit 8a37465

Browse files
SergeyKleymanElfoLiNk
authored andcommitted
[chore] Build docker image for PHP auto-intrumentation (open-telemetry#3409)
* Build docker image for PHP autointrumentation * Fixed parts were commented out during debugging * Fixed path to relative since it's used in Dockerfile * Marked autoinstrumentation/php/prepare_files_for_docker_image.sh as executable * Fixed error message in autoinstrumentation/php/prepare_files_for_docker_image.sh * Build docker image for PHP autointrumentation * Fixed parts were commented out during debugging * Fixed path to relative since it's used in Dockerfile * Marked autoinstrumentation/php/prepare_files_for_docker_image.sh as executable * Fixed error message in autoinstrumentation/php/prepare_files_for_docker_image.sh * Added missing autoinstrumentation/php/version.txt * Removed support for PHP 8.0 Since PHP 8.0 support was removed from OpenTelemetry for PHP SDK (from version 1.1) * Updated versions of instrumentation packages to the latest * Fixed publish-autoinstrumentation-php.yaml Prepare files for docker image * Deleted composer_PHP_8.0.json that was used only for PHP 8.0 * Added PHP to /versions.txt * Renamed composer*.json files to "before_8.2" and "8.2_and_later"
1 parent d565846 commit 8a37465

7 files changed

+384
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: "Publish PHP Auto-Instrumentation"
2+
3+
on:
4+
push:
5+
paths:
6+
- 'autoinstrumentation/php/**'
7+
- '.github/workflows/publish-autoinstrumentation-php.yaml'
8+
branches:
9+
- main
10+
pull_request:
11+
paths:
12+
- 'autoinstrumentation/php/**'
13+
- '.github/workflows/publish-autoinstrumentation-php.yaml'
14+
workflow_dispatch:
15+
16+
concurrency:
17+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
18+
cancel-in-progress: true
19+
20+
jobs:
21+
publish:
22+
runs-on: ubuntu-22.04
23+
24+
steps:
25+
- uses: actions/checkout@v4
26+
27+
- name: Read version
28+
run: echo "VERSION=$(cat autoinstrumentation/php/version.txt)" >> $GITHUB_ENV
29+
30+
- name: Docker meta
31+
id: meta
32+
uses: docker/metadata-action@v5
33+
with:
34+
images: |
35+
otel/autoinstrumentation-php
36+
ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-php
37+
tags: |
38+
type=match,pattern=v(.*),group=1,value=v${{ env.VERSION }}
39+
40+
- name: Set up Docker Buildx
41+
uses: docker/setup-buildx-action@v3
42+
43+
- name: Cache Docker layers
44+
uses: actions/cache@v4
45+
with:
46+
path: /tmp/.buildx-cache
47+
key: ${{ runner.os }}-buildx-${{ github.sha }}
48+
restore-keys: |
49+
${{ runner.os }}-buildx-
50+
51+
- name: Log into Docker.io
52+
uses: docker/login-action@v3
53+
if: ${{ github.event_name == 'push' }}
54+
with:
55+
username: ${{ secrets.DOCKER_USERNAME }}
56+
password: ${{ secrets.DOCKER_PASSWORD }}
57+
58+
- name: Login to GitHub Package Registry
59+
uses: docker/login-action@v3
60+
if: ${{ github.event_name == 'push' }}
61+
with:
62+
registry: ghcr.io
63+
username: ${{ github.repository_owner }}
64+
password: ${{ secrets.GITHUB_TOKEN }}
65+
66+
- name: Prepare files for docker image
67+
run: pushd ./autoinstrumentation/php ; ./prepare_files_for_docker_image.sh --ext-ver ${{ env.VERSION }} --dest-dir ${PWD}/files_for_docker_image ; popd
68+
69+
- name: Build and push
70+
uses: docker/build-push-action@v6
71+
with:
72+
context: autoinstrumentation/php
73+
push: ${{ github.event_name == 'push' }}
74+
build-args: SUB_DIR_WITH_FILES_FOR_DOCKER_IMAGE=files_for_docker_image
75+
tags: ${{ steps.meta.outputs.tags }}
76+
labels: ${{ steps.meta.outputs.labels }}
77+
cache-from: type=local,src=/tmp/.buildx-cache
78+
cache-to: type=local,dest=/tmp/.buildx-cache

autoinstrumentation/php/Dockerfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# To build one auto-instrumentation image for php, please:
2+
# - Download your php auto-instrumentation artefacts to `/autoinstrumentation` directory. This is required as when instrumenting the pod,
3+
# one init container will be created to copy the files to your app's container.
4+
# - Grant the necessary access to the files in the `/autoinstrumentation` directory.
5+
# - Following environment variables are injected to the application container to enable the auto-instrumentation.
6+
# PHP_INI_SCAN_DIR=:/otel-auto-instrumentation-php/php_ini_scan_dir
7+
# OTEL_PHP_AUTOLOAD_ENABLED=true
8+
# - For auto-instrumentation by container injection, the Linux command cp is
9+
# used and must be availabe in the image.
10+
11+
FROM busybox
12+
13+
ARG SUB_DIR_WITH_FILES_FOR_DOCKER_IMAGE
14+
15+
COPY ${SUB_DIR_WITH_FILES_FOR_DOCKER_IMAGE} /autoinstrumentation/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "open-telemetry/operator-autoinstrumentation-php",
3+
"description": "OpenTelemetry PHP auto-instrumentation packages to include in the image used by OpenTelemetry Operator for Kubernetes",
4+
"type": "project",
5+
"require": {
6+
"open-telemetry/exporter-otlp": "1.1.0",
7+
"open-telemetry/opentelemetry-auto-guzzle": "1.0.1",
8+
"open-telemetry/opentelemetry-auto-http-async": "1.0.1",
9+
"open-telemetry/opentelemetry-auto-laravel": "1.0.1",
10+
"open-telemetry/opentelemetry-auto-pdo": "0.0.16",
11+
"open-telemetry/opentelemetry-auto-psr15": "1.0.6",
12+
"open-telemetry/opentelemetry-auto-psr18": "1.0.4",
13+
"open-telemetry/opentelemetry-auto-slim": "1.0.7",
14+
"open-telemetry/opentelemetry-auto-symfony": "1.0.0beta30",
15+
"open-telemetry/opentelemetry-auto-wordpress": "0.0.16",
16+
"open-telemetry/sdk": "1.1.2",
17+
"php-http/guzzle7-adapter": "1.0.0"
18+
},
19+
"provide": {
20+
"psr/http-client": "*",
21+
"psr/http-server-middleware": "*",
22+
"laravel/framework": "*",
23+
"slim/slim": "*",
24+
"symfony/http-client-contracts": "*",
25+
"symfony/http-kernel": "*"
26+
},
27+
"config": {
28+
"process-timeout": 0,
29+
"sort-packages": true,
30+
"allow-plugins": {
31+
"dealerdirect/phpcodesniffer-composer-installer": true,
32+
"php-http/discovery": true
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "open-telemetry/operator-autoinstrumentation-php",
3+
"description": "OpenTelemetry PHP auto-instrumentation packages to include in the image used by OpenTelemetry Operator for Kubernetes",
4+
"type": "project",
5+
"require": {
6+
"open-telemetry/exporter-otlp": "1.1.0",
7+
"open-telemetry/opentelemetry-auto-guzzle": "1.0.1",
8+
"open-telemetry/opentelemetry-auto-http-async": "1.0.1",
9+
"open-telemetry/opentelemetry-auto-laravel": "1.0.1",
10+
"open-telemetry/opentelemetry-auto-psr15": "1.0.6",
11+
"open-telemetry/opentelemetry-auto-psr18": "1.0.4",
12+
"open-telemetry/opentelemetry-auto-slim": "1.0.7",
13+
"open-telemetry/opentelemetry-auto-symfony": "1.0.0beta30",
14+
"open-telemetry/opentelemetry-auto-wordpress": "0.0.16",
15+
"open-telemetry/sdk": "1.1.2",
16+
"php-http/guzzle7-adapter": "1.0.0"
17+
},
18+
"provide": {
19+
"psr/http-client": "*",
20+
"psr/http-server-middleware": "*",
21+
"laravel/framework": "*",
22+
"slim/slim": "*",
23+
"symfony/http-client-contracts": "*",
24+
"symfony/http-kernel": "*"
25+
},
26+
"config": {
27+
"process-timeout": 0,
28+
"sort-packages": true,
29+
"allow-plugins": {
30+
"dealerdirect/phpcodesniffer-composer-installer": true,
31+
"php-http/discovery": true
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
#!/usr/bin/env bash
2+
set -xe -o pipefail
3+
4+
PHP_versions=(8.1 8.2 8.3)
5+
libc_variants=(glibc musl)
6+
7+
show_help() {
8+
echo "Usage: $0 --ext-ver <opentelemetry extension version> --dest-dir <destination directory>"
9+
echo
10+
echo "Arguments:"
11+
echo " <opentelemetry extension version> - opentelemetry PHP extension version to use. This argument is mandatory."
12+
echo " <destination directory> - Directory to store files for docker image. All existing files in this directory will be deleted. This argument is mandatory."
13+
echo
14+
echo "Example:"
15+
echo " $0 ./files_for_docker_image"
16+
}
17+
18+
parse_args() {
19+
while [[ "$#" -gt 0 ]]; do
20+
case $1 in
21+
--ext-ver)
22+
opentelemetry_extension_version="$2"
23+
shift
24+
;;
25+
--dest-dir)
26+
destination_directory="$2"
27+
shift
28+
;;
29+
--help)
30+
show_help
31+
exit 0
32+
;;
33+
*)
34+
echo "Unknown parameter passed: $1"
35+
show_help
36+
exit 1
37+
;;
38+
esac
39+
shift
40+
done
41+
42+
if [ -z "${opentelemetry_extension_version}" ] ; then
43+
echo "<opentelemetry extension version> argument is missing"
44+
show_help
45+
exit 1
46+
fi
47+
if [ -z "${destination_directory}" ] ; then
48+
echo "<destination directory> argument is missing"
49+
show_help
50+
exit 1
51+
fi
52+
}
53+
54+
ensure_dir_exists_and_empty() {
55+
local dir_to_clean="${1:?}"
56+
57+
if [ -d "${dir_to_clean}" ]; then
58+
rm -rf "${dir_to_clean}"
59+
if [ -d "${dir_to_clean}" ]; then
60+
echo "Directory ${dir_to_clean} still exists. Directory content:"
61+
ls -l "${dir_to_clean}"
62+
exit 1
63+
fi
64+
else
65+
mkdir -p "${dir_to_clean}"
66+
fi
67+
}
68+
69+
build_native_binaries_for_PHP_version_libc_variant() {
70+
local PHP_version="${1:?}"
71+
local libc_variant="${2:?}"
72+
local dest_dir_for_current_args
73+
dest_dir_for_current_args="${destination_directory}/native_binaries/PHP_${PHP_version}_${libc_variant}"
74+
75+
echo "Building extension binaries for PHP version: ${PHP_version} and libc variant: ${libc_variant} to ${dest_dir_for_current_args} ..."
76+
77+
ensure_dir_exists_and_empty "${dest_dir_for_current_args}"
78+
79+
local PHP_docker_image="php:${PHP_version}-cli"
80+
local install_compiler_command=""
81+
case "${libc_variant}" in
82+
glibc)
83+
;;
84+
musl)
85+
PHP_docker_image="${PHP_docker_image}-alpine"
86+
install_compiler_command="&& apk update && apk add autoconf build-base"
87+
;;
88+
*)
89+
echo "Unexpected libc variant: ${libc_variant}"
90+
exit 1
91+
;;
92+
esac
93+
94+
local current_user_id
95+
current_user_id="$(id -u)"
96+
local current_user_group_id
97+
current_user_group_id="$(id -g)"
98+
docker run --rm \
99+
-v "${dest_dir_for_current_args}:/dest_dir" \
100+
"${PHP_docker_image}" sh -c "\
101+
mkdir -p /app && cd /app \
102+
${install_compiler_command} \
103+
&& pecl install opentelemetry-${opentelemetry_extension_version} \
104+
&& cp /usr/local/lib/php/extensions/no-debug-non-zts-*/opentelemetry.so /dest_dir/ \
105+
&& chown -R ${current_user_id}:${current_user_group_id} /dest_dir/"
106+
107+
echo "Built extension binaries for PHP version: ${PHP_version} and libc variant: ${libc_variant}"
108+
}
109+
110+
build_native_binaries() {
111+
echo "Building extension binaries..."
112+
113+
for PHP_version in "${PHP_versions[@]}" ; do
114+
for libc_variant in "${libc_variants[@]}" ; do
115+
build_native_binaries_for_PHP_version_libc_variant "${PHP_version}" "${libc_variant}"
116+
done
117+
done
118+
119+
echo "Built extension binaries"
120+
}
121+
122+
is_earlier_major_minor_version() {
123+
local lhs_version="${1:?}"
124+
local rhs_version="${2:?}"
125+
local lhs_version_major
126+
lhs_version_major=$(echo "${lhs_version}" | cut -d. -f1)
127+
local rhs_version_major
128+
rhs_version_major=$(echo "${rhs_version}" | cut -d. -f1)
129+
130+
if [ "${lhs_version_major}" -lt "${rhs_version_major}" ]; then
131+
echo "true"
132+
return
133+
fi
134+
135+
if [ "${lhs_version_major}" -gt "${rhs_version_major}" ]; then
136+
echo "false"
137+
return
138+
fi
139+
140+
local lhs_version_minor
141+
lhs_version_minor=$(echo "${lhs_version}" | cut -d. -f2)
142+
local rhs_version_minor
143+
rhs_version_minor=$(echo "${rhs_version}" | cut -d. -f2)
144+
145+
if [ "${lhs_version_minor}" -lt "${rhs_version_minor}" ]; then
146+
echo "true"
147+
return
148+
fi
149+
150+
echo "false"
151+
}
152+
153+
select_composer_json_for_PHP_version() {
154+
local PHP_version="${1:?}"
155+
#
156+
# Supported instrumentations are different for PHP prior to 8.2 and for PHP 8.2 and later
157+
# because PHP 8.2 added ability to instrument internal functions
158+
#
159+
local is_PHP_version_before_8_2
160+
is_PHP_version_before_8_2=$(is_earlier_major_minor_version "${PHP_version}" "8.2")
161+
if [ "${is_PHP_version_before_8_2}" == "true" ]; then
162+
echo "composer_for_PHP_before_8.2.json"
163+
else
164+
echo "composer_for_PHP_8.2_and_later.json"
165+
fi
166+
}
167+
168+
download_PHP_packages_for_PHP_version() {
169+
local PHP_version="${1:?}"
170+
local dest_dir_for_current_args
171+
dest_dir_for_current_args="${destination_directory}/PHP_packages/PHP_${PHP_version}"
172+
173+
echo "Downloading PHP packages for PHP version: ${PHP_version} to ${dest_dir_for_current_args} ..."
174+
175+
ensure_dir_exists_and_empty "${dest_dir_for_current_args}"
176+
local composer_json_file_name
177+
composer_json_file_name=$(select_composer_json_for_PHP_version "${PHP_version}")
178+
local current_user_id
179+
current_user_id="$(id -u)"
180+
local current_user_group_id
181+
current_user_group_id="$(id -g)"
182+
docker run --rm \
183+
-v "${dest_dir_for_current_args}:/app/vendor" \
184+
-v "${PWD}/${composer_json_file_name}:/app/composer.json" \
185+
-w /app \
186+
"php:${PHP_version}"-cli sh -c "\
187+
apt-get update && apt-get install -y unzip \
188+
&& curl -sS https://getcomposer.org/installer | php -- --filename=composer --install-dir=/usr/local/bin \
189+
&& composer --ignore-platform-req=ext-opentelemetry --no-dev install \
190+
&& chown -R ${current_user_id}:${current_user_group_id} ./vendor/"
191+
192+
echo "Downloaded PHP packages for PHP version: ${PHP_version} to ${dest_dir_for_current_args}"
193+
}
194+
195+
download_PHP_packages() {
196+
echo "Downloading PHP packages..."
197+
198+
for PHP_version in "${PHP_versions[@]}" ; do
199+
download_PHP_packages_for_PHP_version "${PHP_version}"
200+
done
201+
202+
echo "Downloaded PHP packages"
203+
}
204+
205+
main() {
206+
parse_args "$@"
207+
208+
echo "Preparing files for docker image into directory ${destination_directory} ..."
209+
210+
ensure_dir_exists_and_empty "${destination_directory}"
211+
build_native_binaries
212+
download_PHP_packages
213+
214+
echo "Prepared files for docker image into directory ${destination_directory}"
215+
}
216+
217+
main "$@"

autoinstrumentation/php/version.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.1.0

versions.txt

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ autoinstrumentation-dotnet=1.2.0
3232
# Represents the current release of Go instrumentation.
3333
autoinstrumentation-go=v0.19.0-alpha
3434

35+
# Represents the current release of PHP instrumentation.
36+
# Should match autoinstrumentation/php/version.txt
37+
autoinstrumentation-PHP=1.1.0
38+
3539
# Represents the current release of Apache HTTPD instrumentation.
3640
# Should match autoinstrumentation/apache-httpd/version.txt
3741
autoinstrumentation-apache-httpd=1.0.4

0 commit comments

Comments
 (0)