Skip to content

Commit

Permalink
Merge pull request #4 from realFlowControl/installer
Browse files Browse the repository at this point in the history
Use `datadog-setup.php` installer
  • Loading branch information
josephwynn-sc authored Jun 21, 2023
2 parents 01a8b78 + 8657478 commit 1ec945d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 49 deletions.
27 changes: 7 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,14 @@ To add this buildpack to your project, run the following:
heroku buildpacks:add --app <your-app-name> https://github.com/SpeedCurve-Metrics/heroku-buildpack-php-ddtrace.git
```

Make sure to add this buildpack after the PHP buildpack, as a PHP binary is
needed to run the installer.

## Configuration

This buildpack accepts several config vars. All of them are optional:

- `DDTRACE_EXT_PHP_API` - The PHP API version that your application uses. Defaults to `20210902`. Use `phpinfo()` to find the API version if you're not sure.
- `DDTRACE_EXT_RELEASE` - The release name of [dd-trace-php](https://github.com/DataDog/dd-trace-php/releases/) to download. Defaults to `0.82.0`.
- `DDTRACE_EXT_PKG_URL` - The URL to a dd-trace-php `.deb` file. This option overides `DDTRACE_EXT_RELEASE`.

### More information: `DDTRACE_EXT_PHP_API`

This buildpack does not determine that PHP API version automatically. The default value should work for most PHP versions, but if the version is wrong then you will see warnings like this in your application logs:

```
PHP Warning: PHP Startup: ddtrace: Unable to initialize module
Module compiled with module API=20210902
PHP compiled with module API=20220829
These options need to match
```

You can change the version by setting the `DDTRACE_EXT_PHP_API` config var in Heroku. For example:

```
heroku config:set --app <your-app-name> DDTRACE_EXT_PHP_API=20220829
```
- `DD_RELEASE` - The release name of [dd-trace-php](https://github.com/DataDog/dd-trace-php/releases/) to download. Defaults to `0.87.2`.
- `DD_INSTALLER_URL` - The URL to the `datadog-setup.php` to be used (to be found on the [dd-trace-php release page](https://github.com/DataDog/dd-trace-php/releases/)).
- `DD_PROFILING_ENABLED` - Set to a non empty value, this will install and enable the profiling extension.
- `DD_APPSEC_ENABLED` - Set to a non empty value, this will install and enable the appsec extension.
78 changes: 50 additions & 28 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -37,48 +37,70 @@ export_env_dir() {
fi
}

export_env_dir "$ENV_DIR" '^DDTRACE_EXT_'
export_env_dir "$ENV_DIR" '^DD_|DDTRACE_'

topic "Preparing to install ddtrace PHP extension"
topic "Preparing to install Datadog PHP extensions"

if ! which php >/dev/null; then
error "PHP is not installed. Please add a PHP buildpack to your app before adding this buildpack."
fi

# Warn folks about
if [[ -n ${DDTRACE_EXT_PKG_URL:-} ]]; then
echo "Using package URL '$DDTRACE_EXT_PKG_URL' (from "'$DDTRACE_EXT_PKG_URL env var).' | indent
error "The environment variable \$DDTRACE_EXT_PKG_URL has been renamed to \$DD_INSTALLER_URL and it needs to be a URL to the desired 'datadog-setup.php' to be found at https://github.com/DataDog/dd-trace-php/releases"
fi

if [[ -n ${DDTRACE_EXT_RELEASE:-} ]]; then
echo "The environment variable \$DDTRACE_EXT_RELEASE got renamed to \$DD_RELEASE, rewriting it for now, please update your environment variable." | indent
DD_RELEASE=$DDTRACE_EXT_RELEASE
fi

if [[ -n ${DDTRACE_EXT_PHP_API} ]]; then
echo "The environment variable \$DDTRACE_EXT_PHP_API is superfluous and will be ignored, feel free to remove it." | indent
fi

if [[ -n ${DD_INSTALLER_URL:-} ]]; then
echo "Using package URL '$DD_INSTALLER_URL' (from "'$DD_INSTALLER_URL env var).' | indent
else
if [[ -n ${DDTRACE_EXT_RELEASE:-} ]]; then
echo "Using dd-trace-php release '$DDTRACE_EXT_RELEASE' (from "'$DDTRACE_EXT_RELEASE env var).' | indent
if [[ -n ${DD_RELEASE:-} ]]; then
echo "Using dd-trace-php release '$DD_RELEASE' (from "'$DD_RELEASE env var).' | indent
else
export DDTRACE_EXT_RELEASE="0.82.0"
echo "Using dd-trace-php release '$DDTRACE_EXT_RELEASE' (default value)." | indent
export DD_RELEASE="0.87.2"
echo "Using dd-trace-php release '$DD_RELEASE' (default value)." | indent
fi
export DDTRACE_EXT_PKG_URL="https://github.com/DataDog/dd-trace-php/releases/download/"$DDTRACE_EXT_RELEASE"/datadog-php-tracer_"$DDTRACE_EXT_RELEASE"_amd64.deb"
export DD_INSTALLER_URL="https://github.com/DataDog/dd-trace-php/releases/download/"$DD_RELEASE"/datadog-setup.php"
fi

APT_DIR="$BUILD_DIR/.apt"
APT_CACHE_DIR="$CACHE_DIR/apt/cache"
PACKAGE_NAME="dd-php-tracer"
PACKAGE_FILE=$APT_CACHE_DIR/archives/$PACKAGE_NAME.deb
if [[ "$DD_INSTALLER_URL" != *datadog-setup.php ]]
then
error "The package URL is out of date. Please use the 'datadog-setup.php' file URL from the releases at https://github.com/DataDog/dd-trace-php/releases"
fi

mkdir -p $APT_CACHE_DIR/archives/partial
mkdir -p $BUILD_DIR/.apt
topic "Downloading installer from $DD_INSTALLER_URL"
curl --silent --show-error --fail --location --output datadog-setup.php $DD_INSTALLER_URL 2>&1 | indent

topic "Downloading package from $DDTRACE_EXT_PKG_URL"
curl --silent --show-error --fail --location --output $PACKAGE_FILE $DDTRACE_EXT_PKG_URL 2>&1 | indent
topic "Running Datadog installer"

topic "Installing ddtrace PHP extension"
dpkg -x $PACKAGE_FILE $BUILD_DIR/.apt/
OPTS=""

topic "Enabling ddtrace PHP extension"
if [[ -n ${DD_PROFILING_ENABLED:-} ]];
then
OPTS+=" --enable-profiling"
echo "Profiling will be enabled" | indent
fi

if [[ -n ${DDTRACE_EXT_PHP_API:-} ]]; then
echo "Using version '$DDTRACE_EXT_PHP_API' (from "'$DDTRACE_EXT_PHP_API env var).' | indent
else
export DDTRACE_EXT_PHP_API="20210902"
echo "Using version '$DDTRACE_EXT_PHP_API' (default value)." | indent
if [[ -n ${DD_APPSEC_ENABLED:-} ]];
then
OPTS+=" --enable-appsec"
echo "Appsec will be enabled" | indent
fi

touch /app/.heroku/php/etc/php/conf.d/datadog-php-custom.ini
echo "[PHP]" >> /app/.heroku/php/etc/php/conf.d/datadog-php-custom.ini
echo "extension=/app/.apt/opt/datadog-php/extensions/ddtrace-$DDTRACE_EXT_PHP_API.so" >> /app/.heroku/php/etc/php/conf.d/datadog-php-custom.ini
echo "ddtrace.request_init_hook=/app/.apt/opt/datadog-php/dd-trace-sources/bridge/dd_wrap_autoloader.php" >> /app/.heroku/php/etc/php/conf.d/datadog-php-custom.ini
mkdir -p $BUILD_DIR/opt

env -u PHP_INI_SCAN_DIR php datadog-setup.php --php-bin=all --install-dir=$BUILD_DIR/opt $OPTS 2>&1 | indent

topic "Validating installation"

env -u PHP_INI_SCAN_DIR php -v 2>&1 | indent

echo "Done" | indent
2 changes: 1 addition & 1 deletion bin/detect
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash

echo "ddtrace PHP extension"
echo "Datadog PHP extensions"
exit 0

0 comments on commit 1ec945d

Please sign in to comment.