Skip to content

Instantly share code, notes, and snippets.

@bennadel
Last active November 26, 2019 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/ba85c7d889fa3904afd1c8b3a4615aa4 to your computer and use it in GitHub Desktop.
Save bennadel/ba85c7d889fa3904afd1c8b3a4615aa4 to your computer and use it in GitHub Desktop.
Tracking Feature Flags In New Relic And NRQL Using The Java Agent In Lucee CFML 5.3.3.62
SELECT
average( duration )
FROM
Transaction
WHERE
appName = 'local-cfprojects-bennadel'
AND
resourceUri = '/d/ben/default'
SINCE
10 minutes ago
FACET
features.DemoOptimization
TIMESERIES
SELECT
average( duration )
FROM
Transaction
WHERE
appName = 'local-cfprojects-bennadel'
AND
resourceUri = '/d/ben/default'
AND
features.DemoOptimization = 'false'
SINCE
10 minutes ago
TIMESERIES
SELECT
average( duration )
FROM
Transaction
WHERE
appName = 'local-cfprojects-bennadel'
AND
resourceUri = '/d/ben/default'
AND
features.DemoOptimization = 'true'
SINCE
10 minutes ago
TIMESERIES
component
output = false
accessors = true
{
// Define properties for dependency-injection.
property javaAgentHelper;
// ---
// PUBLIC METHODS.
// ---
/**
* I render the demo for the New Relic feature-flag tracking.
*
* @rc I am the FW/1 request context.
*/
public void function default( required struct rc ) {
var isFeatureEnabled = shouldUseFeature( userID = 1 );
// Now that we know if the feature-flag is enabled, we're going to track the
// feature flag state as a TRANSACTION PARAMETER using the New Relic Java Agent.
// This way, we can differentiate requests by feature-flag in our Transaction
// traces and NRQL (New Relic Query Language) queries.
javaAgentHelper.addCustomParameter(
"features.DemoOptimization",
booleanFormat( isFeatureEnabled )
);
// DEMO LOGIC: For the purposes of the demo, we're going to randomly assign
// better performance to the branch with the feature enabled. This way, we
// should be able to see a difference in New Relic's Insights dashboards.
if ( isFeatureEnabled ) {
sleep( randRange( 50, 150 ) );
} else {
sleep( randRange( 100, 1000 ) );
}
}
// ---
// PRIVATE METHODS.
// ---
/**
* I determine if the feature should be enabled for the given user.
*
* @userID I am the user being tested.
*/
private boolean function shouldUseFeature( required numeric userID ) {
// FOR THE DEMO, we are going to randomly enable or disable the given feature.
// This way, we can see the difference in the New Relic transactions.
// --
// NOTE: This is the logical equivalent of a 50% feature roll-out.
return( !! randRange( 0, 1 ) );
}
}
SELECT
count( * )
FROM
Transaction
WHERE
appName = 'local-cfprojects-bennadel'
AND
resourceUri = '/d/ben/default'
SINCE
10 minutes ago
FACET
features.DemoOptimization
TIMESERIES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment