use of com.amazonaws.xray.plugins.Plugin in project aws-xray-sdk-java by aws.
the class AWSXRayRecorderTest method testPluginEquality.
@Test
public void testPluginEquality() {
Collection<Plugin> plugins = new HashSet<>();
plugins.add(new EC2Plugin());
// should be deduped
plugins.add(new EC2Plugin());
plugins.add(new ECSPlugin());
plugins.add(new EKSPlugin());
plugins.add(new ElasticBeanstalkPlugin());
Assert.assertEquals(4, plugins.size());
}
use of com.amazonaws.xray.plugins.Plugin in project aws-xray-sdk-java by aws.
the class AWSXRayRecorderTest method testOriginResolutionWithAllPlugins.
@Test
public void testOriginResolutionWithAllPlugins() {
// given
EC2Plugin ec2Plugin = Mockito.mock(EC2Plugin.class);
ECSPlugin ecsPlugin = Mockito.mock(ECSPlugin.class);
ElasticBeanstalkPlugin ebPlugin = Mockito.mock(ElasticBeanstalkPlugin.class);
EKSPlugin eksPlugin = Mockito.mock(EKSPlugin.class);
List<Plugin> plugins = new ArrayList<>();
plugins.add(ec2Plugin);
plugins.add(ecsPlugin);
plugins.add(ebPlugin);
plugins.add(eksPlugin);
List<String> origins = new ArrayList<>();
origins.add(EC2Plugin.ORIGIN);
origins.add(ECSPlugin.ORIGIN);
origins.add(ElasticBeanstalkPlugin.ORIGIN);
origins.add(EKSPlugin.ORIGIN);
Map<String, Object> runtimeContext = new HashMap<>();
runtimeContext.put("key", "value");
for (int i = 0; i < 4; i++) {
Mockito.doReturn(true).when(plugins.get(i)).isEnabled();
Mockito.doReturn(runtimeContext).when(plugins.get(i)).getRuntimeContext();
Mockito.doReturn("serviceName").when(plugins.get(i)).getServiceName();
Mockito.doReturn(origins.get(i)).when(plugins.get(i)).getOrigin();
}
AWSXRayRecorder recorder = AWSXRayRecorderBuilder.standard().withPlugin(ec2Plugin).withPlugin(ecsPlugin).withPlugin(ebPlugin).withPlugin(eksPlugin).build();
// when
Assert.assertEquals(ElasticBeanstalkPlugin.ORIGIN, recorder.getOrigin());
}
use of com.amazonaws.xray.plugins.Plugin in project aws-xray-sdk-java by aws.
the class AWSXRayRecorderBuilder method build.
/**
* Constructs and returns an AWSXRayRecorder with the provided configuration.
*
* @return a configured instance of AWSXRayRecorder
*/
public AWSXRayRecorder build() {
AWSXRayRecorder client = new AWSXRayRecorder();
if (samplingStrategy != null) {
client.setSamplingStrategy(samplingStrategy);
}
if (streamingStrategy != null) {
client.setStreamingStrategy(streamingStrategy);
}
if (prioritizationStrategy != null) {
client.setPrioritizationStrategy(prioritizationStrategy);
}
if (throwableSerializationStrategy != null) {
client.setThrowableSerializationStrategy(throwableSerializationStrategy);
}
ContextMissingStrategy contextMissingStrategy = this.contextMissingStrategy;
if (contextMissingStrategy != null && !AWSXRayRecorderBuilder.contextMissingStrategyFromEnvironmentVariable().isPresent() && !AWSXRayRecorderBuilder.contextMissingStrategyFromSystemProperty().isPresent()) {
client.setContextMissingStrategy(contextMissingStrategy);
}
if (segmentContextResolverChain != null) {
client.setSegmentContextResolverChain(segmentContextResolverChain);
}
if (emitter != null) {
client.setEmitter(emitter);
}
if (!segmentListeners.isEmpty()) {
client.addAllSegmentListeners(segmentListeners);
}
if (useFastIdGenerator) {
client.useFastIdGenerator();
} else {
client.useSecureIdGenerator();
}
if (forcedTraceIdGeneration) {
client.setForcedTraceIdGeneration(true);
}
plugins.stream().filter(Objects::nonNull).filter(p -> p.isEnabled()).forEach(plugin -> {
logger.info("Collecting trace metadata from " + plugin.getClass().getName() + ".");
try {
Map<String, @Nullable Object> runtimeContext = plugin.getRuntimeContext();
if (!runtimeContext.isEmpty()) {
client.putRuntimeContext(plugin.getServiceName(), runtimeContext);
/**
* Given several enabled plugins, the recorder should resolve a single one that's most representative of this
* environment
* Resolution order: EB > EKS > ECS > EC2
* EKS > ECS because the ECS plugin checks for an environment variable whereas the EKS plugin checks for a
* kubernetes authentication file, which is a stronger enable condition
*/
String clientOrigin = client.getOrigin();
if (clientOrigin == null || ORIGIN_PRIORITY.getOrDefault(plugin.getOrigin(), 0) < ORIGIN_PRIORITY.getOrDefault(clientOrigin, 0)) {
client.setOrigin(plugin.getOrigin());
}
} else {
logger.warn(plugin.getClass().getName() + " plugin returned empty runtime context data. The recorder will " + "not be setting segment origin or runtime context values from this plugin.");
}
} catch (Exception e) {
logger.warn("Failed to get runtime context from " + plugin.getClass().getName() + ".", e);
}
try {
Set<AWSLogReference> logReferences = plugin.getLogReferences();
if (Objects.nonNull(logReferences)) {
if (!logReferences.isEmpty()) {
client.addAllLogReferences(logReferences);
} else {
logger.debug(plugin.getClass().getName() + " plugin returned empty Log References. The recorder will not " + "reflect the logs from this plugin.");
}
}
} catch (Exception e) {
logger.warn("Failed to get log references from " + plugin.getClass().getName() + ".", e);
}
});
String logGroupFromEnv = System.getenv(LOG_GROUP_KEY);
if (StringValidator.isNotNullOrBlank(logGroupFromEnv)) {
logger.info("Recording log group " + logGroupFromEnv + " from environment variable.");
AWSLogReference logReference = new AWSLogReference();
logReference.setLogGroup(logGroupFromEnv);
client.addAllLogReferences(Collections.singleton(logReference));
}
return client;
}
Aggregations