Skip to content

Commit

Permalink
Add default test, document properties
Browse files Browse the repository at this point in the history
  • Loading branch information
lainekendall committed Oct 18, 2018
1 parent 0d9abc0 commit bdbf55a
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 3 deletions.
Expand Up @@ -21,9 +21,12 @@
public class JdbcTracingProperties {

/**
* Enable tracing for {@link java.sql.Connection}
* Trace JDBC calls only if it's part of an active span.
*/
private boolean withActiveSpanOnly = false;
/**
* Set of JDBC statement calls to not trace.
*/
private Set<String> ignoreStatements = new HashSet<>();

public boolean isWithActiveSpanOnly() {
Expand Down
Expand Up @@ -5,6 +5,18 @@
"type": "java.lang.Boolean",
"description": "Enable JDBC tracing.",
"defaultValue": true
},
{
"name": "opentracing.spring.cloud.jdbc.withActiveSpanOnly",
"type": "java.lang.Boolean",
"description": "Only trace JDBC calls if they are part of an active Span.",
"defaultValue": false
},
{
"name": "opentracing.spring.cloud.jdbc.ignoreStatements",
"type": "java.util.Set<java.lang.String>",
"description": "Set of JDBC statements to not trace.",
"defaultValue": null
}
]
}
Expand Up @@ -40,7 +40,7 @@
@TestPropertySource(properties = {
"opentracing.spring.cloud.jdbc.ignoreStatements=SELECT 1"
})
public class JdbcIgnoredStatements {
public class JdbcIgnoreStatementsTest {

@Autowired
MockTracer tracer;
Expand Down
@@ -0,0 +1,91 @@
/**
* Copyright 2017-2018 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package io.opentracing.contrib.spring.cloud.jdbc.jdbc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import io.opentracing.Scope;
import io.opentracing.contrib.spring.cloud.jdbc.MockTracingConfiguration;
import io.opentracing.mock.MockSpan;
import io.opentracing.mock.MockTracer;
import io.opentracing.tag.Tags;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Optional;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
* Test behaviour when withActiveSpanOnly is set
* @author Will Penington
*/
@SpringBootTest(classes = {MockTracingConfiguration.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class JdbcWithActiveSpanDefaultTest {

@Autowired
MockTracer tracer;

@Autowired
DataSource dataSource;

@Autowired
JdbcTemplate jdbcTemplate;

@Before
public void before() {
tracer.reset();
}

/**
* Make sure that a span is created when an active span exists.
*/
@Test
public void spanJoinsActiveSpan() throws SQLException {
try (Scope ignored = tracer.buildSpan("parent").startActive(true)) {
assertTrue(dataSource.getConnection().prepareStatement("select 1").execute());
assertEquals(1, tracer.finishedSpans().size());
}

assertEquals(2, tracer.finishedSpans().size());

Optional<MockSpan> jdbcSpan = tracer
.finishedSpans()
.stream()
.filter((s) -> "java-jdbc".equals(s.tags().get(Tags.COMPONENT.getKey())))
.findFirst();

assertTrue(jdbcSpan.isPresent());
}

/**
* Make sure a new span is created when there is no active parent span.
*/
@Test
public void spanIsCreatedForPreparedStatement() throws SQLException {
PreparedStatement pstmt = dataSource.getConnection().prepareStatement("select 1");
assertTrue(pstmt.execute());
assertEquals(1, tracer.finishedSpans().size());
}

}
Expand Up @@ -44,7 +44,7 @@
@TestPropertySource(properties = {
"opentracing.spring.cloud.jdbc.withActiveSpanOnly=true"
})
public class JdbcOnlyWithActiveTest {
public class JdbcWithActiveSpanTest {

@Autowired
MockTracer tracer;
Expand Down

0 comments on commit bdbf55a

Please sign in to comment.