Skip to content

Refactored createSqlSubsegment into a public util file - #186

Merged
willarmiros merged 4 commits into
aws:masterfrom
willarmiros:public-sql
Jul 27, 2020
Merged

Refactored createSqlSubsegment into a public util file#186
willarmiros merged 4 commits into
aws:masterfrom
willarmiros:public-sql

Conversation

@willarmiros

Copy link
Copy Markdown
Contributor

Description of changes:
In order to reuse the subsegment creation logic in the Agent's SQL auto-instrumentation, I moved the private createSubsegment method into a new public util class. I made the new Util class rather than just making the method public in TracingStatement since we've deprecated TracingStatement, so I thought this public method should be separate.

I also added a capability to record SQL queries, relating to #28. Including them in subsegments will be configurable for the agent, but no such config exists in the SDK yet.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Comment thread aws-xray-recorder-sdk-sql/src/main/java/com/amazonaws/xray/sql/SqlUtils.java Outdated
Comment thread aws-xray-recorder-sdk-sql/src/main/java/com/amazonaws/xray/sql/SqlUtils.java Outdated
Comment thread aws-xray-recorder-sdk-sql/src/main/java/com/amazonaws/xray/sql/SqlUtils.java Outdated
Comment thread aws-xray-recorder-sdk-sql/src/main/java/com/amazonaws/xray/sql/SqlUtils.java Outdated
DatabaseMetaData metadata = connection.getMetaData();
String subsegmentName = DEFAULT_DATABASE_NAME;
try {
URI normalizedUri = new URI(new URI(metadata.getURL()).getSchemeSpecificPart());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional because just copy paste, but I can't find a difference between this double-parse and just metadata.getHost()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JDBC DatabaseMetaData interface doesn't have a getHost() method, but I'm not sure why the getSchemeSpecificPart is necessary. I'll leave it as is to be safe, maybe @shengxil can shed some light.

URI normalizedUri = new URI(new URI(metadata.getURL()).getSchemeSpecificPart());
subsegmentName = connection.getCatalog() + "@" + normalizedUri.getHost();
} catch (URISyntaxException e) {
logger.warn("Unable to parse database URI. Falling back to default '" + DEFAULT_DATABASE_NAME

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we could simplify the above this warning could go away, I think it's bad to spam this for every query.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree we shouldn't be spamming this, but seeing as we're keeping the parsing for now I'll change it to debug

Comment thread aws-xray-recorder-sdk-sql/src/main/java/com/amazonaws/xray/sql/SqlUtils.java Outdated
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer rule, @Rule public MockitoRule = MockitoJunit.rue()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do this but kept getting NPEs where I was stubbing methods because the mocks weren't initialized. I tried to also use ExtendWith like you did in some other classes, but that caused a weird NoSuchMethodError. I ended up settling on the static initMocks method in the BeforeEach block.

Comment thread aws-xray-recorder-sdk-sql/src/test/java/com/amazonaws/xray/sql/SqlUtilsTest.java Outdated

@willarmiros willarmiros left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed the comments, and upgraded the tests to JUnit 5. This caused some weird errors that prevented me from adopting @rule annotation, let me know if there's a better fix than what I did.

Comment thread aws-xray-recorder-sdk-sql/src/main/java/com/amazonaws/xray/sql/SqlUtils.java Outdated
DatabaseMetaData metadata = connection.getMetaData();
String subsegmentName = DEFAULT_DATABASE_NAME;
try {
URI normalizedUri = new URI(new URI(metadata.getURL()).getSchemeSpecificPart());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JDBC DatabaseMetaData interface doesn't have a getHost() method, but I'm not sure why the getSchemeSpecificPart is necessary. I'll leave it as is to be safe, maybe @shengxil can shed some light.

URI normalizedUri = new URI(new URI(metadata.getURL()).getSchemeSpecificPart());
subsegmentName = connection.getCatalog() + "@" + normalizedUri.getHost();
} catch (URISyntaxException e) {
logger.warn("Unable to parse database URI. Falling back to default '" + DEFAULT_DATABASE_NAME

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree we shouldn't be spamming this, but seeing as we're keeping the parsing for now I'll change it to debug

Comment thread aws-xray-recorder-sdk-sql/src/main/java/com/amazonaws/xray/sql/SqlUtils.java Outdated
Comment thread aws-xray-recorder-sdk-sql/src/main/java/com/amazonaws/xray/sql/SqlUtils.java Outdated
Comment thread aws-xray-recorder-sdk-sql/src/test/java/com/amazonaws/xray/sql/SqlUtilsTest.java Outdated
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do this but kept getting NPEs where I was stubbing methods because the mocks weren't initialized. I tried to also use ExtendWith like you did in some other classes, but that caused a weird NoSuchMethodError. I ended up settling on the static initMocks method in the BeforeEach block.


@BeforeEach
void setup() throws SQLException {
MockitoAnnotations.initMocks(this);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm surprised extends with didn't work will take a look after the PR

@willarmiros
willarmiros merged commit f527a14 into aws:master Jul 27, 2020
@willarmiros
willarmiros deleted the public-sql branch July 27, 2020 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants