Search in sources :

Example 1 with AWSLogReference

use of com.amazonaws.xray.entities.AWSLogReference in project aws-xray-sdk-java by aws.

the class ECSPlugin method populateLogReferences.

// See: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-account-settings.html#ecs-resource-ids
private void populateLogReferences() {
    String logGroup = containerMetadata.get(ECSMetadataFetcher.ECSContainerMetadata.LOG_GROUP_NAME);
    if (logGroup == null) {
        return;
    }
    AWSLogReference logReference = new AWSLogReference();
    logReference.setLogGroup(logGroup);
    String logRegion = containerMetadata.get(ECSMetadataFetcher.ECSContainerMetadata.LOG_GROUP_REGION);
    String containerArn = containerMetadata.get(ECSMetadataFetcher.ECSContainerMetadata.CONTAINER_ARN);
    String logAccount = containerArn != null ? containerArn.split(":")[4] : null;
    if (logRegion != null && logAccount != null) {
        logReference.setArn("arn:aws:logs:" + logRegion + ":" + logAccount + ":log-group:" + logGroup);
    }
    logReferences.add(logReference);
}
Also used : AWSLogReference(com.amazonaws.xray.entities.AWSLogReference)

Example 2 with AWSLogReference

use of com.amazonaws.xray.entities.AWSLogReference in project aws-xray-sdk-java by aws.

the class EKSPluginTest method testGenerationOfLogGroupName.

@Test
public void testGenerationOfLogGroupName() {
    Set<AWSLogReference> references = plugin.getLogReferences();
    AWSLogReference reference = (AWSLogReference) references.toArray()[0];
    assertEquals(TEST_LOG_GROUP, reference.getLogGroup());
}
Also used : AWSLogReference(com.amazonaws.xray.entities.AWSLogReference) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with AWSLogReference

use of com.amazonaws.xray.entities.AWSLogReference in project aws-xray-sdk-java by aws.

the class ECSPluginTest method testLogGroupRecording.

@Test
void testLogGroupRecording() {
    Map<ECSMetadataFetcher.ECSContainerMetadata, String> containerMetadata = new HashMap<>();
    containerMetadata.put(ECSMetadataFetcher.ECSContainerMetadata.LOG_GROUP_REGION, "us-west-2");
    containerMetadata.put(ECSMetadataFetcher.ECSContainerMetadata.LOG_GROUP_NAME, "my-log-group");
    containerMetadata.put(ECSMetadataFetcher.ECSContainerMetadata.CONTAINER_ARN, "arn:aws:ecs:us-west-2:123456789012:container-instance/my-cluster/12345");
    when(mockFetcher.fetchContainer()).thenReturn(containerMetadata);
    ECSPlugin plugin = new ECSPlugin(mockFetcher);
    Set<AWSLogReference> references = plugin.getLogReferences();
    AWSLogReference expected = new AWSLogReference();
    expected.setLogGroup("my-log-group");
    expected.setArn("arn:aws:logs:us-west-2:123456789012:log-group:my-log-group");
    assertThat(references).containsOnly(expected);
}
Also used : HashMap(java.util.HashMap) AWSLogReference(com.amazonaws.xray.entities.AWSLogReference) Test(org.junit.jupiter.api.Test)

Example 4 with AWSLogReference

use of com.amazonaws.xray.entities.AWSLogReference in project aws-xray-sdk-java by aws.

the class AWSXRayRecorderTest method testLogGroupFromEnvironment.

@Test
public void testLogGroupFromEnvironment() {
    environmentVariables.set("AWS_LOG_GROUP", "my-group");
    AWSXRayRecorder recorder = AWSXRayRecorderBuilder.standard().build();
    Segment segment = recorder.beginSegment("test");
    AWSLogReference expected = new AWSLogReference();
    expected.setLogGroup("my-group");
    assertThat(segment.getAws()).containsKey("cloudwatch_logs");
    Set<AWSLogReference> logReferences = (Set<AWSLogReference>) segment.getAws().get("cloudwatch_logs");
    assertThat(logReferences).containsOnly(expected);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) AWSLogReference(com.amazonaws.xray.entities.AWSLogReference) Segment(com.amazonaws.xray.entities.Segment) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with AWSLogReference

use of com.amazonaws.xray.entities.AWSLogReference 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;
}
Also used : HashMap(java.util.HashMap) ECSPlugin(com.amazonaws.xray.plugins.ECSPlugin) StreamingStrategy(com.amazonaws.xray.strategy.StreamingStrategy) Plugin(com.amazonaws.xray.plugins.Plugin) ArrayList(java.util.ArrayList) RuntimeErrorContextMissingStrategy(com.amazonaws.xray.strategy.RuntimeErrorContextMissingStrategy) HashSet(java.util.HashSet) IgnoreErrorContextMissingStrategy(com.amazonaws.xray.strategy.IgnoreErrorContextMissingStrategy) Map(java.util.Map) StringValidator(com.amazonaws.xray.entities.StringValidator) SegmentListener(com.amazonaws.xray.listeners.SegmentListener) Emitter(com.amazonaws.xray.emitters.Emitter) Nullable(org.checkerframework.checker.nullness.qual.Nullable) SamplingStrategy(com.amazonaws.xray.strategy.sampling.SamplingStrategy) Set(java.util.Set) SegmentContextResolverChain(com.amazonaws.xray.contexts.SegmentContextResolverChain) PrioritizationStrategy(com.amazonaws.xray.strategy.PrioritizationStrategy) ContextMissingStrategy(com.amazonaws.xray.strategy.ContextMissingStrategy) EKSPlugin(com.amazonaws.xray.plugins.EKSPlugin) Objects(java.util.Objects) List(java.util.List) ElasticBeanstalkPlugin(com.amazonaws.xray.plugins.ElasticBeanstalkPlugin) ThrowableSerializationStrategy(com.amazonaws.xray.strategy.ThrowableSerializationStrategy) Optional(java.util.Optional) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) LogErrorContextMissingStrategy(com.amazonaws.xray.strategy.LogErrorContextMissingStrategy) Collections(java.util.Collections) AWSLogReference(com.amazonaws.xray.entities.AWSLogReference) EC2Plugin(com.amazonaws.xray.plugins.EC2Plugin) AWSLogReference(com.amazonaws.xray.entities.AWSLogReference) Objects(java.util.Objects) RuntimeErrorContextMissingStrategy(com.amazonaws.xray.strategy.RuntimeErrorContextMissingStrategy) IgnoreErrorContextMissingStrategy(com.amazonaws.xray.strategy.IgnoreErrorContextMissingStrategy) ContextMissingStrategy(com.amazonaws.xray.strategy.ContextMissingStrategy) LogErrorContextMissingStrategy(com.amazonaws.xray.strategy.LogErrorContextMissingStrategy)

Aggregations

AWSLogReference (com.amazonaws.xray.entities.AWSLogReference)7 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 SegmentContextResolverChain (com.amazonaws.xray.contexts.SegmentContextResolverChain)1 Emitter (com.amazonaws.xray.emitters.Emitter)1 Segment (com.amazonaws.xray.entities.Segment)1 StringValidator (com.amazonaws.xray.entities.StringValidator)1 SegmentListener (com.amazonaws.xray.listeners.SegmentListener)1 EC2Plugin (com.amazonaws.xray.plugins.EC2Plugin)1 ECSPlugin (com.amazonaws.xray.plugins.ECSPlugin)1 EKSPlugin (com.amazonaws.xray.plugins.EKSPlugin)1 ElasticBeanstalkPlugin (com.amazonaws.xray.plugins.ElasticBeanstalkPlugin)1 Plugin (com.amazonaws.xray.plugins.Plugin)1 ContextMissingStrategy (com.amazonaws.xray.strategy.ContextMissingStrategy)1 IgnoreErrorContextMissingStrategy (com.amazonaws.xray.strategy.IgnoreErrorContextMissingStrategy)1 LogErrorContextMissingStrategy (com.amazonaws.xray.strategy.LogErrorContextMissingStrategy)1 PrioritizationStrategy (com.amazonaws.xray.strategy.PrioritizationStrategy)1