Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored createSqlSubsegment into a public util file #186

Merged
merged 4 commits into from
Jul 27, 2020

Conversation

willarmiros
Copy link
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.

DatabaseMetaData metadata = connection.getMetaData();
String subsegmentName = DEFAULT_DATABASE_NAME;
try {
URI normalizedUri = new URI(new URI(metadata.getURL()).getSchemeSpecificPart());
Copy link
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
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
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
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

import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
Copy link
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
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.

Copy link
Contributor Author

@willarmiros willarmiros left a comment

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.

DatabaseMetaData metadata = connection.getMetaData();
String subsegmentName = DEFAULT_DATABASE_NAME;
try {
URI normalizedUri = new URI(new URI(metadata.getURL()).getSchemeSpecificPart());
Copy link
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
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

import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
Copy link
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
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.

None yet

2 participants