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

Trouble getting aws-xray running with Dockerized bedrock based WP instance #102

Open
colmtroy opened this issue Sep 1, 2024 · 3 comments

Comments

@colmtroy
Copy link

colmtroy commented Sep 1, 2024

Howdy folks,
I really enjoyed your recent video!

I struggled to get this plugin up and running in my local dev environment and have a few questions.

Here are the steps I took to get things up and running. My WP directory structure is based on Bedrock

  1. installed aws-xray by running composer require humanmade/aws-xray which adds the plugin to /vendor/humanmade/aws-xray
  2. Added an mu-plugin to load the plugin

A few things of note below.

  • mu-plugins exists in /web/app/mu-plugins with Bedrock - hence why the base path travels up 3 directories
  • I manually called namespace.php and plugin.php based on the very useful feedback provided by @mikelittle in Usage with local docker environment #101
  • given the plugin enqueues flamegraph.js using plugin_dir_url i'm confused as to how loading the plugin via composer in the vendor directory would ever fully work?
wp_enqueue_script( 'aws-xray-flamegraph', plugin_dir_url( dirname( __FILE__, 2 ) ) . 'assets/flamegraph.js', [], '2022-10-10' );

I ended up proxying those requests in my mu-plugin to save having to manually copy flamegraph.js into the plugins dir - crude - but it works :)

<?php
/**
 * Plugin Name: Load AWS X-Ray
 * Plugin URI: SNIP
 * Description: An autoloader that enables AWS X-Ray tracing for WordPress.
 * Version: 1.0.3
 * Author: Colm Troy
 * Author URI: https://www.commercegurus.com
 * License: GPLv2
 */

$has_profiler = class_exists( 'ExcimerProfiler' ) || function_exists( 'xhprof_sample_enable' );
if ( $has_profiler ) {
	$base_path = realpath( __DIR__ . '/../../../vendor/humanmade/aws-xray' );

	if ( ! $base_path ) {
		error_log( 'AWS X-Ray base path not found. Expected path: ' . __DIR__ . '/../../../vendor/humanmade/aws-xray' );
		return;
	}

	$namespace_file = $base_path . '/inc/namespace.php';
	$plugin_file    = $base_path . '/plugin.php';

	if ( file_exists( $namespace_file ) ) {
		require_once $namespace_file;
	} else {
		error_log( 'Namespace file does not exist' );
		return;
	}

	if ( file_exists( $plugin_file ) ) {
		require_once $plugin_file;
	} else {
		error_log( 'Plugin file does not exist' );
		return;
	}

	if ( function_exists( '\HM\Platform\XRay\bootstrap' ) ) {
		\HM\Platform\XRay\bootstrap();
	} else {
		error_log( 'AWS X-Ray bootstrap function not found' );
	}

	if ( function_exists( '\HM\Platform\XRay\Query_Monitor\bootstrap' ) ) {
		\HM\Platform\XRay\Query_Monitor\bootstrap();
	} else {
		error_log( 'AWS X-Ray Query Monitor bootstrap function not found' );
	}

	// Serve flamegraph.js file
	add_action(
		'init',
		function () use ( $base_path ) {
			if ( isset( $_GET['aws_xray_flamegraph'] ) ) {
				$flamegraph_path = $base_path . '/assets/flamegraph.js';
				if ( file_exists( $flamegraph_path ) ) {
					header( 'Content-Type: application/javascript' );
					readfile( $flamegraph_path );
					exit;
				}
			}
		}
	);

	// Enqueue the flamegraph.js file
	add_action(
		'wp_enqueue_scripts',
		function () {
			wp_enqueue_script(
				'aws-xray-flamegraph',
				add_query_arg( 'aws_xray_flamegraph', '1', home_url( '/' ) ),
				array(),
				'1.0.3',
				true
			);
		}
	);

	// Enqueue for admin pages as well
	add_action(
		'admin_enqueue_scripts',
		function () {
			wp_enqueue_script(
				'aws-xray-flamegraph',
				add_query_arg( 'aws_xray_flamegraph', '1', home_url( '/' ) ),
				array(),
				'1.0.3',
				true
			);
		}
	);
}

So I guess my main question is, is there a proper install guide for this plugin? I'm sure I probably didn't need to jump through the hoops above to get this up and running.

Thanks.

@joehoyle
Copy link
Member

joehoyle commented Oct 1, 2024

In Altis we filter plugin_dir_url to return a /vendor/ path if the plugin is in a given allow-list I think, that's how the URL is built.

Sorry the docs are a bit light on using it outside of hte Altis Local Server context

@mikelittle
Copy link
Contributor

The other thing to note is that we also serve static assets directly from the /vendor folder with nginx. So the flamegraph.js works from there without needing to jump through any hoops.

@colmtroy
Copy link
Author

Aha! thanks @joehoyle @mikelittle for the follow up - that at least explains I wasn't going crazy :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants