-
Notifications
You must be signed in to change notification settings - Fork 872
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sanitize cql in Apache Camel instrumentation (#3717)
* sanitize cassandra * use SemanticAttributes
- Loading branch information
Showing
5 changed files
with
168 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
.../io/opentelemetry/javaagent/instrumentation/apachecamel/decorators/CassandraConfig.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.apachecamel.decorators | ||
|
||
import org.apache.camel.builder.RouteBuilder | ||
import org.springframework.boot.SpringBootConfiguration | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration | ||
import org.springframework.context.annotation.Bean | ||
|
||
@SpringBootConfiguration | ||
@EnableAutoConfiguration | ||
class CassandraConfig { | ||
|
||
@Bean | ||
RouteBuilder serviceRoute() { | ||
return new RouteBuilder() { | ||
|
||
@Override | ||
void configure() throws Exception { | ||
from("direct:input") | ||
.setHeader("CamelCqlQuery", simple("select * from test.users where id=1 ALLOW FILTERING")) | ||
.toD("cql://{{cassandra.host}}:{{cassandra.port}}/test") | ||
} | ||
} | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
...vy/io/opentelemetry/javaagent/instrumentation/apachecamel/decorators/CassandraTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.apachecamel.decorators | ||
|
||
import com.datastax.driver.core.Cluster | ||
import com.datastax.driver.core.Session | ||
import org.apache.camel.CamelContext | ||
import org.apache.camel.ProducerTemplate | ||
import org.testcontainers.containers.CassandraContainer | ||
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification | ||
import io.opentelemetry.instrumentation.test.RetryOnAddressAlreadyInUseTrait | ||
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes | ||
import org.springframework.boot.SpringApplication | ||
import org.springframework.context.ConfigurableApplicationContext | ||
import org.testcontainers.containers.GenericContainer | ||
import spock.lang.Shared | ||
|
||
import static io.opentelemetry.api.trace.SpanKind.CLIENT | ||
import static io.opentelemetry.api.trace.SpanKind.INTERNAL | ||
|
||
class CassandraTest extends AgentInstrumentationSpecification implements RetryOnAddressAlreadyInUseTrait { | ||
|
||
@Shared | ||
ConfigurableApplicationContext server | ||
@Shared | ||
GenericContainer cassandra | ||
@Shared | ||
Cluster cluster | ||
@Shared | ||
String host | ||
@Shared | ||
int port | ||
|
||
Session session | ||
|
||
def setupSpec() { | ||
withRetryOnAddressAlreadyInUse({ | ||
setupSpecUnderRetry() | ||
}) | ||
} | ||
|
||
def setupSpecUnderRetry() { | ||
cassandra = new CassandraContainer() | ||
cassandra.withExposedPorts(9042) | ||
cassandra.start() | ||
|
||
port = cassandra.getFirstMappedPort() | ||
host = cassandra.getHost() | ||
|
||
cluster = cassandra.getCluster() | ||
|
||
def app = new SpringApplication(CassandraConfig) | ||
app.setDefaultProperties(["cassandra.host": host, "cassandra.port": port]) | ||
server = app.run() | ||
} | ||
|
||
def cleanupSpec() { | ||
server?.close() | ||
cluster?.close() | ||
cassandra.stop() | ||
} | ||
|
||
def setup() { | ||
session = cluster.connect() | ||
|
||
session.execute("CREATE KEYSPACE IF NOT EXISTS test WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':1};") | ||
session.execute("CREATE TABLE IF NOT EXISTS test.users ( id int primary key, name text );") | ||
session.execute("INSERT INTO test.users (id,name) VALUES (1, 'user1') IF NOT EXISTS;") | ||
session.execute("INSERT INTO test.users (id,name) VALUES (2, 'user2') IF NOT EXISTS;") | ||
} | ||
|
||
def cleanup() { | ||
session?.close() | ||
} | ||
|
||
def "test cassandra "() { | ||
|
||
setup: | ||
def camelContext = server.getBean(CamelContext) | ||
ProducerTemplate template = camelContext.createProducerTemplate() | ||
|
||
when: | ||
def response = template.requestBody("direct:input", null) | ||
|
||
then: | ||
response.first().getString("name") == "user1" | ||
|
||
assertTraces(1) { | ||
trace(0, 2) { | ||
span(0) { | ||
kind INTERNAL | ||
hasNoParent() | ||
attributes { | ||
"apache-camel.uri" "direct://input" | ||
} | ||
} | ||
span(1){ | ||
kind CLIENT | ||
attributes { | ||
"apache-camel.uri" "cql://$host:$port/test" | ||
"$SemanticAttributes.DB_NAME.key" "test" | ||
"$SemanticAttributes.DB_STATEMENT.key" "select * from test.users where id=? ALLOW FILTERING" | ||
"$SemanticAttributes.DB_SYSTEM.key" "cassandra" | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} |