Skip to content

Commit

Permalink
Ability to filters samplers (list and regex) (#8)
Browse files Browse the repository at this point in the history
Signed-off-by: Adrián Moreno <[email protected]>
  • Loading branch information
adrianmo authored May 8, 2020
1 parent cef70cc commit d7070e2
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ Then, in the Parameters table, configure the following attributes.
| *instrumentationKey* | The Instrumentation Key of your Application Insights instance | Yes |
| *testName* | Name of the test. This value is used to differentiate metrics across test runs or plans in Application Insights and allow you to filter them. | Yes |
| *liveMetrics* | Boolean to indicate whether or not real-time metrics are enabled and available in the [Live Metrics Stream](https://docs.microsoft.com/en-us/azure/azure-monitor/app/live-stream). Defaults to `true`. | No |
| *samplersList* | Optional list of samplers separated by a semi-colon (`;`) that the listener will collect and send metrics to Application Insights. If the list is empty, the listener will not filter samplers and send metrics from all of them. Defaults to an empty string. | No |
| *useRegexForSamplerList* | If set to `true` the `samplersList` will be evaluated as a regex to filter samplers. Defaults to `false`. | No |


*Example of configuration:*

Expand Down
Binary file modified docs/configuration.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.adrianmo</groupId>
<artifactId>jmeter.backendlistener.azure</artifactId>
<version>0.2.1</version>
<version>0.2.2</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>A JMeter plug-in that enables you to send test results to Azure Monitor.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,74 @@
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient;
import org.apache.jmeter.visualizers.backend.BackendListenerContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AzureBackendClient extends AbstractBackendListenerClient {

/**
* Logger.
*/
private static final Logger log = LoggerFactory.getLogger(AzureBackendClient.class);

/**
* Argument keys.
*/
private static final String KEY_TEST_NAME = "testName";
private static final String KEY_INSTRUMENTATION_KEY = "instrumentationKey";
private static final String KEY_LIVE_METRICS = "liveMetrics";
private static final String KEY_SAMPLERS_LIST = "samplersList";
private static final String KEY_USE_REGEX_FOR_SAMPLER_LIST = "useRegexForSamplerList";

/**
* Default argument values.
*/
private static final String DEFAULT_TEST_NAME = "jmeter";
private static final String DEFAULT_INSTRUMENTATION_KEY = "";
private static final boolean DEFAULT_LIVE_METRICS = true;
private static final String DEFAULT_SAMPLERS_LIST = "";
private static final boolean DEFAULT_USE_REGEX_FOR_SAMPLER_LIST = false;

/**
* Separator for samplers list.
*/
private static final String SEPARATOR = ";";

/**
* Application Insights telemetry client.
*/
private TelemetryClient telemetryClient;
private static final String TEST_NAME = "testName";
private static final String INSTRUMENTATION_KEY = "instrumentationKey";
private static final String LIVE_METRICS = "liveMetrics";

/**
* Name of the test.
*/
private String testName;

/**
* Whether to send metrics to the Live Metrics Stream.
*/
private boolean liveMetrics;

/**
* List of samplers to record.
*/
private String samplersList = "";

/**
* Regex if samplers are defined through regular expression.
*/
private Boolean useRegexForSamplerList;

/**
* Set of samplers to record.
*/
private Set<String> samplersToFilter;

public AzureBackendClient() {
super();
Expand All @@ -31,23 +87,37 @@ public AzureBackendClient() {
@Override
public Arguments getDefaultParameters() {
Arguments arguments = new Arguments();
arguments.addArgument(TEST_NAME, "jmeter");
arguments.addArgument(INSTRUMENTATION_KEY, "");
arguments.addArgument(LIVE_METRICS, "true");
arguments.addArgument(KEY_TEST_NAME, DEFAULT_TEST_NAME);
arguments.addArgument(KEY_INSTRUMENTATION_KEY, DEFAULT_INSTRUMENTATION_KEY);
arguments.addArgument(KEY_LIVE_METRICS, Boolean.toString(DEFAULT_LIVE_METRICS));
arguments.addArgument(KEY_SAMPLERS_LIST, DEFAULT_SAMPLERS_LIST);
arguments.addArgument(KEY_USE_REGEX_FOR_SAMPLER_LIST, Boolean.toString(DEFAULT_USE_REGEX_FOR_SAMPLER_LIST));

return arguments;
}

@Override
public void setupTest(BackendListenerContext context) throws Exception {
boolean liveMetrics = context.getBooleanParameter(LIVE_METRICS, true);
testName = context.getParameter(KEY_TEST_NAME, DEFAULT_TEST_NAME);
liveMetrics = context.getBooleanParameter(KEY_LIVE_METRICS, DEFAULT_LIVE_METRICS);
samplersList = context.getParameter(KEY_SAMPLERS_LIST, DEFAULT_SAMPLERS_LIST).trim();
useRegexForSamplerList = context.getBooleanParameter(KEY_USE_REGEX_FOR_SAMPLER_LIST, DEFAULT_USE_REGEX_FOR_SAMPLER_LIST);

TelemetryConfiguration config = TelemetryConfiguration.createDefault();
config.setInstrumentationKey(context.getParameter(INSTRUMENTATION_KEY));
config.setInstrumentationKey(context.getParameter(KEY_INSTRUMENTATION_KEY));
telemetryClient = new TelemetryClient(config);
if (liveMetrics) {
QuickPulse.INSTANCE.initialize(config);
}
super.setupTest(context);

samplersToFilter = new HashSet<String>();
if (!useRegexForSamplerList) {
String[] samplers = samplersList.split(SEPARATOR);
samplersToFilter = new HashSet<String>();
for (String samplerName : samplers) {
samplersToFilter.add(samplerName);
}
}
}

private void trackRequest(String name, SampleResult sr) {
Expand Down Expand Up @@ -84,13 +154,23 @@ private void trackRequest(String name, SampleResult sr) {

@Override
public void handleSampleResults(List<SampleResult> results, BackendListenerContext context) {

boolean samplersToFilterMatch;
for (SampleResult sr : results) {
trackRequest(context.getParameter(TEST_NAME), sr);

samplersToFilterMatch = samplersList.isEmpty() ||
(useRegexForSamplerList && sr.getSampleLabel().matches(samplersList)) ||
(!useRegexForSamplerList && samplersToFilter.contains(sr.getSampleLabel()));

if (samplersToFilterMatch) {
trackRequest(testName, sr);
}
}
}

@Override
public void teardownTest(BackendListenerContext context) throws Exception {
samplersToFilter.clear();
telemetryClient.flush();
super.teardownTest(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.internal.util.reflection.Whitebox;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static junit.framework.TestCase.fail;
Expand All @@ -34,6 +36,8 @@ public void setUp() {
Arguments args = new Arguments();
args.addArgument("testName", "test-1");
context = new BackendListenerContext(args);
Whitebox.setInternalState(client, "testName", "test-1");
Whitebox.setInternalState(client, "samplersToFilter", new HashSet<>());
}

@Test
Expand Down

0 comments on commit d7070e2

Please sign in to comment.