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 Blackfire agent and full support #2448

Merged
merged 13 commits into from
Jan 14, 2021
1 change: 1 addition & 0 deletions .spellcheckwordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ bashrc
bcmath
be
belonging
blackfire
breakpoint
browser
busybox
Expand Down
56 changes: 56 additions & 0 deletions cmd/ddev/cmd/global_dotddev_assets/commands/web/blackfire
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash

#ddev-generated: Remove this line to take over this script
## Description: Enable or disable blackfire.io profiling
## Usage: blackfire start|stop|on|off|enable|disable|true|false|status
## Example: "ddev blackfire" (default is "on"), "ddev blackfire off", "ddev blackfire on", "ddev blackfire status"

function enable {
if [ -z ${BLACKFIRE_SERVER_ID} ] || [ -z ${BLACKFIRE_SERVER_TOKEN} ]; then
lolautruche marked this conversation as resolved.
Show resolved Hide resolved
echo "BLACKFIRE_SERVER_ID and BLACKFIRE_SERVER_TOKEN environment variables must be set" >&2
echo "See docs for how to set in global or project config" >&2
echo "For example, ddev config global --web-env=BLACKFIRE_SERVER_ID=<id>,BLACKFIRE_SERVER_TOKEN=<token>"
exit 1
fi
phpenmod blackfire
killall -HUP php-fpm
killall blackfire-agent 2>/dev/null
nohup blackfire-agent --log-level=4 >/tmp/blackfire_nohup.out 2>&1 &
lolautruche marked this conversation as resolved.
Show resolved Hide resolved
sleep 1
echo "Enabled blackfire PHP extension and started blackfire-agent"
exit
}
function disable {
phpdismod blackfire
killall -HUP php-fpm
killall blackfire-agent 2>/dev/null
echo "Disabled blackfire PHP extension and stopped blackfire-agent"
exit
}


if [ $# -eq 0 ] ; then
enable
fi

case $1 in
on|true|enable|start)
disable_xdebug
enable
;;
off|false|disable|stop)
disable
;;
status)
php --version | grep "with blackfire" >/dev/null 2>&1
phpstatus=$?
killall -0 blackfire-agent 2>/dev/null
agentstatus=$?
if [ ${phpstatus} -eq 0 ]; then echo "blackfire PHP extension enabled"; else echo "blackfire PHP extension disabled"; fi
if [ ${agentstatus} -eq 0 ]; then echo "blackfire agent running"; else echo "blackfire agent not running"; fi
lolautruche marked this conversation as resolved.
Show resolved Hide resolved
;;

*)
echo "Invalid argument: $1"
;;
esac
2 changes: 2 additions & 0 deletions cmd/ddev/cmd/packrd/packed-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions containers/ddev-webserver/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ RUN mkdir /tmp/ddev && \
MAILHOG_ARCH="linux_amd64"' > /tmp/ddev/vars; \
fi

# blackfire-php is not available for arm64 (yet)
RUN if [[ $TARGETPLATFORM != "linux/arm64" ]]; then \
DEBIAN_FRONTEND=noninteractive apt-get -qq install -o Dpkg::Options::="--force-confold" blackfire-php -y --allow-unauthenticated; \
fi
RUN DEBIAN_FRONTEND=noninteractive apt-get -qq install -o Dpkg::Options::="--force-confold" --no-install-recommends --no-install-suggests -y \
blackfire-agent \
blackfire-php \
fontconfig \
gettext \
git \
Expand Down Expand Up @@ -121,7 +119,7 @@ RUN mkdir -p /etc/nginx/sites-enabled /var/log/apache2 /var/run/apache2 /var/lib
RUN chmod -R 777 /var/log

# we need to create the /var/cache/linux and /var/lib/nginx manually for the arm64 image and chmod them, please don't remove them!
RUN mkdir -p /mnt/ddev-global-cache/mkcert /run/php /var/cache/nginx /var/lib/nginx && chmod -R ugo+rw /mnt/ddev-global-cache/
RUN mkdir -p /mnt/ddev-global-cache/mkcert /run/{php,blackfire} /var/cache/nginx /var/lib/nginx && chmod -R ugo+rw /mnt/ddev-global-cache/

RUN chmod -R ugo+w /usr/sbin /usr/bin /etc/nginx /var/cache/nginx /var/lib/nginx /run /var/www /etc/php/*/*/conf.d/ /var/lib/php/modules /etc/alternatives /usr/lib/node_modules /etc/php /etc/apache2 /var/log/apache2/ /var/run/apache2 /var/lib/apache2 /mnt/ddev-global-cache/*

Expand All @@ -133,6 +131,8 @@ RUN touch /var/log/nginx/error.log /var/log/nginx/access.log /var/log/php-fpm.lo
RUN a2dismod mpm_event
RUN a2enmod ssl headers expires

RUN phpdismod blackfire

# scripts added last because they're most likely place to make changes, speeds up build
ADD ddev-webserver-base-scripts /
RUN chmod ugo+x /start.sh /healthcheck.sh
Expand Down
1 change: 1 addition & 0 deletions containers/ddev-webserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This is a Dockerfile to build a container image for ddev's web container.
* [Composer](https://getcomposer.org/) (from the production container)
* [Drush](http://www.drush.org) (from the production container)
* [WP-CLI](http://www.wp-cli.org) (from the production container)
* [Blackfire CLI](https://blackfire.io/docs/cookbooks/profiling-http-via-cli)
* [Mailhog](https://github.com/mailhog/MailHog)
* npm
* yarn
Expand Down
25 changes: 25 additions & 0 deletions docs/users/blackfire-profiling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Profiling with Blackfire.io

DDEV-Local has built-in [blackfire.io](https://blackfire.io) integration.

### Basic Blackfire Usage

1. Create an account on [blackfire.io](https://blackfire.io)
2. Get the Server ID and the Server Token from your Account->Credentials on blackfire.io
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Server Credentials may come from an environment the user is being part of.
Client credentials should be retrieved as well (Account/Credentials)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don' t really understand "may come from an environment the user is being part of" - but we can link to docs if you can point me to it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Blackfire user may be part of an organization, and the latter may have one or several collaborative environments.
The easiest way is to use the personal credentials (the user inherit most of the org permissions), but doing so prevents the profiles to be shared within the team working in an environment.

See the documentation here: https://blackfire.io/docs/up-and-running/configuration/agent#configuring-the-agent-via-environment-variables. A user may have a dropdown menu like this:
image

3. Configure ddev with the credentials, `ddev config global --web-environment="BLACKFIRE_SERVER_ID=<id>,BLACKFIRE_SERVER_TOKEN=<token>"`. It's easiest to do this globally, but you can do the same thing with project-level `ddev config`.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that this command doesn't append but replaces the env vars to the global web_environment setting.
Is there a way to append variables instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The environment variable feature was just added in the previous PR (prompted by the need here, you used to have to do a docker-compose override), so I'm sure it will have some maturing to do. Most people just edit the appropriate configuration, the project .ddev/config.yaml or the global ~/.ddev/global_config.yaml - so that's the easiest way to just "edit" a set of env vars.

4. `ddev start`
5. `ddev blackfire on` to enable, `ddev blackfire off` to disable, `ddev blackfire status` to see status.
6. With blackfire enabled, you can use the [browser extension](https://blackfire.io/docs/profiling-cookbooks/profiling-http-via-browser) or [blackfire cli](https://blackfire.io/docs/profiling-cookbooks/profiling-http-via-cli) to profile.
rfay marked this conversation as resolved.
Show resolved Hide resolved

### Profiling with the Blackfire CLI

The blackfire CLI is built into the web container, but you still have to provide the client id and token.
rfay marked this conversation as resolved.
Show resolved Hide resolved

1. `ddev config global --web-environment="BLACKFIRE_SERVER_ID=<id>,BLACKFIRE_SERVER_TOKEN=<token>,BLACKFIRE_CLIENT_ID=<id>,BLACKFIRE_CLIENT_TOKEN=<token>"`
2. `ddev start`
3. Examples of using the blackfire CLI:

* `ddev exec blackfire curl https://<yoursite>.ddev.site`
* `ddev exec blackfire drush st
* `ddev exec blackfire curl https://<yoursite>.ddev.site`
* `ddev ssh` and then use the blackfire CLI as described in [Profiling HTTP Requests with the CLI](https://blackfire.io/docs/profiling-cookbooks/profiling-http-via-cli).
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pages:
- 'ddev Hooks': 'users/extending-commands.md'
- 'Using Developer Tools with ddev': 'users/developer-tools.md'
- 'PHP Step Debugging': 'users/step-debugging.md'
- 'Profiling with Blackfire': 'users/blackfire-profiling.md'
- 'Troubleshooting': 'users/troubleshooting.md'
- 'Docker Installation': 'users/docker_installation.md'
- 'Performance': 'users/performance.md'
Expand Down
2 changes: 1 addition & 1 deletion pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var DockerComposeFileFormatVersion = "3.6"
var WebImg = "drud/ddev-webserver"

// WebTag defines the default web image tag for drud dev
var WebTag = "20210107_hardened_drush" // Note that this can be overridden by make
var WebTag = "20210111_lolautruche_blackfire" // Note that this can be overridden by make

// DBImg defines the default db image used for applications.
var DBImg = "drud/ddev-dbserver"
Expand Down