Skip to content

Commit

Permalink
apache camel propagation tests - cleanup #1 (#2607)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-wu authored Mar 29, 2021
1 parent 202e038 commit f856452
Show file tree
Hide file tree
Showing 11 changed files with 462 additions and 974 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.apachecamel
package io.opentelemetry.javaagent.instrumentation.apachecamel.aws

import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.BucketNotificationConfiguration
Expand All @@ -12,7 +12,6 @@ import com.amazonaws.services.s3.model.QueueConfiguration
import com.amazonaws.services.s3.model.S3Event
import com.amazonaws.services.s3.model.S3ObjectSummary
import com.amazonaws.services.s3.model.SetBucketNotificationConfigurationRequest
import com.amazonaws.services.s3.model.TopicConfiguration
import com.amazonaws.services.sns.AmazonSNSAsyncClient
import com.amazonaws.services.sns.model.CreateTopicResult
import com.amazonaws.services.sns.model.SetTopicAttributesRequest
Expand All @@ -23,7 +22,6 @@ import com.amazonaws.services.sqs.model.ReceiveMessageRequest

class AwsConnector {


private AmazonSQSAsyncClient sqsClient
private AmazonS3Client s3Client
private AmazonSNSAsyncClient snsClient
Expand All @@ -33,16 +31,26 @@ class AwsConnector {

awsConnector.sqsClient = AmazonSQSAsyncClient.asyncBuilder()
.build()

awsConnector.s3Client = AmazonS3Client.builder()
.build()

awsConnector.snsClient = AmazonSNSAsyncClient.asyncBuilder()
.build()

return awsConnector
}

AmazonSQSAsyncClient getSqsClient() {
return sqsClient
}

AmazonS3Client getS3Client() {
return s3Client
}

AmazonSNSAsyncClient getSnsClient() {
return snsClient
}

def createQueue(String queueName) {
println "Create queue ${queueName}"
return sqsClient.createQueue(queueName).getQueueUrl()
Expand Down Expand Up @@ -110,15 +118,6 @@ class AwsConnector {
bucketName, notificationConfiguration))
}

def enableS3ToSnsNotifications(String bucketName, String snsTopicArn) {
println "Enable notification for bucket ${bucketName} to topic ${snsTopicArn}"
BucketNotificationConfiguration notificationConfiguration = new BucketNotificationConfiguration()
notificationConfiguration.addConfiguration("snsTopicConfig",
new TopicConfiguration(snsTopicArn, EnumSet.of(S3Event.ObjectCreatedByPut)))
s3Client.setBucketNotificationConfiguration(new SetBucketNotificationConfigurationRequest(
bucketName, notificationConfiguration))
}

def createTopicAndSubscribeQueue(String topicName, String queueArn) {
println "Create topic ${topicName} and subscribe to queue ${queueArn}"
CreateTopicResult ctr = snsClient.createTopic(topicName)
Expand All @@ -136,11 +135,6 @@ class AwsConnector {
sqsClient.purgeQueue(new PurgeQueueRequest(queueUrl))
}

def putSampleData(String bucketName) {
println "Put sample data to bucket ${bucketName}"
s3Client.putObject(bucketName, "otelTestKey", "otelTestData")
}

def publishSampleNotification(String topicArn) {
snsClient.publish(topicArn, "Hello There")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.apachecamel.aws

import static io.opentelemetry.api.trace.SpanKind.CLIENT

import io.opentelemetry.instrumentation.test.asserts.TraceAssert

class AwsSpan {

static s3(TraceAssert traceAssert, int index, spanName, bucketName, method="GET", parentSpan=null) {
return traceAssert.span(index) {
name spanName
kind CLIENT
if (index == 0) {
hasNoParent()
} else {
childOf parentSpan
}
attributes {
"aws.agent" "java-aws-sdk"
"aws.endpoint" String
"aws.operation" spanName.substring(3)
"aws.service" "Amazon S3"
"aws.bucket.name" bucketName
"http.flavor" "1.1"
"http.method" method
"http.status_code" 200
"http.url" String
"net.peer.name" String
"net.transport" "IP.TCP"
"net.peer.port" { it == null || Number }
}
}
}

static sqs(TraceAssert traceAssert, int index, spanName, queueUrl=null, queueName=null, spanKind=CLIENT, parentSpan=null) {
return traceAssert.span(index) {
name spanName
kind spanKind
if (index == 0) {
hasNoParent()
} else {
childOf parentSpan
}
attributes {
"aws.agent" "java-aws-sdk"
"aws.endpoint" String
"aws.operation" spanName.substring(4)
"aws.service" "AmazonSQS"
"aws.queue.name" {it == null || it == queueName}
"aws.queue.url" {it == null || it == queueUrl}
"http.flavor" "1.1"
"http.method" "POST"
"http.status_code" 200
"http.url" String
"http.user_agent" { it == null || String }
"net.peer.name" String
"net.peer.port" { it == null || Number }
"net.transport" "IP.TCP"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.apachecamel.aws

import org.apache.camel.CamelContext
import org.apache.camel.ProducerTemplate
import org.springframework.boot.SpringApplication
import org.springframework.context.ApplicationContextInitializer
import org.springframework.context.ConfigurableApplicationContext
import org.springframework.context.support.AbstractApplicationContext

class CamelSpringApp {

private SpringApplication springApplication
private ConfigurableApplicationContext context

CamelSpringApp(AwsConnector awsConnector, Class config, Map<String, String> properties) {
springApplication = new SpringApplication(config)
springApplication.setDefaultProperties(properties)
injectClients(awsConnector)
}

private injectClients(AwsConnector awsConnector) {
springApplication.addInitializers(new ApplicationContextInitializer<AbstractApplicationContext>() {
@Override
void initialize(AbstractApplicationContext applicationContext) {
applicationContext.getBeanFactory().registerSingleton("snsClient", awsConnector.getSnsClient())
applicationContext.getBeanFactory().registerSingleton("sqsClient", awsConnector.getSqsClient())
applicationContext.getBeanFactory().registerSingleton("s3Client", awsConnector.getS3Client())
}
})
}

def start() {
context = springApplication.run()
}

ProducerTemplate producerTemplate() {
def camelContext = context.getBean(CamelContext)
return camelContext.createProducerTemplate()
}

def stop() {
if (context != null) {
SpringApplication.exit(context)
}
}
}
Loading

0 comments on commit f856452

Please sign in to comment.