-
Notifications
You must be signed in to change notification settings - Fork 16
Fallback to db.connection_string for sql backend url #84
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| import com.google.common.collect.Lists; | ||
| import com.google.common.collect.Sets; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
@@ -163,7 +165,39 @@ public static Optional<String> getSqlURI(Event event) { | |
| if (SpanAttributeUtils.containsAttributeKey(event, SQL_URL)) { | ||
| return Optional.of(SpanAttributeUtils.getStringAttribute(event, SQL_URL)); | ||
| } | ||
| return getBackendURIForOtelFormat(event); | ||
| Optional<String> backendUrl = getBackendURIForOtelFormat(event); | ||
| if (backendUrl.isPresent()) { | ||
| return backendUrl; | ||
| } | ||
| if (SpanAttributeUtils.containsAttributeKey( | ||
| event, OTelDbSemanticConventions.DB_CONNECTION_STRING.getValue())) { | ||
| String url = SpanAttributeUtils.getStringAttribute( | ||
| event, OTelDbSemanticConventions.DB_CONNECTION_STRING.getValue()); | ||
| if (!isValidURI(url)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, if you do
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I will do it in a subsequent pr. |
||
| return Optional.empty(); | ||
| } | ||
| return Optional.of(url); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| public static Optional<String> getSqlUrlForOtelFormat( | ||
| Map<String, AttributeValue> attributeValueMap) { | ||
| if (!isSqlTypeBackendForOtelFormat(attributeValueMap)) { | ||
| return Optional.empty(); | ||
| } | ||
| Optional<String> backendUrl = getBackendURIForOtelFormat(attributeValueMap); | ||
| if (backendUrl.isPresent()) { | ||
| return backendUrl; | ||
| } | ||
| if (attributeValueMap.containsKey(OTelDbSemanticConventions.DB_CONNECTION_STRING.getValue())) { | ||
| String url = attributeValueMap.get(OTelDbSemanticConventions.DB_CONNECTION_STRING.getValue()).getValue(); | ||
| if (!isValidURI(url)) { | ||
| return Optional.empty(); | ||
| } | ||
| return Optional.of(url); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -192,4 +226,13 @@ public static Optional<String> getDbTypeForOtelFormat(Event event) { | |
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| static boolean isValidURI(String uri) { | ||
| try { | ||
| new URI(uri); | ||
| } catch (URISyntaxException e) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
|
|
||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import org.hypertrace.core.datamodel.AttributeValue; | ||
| import org.hypertrace.core.semantic.convention.constants.db.OTelDbSemanticConventions; | ||
| import org.hypertrace.core.semantic.convention.constants.span.OTelSpanSemanticConventions; | ||
| import org.hypertrace.semantic.convention.utils.SemanticConventionTestUtil; | ||
|
|
@@ -193,6 +194,46 @@ public void testGetSqlURI() { | |
| when(e.getAttributes()).thenReturn(attributes); | ||
| v = DbSemanticConventionUtils.getSqlURI(e); | ||
| assertTrue(v.isEmpty()); | ||
|
|
||
| attributes = SemanticConventionTestUtil.buildAttributes( | ||
| Map.of( | ||
| OTelDbSemanticConventions.DB_SYSTEM.getValue(), | ||
| SemanticConventionTestUtil.buildAttributeValue(OTelDbSemanticConventions.MYSQL_DB_SYSTEM_VALUE.getValue()), | ||
| OTelDbSemanticConventions.DB_CONNECTION_STRING.getValue(), | ||
| SemanticConventionTestUtil.buildAttributeValue("jdbc:mysql://mysql:3306/shop"))); | ||
| when(e.getAttributes()).thenReturn(attributes); | ||
| v = DbSemanticConventionUtils.getSqlURI(e); | ||
| assertEquals("jdbc:mysql://mysql:3306/shop", v.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetSqlUrlForOtelFormat() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we need some test where the user/password are included in the URL. Not sure tif that is a thing among jdbc.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean if username/pwd is present we should drop the url? |
||
| Map<String, AttributeValue> map = | ||
| Map.of( | ||
| OTelDbSemanticConventions.DB_SYSTEM.getValue(), | ||
| SemanticConventionTestUtil.buildAttributeValue(OTelDbSemanticConventions.MYSQL_DB_SYSTEM_VALUE.getValue()), | ||
| OTelDbSemanticConventions.DB_CONNECTION_STRING.getValue(), | ||
| SemanticConventionTestUtil.buildAttributeValue("jdbc:mysql://mysql:3306/shop")); | ||
| Optional<String> v = DbSemanticConventionUtils.getSqlUrlForOtelFormat(map); | ||
| assertEquals("jdbc:mysql://mysql:3306/shop", v.get()); | ||
|
|
||
| map = Map.of( | ||
| OTelDbSemanticConventions.DB_CONNECTION_STRING.getValue(), | ||
| SemanticConventionTestUtil.buildAttributeValue("jdbc:mysql://mysql:3306/shop") | ||
| ); | ||
| v = DbSemanticConventionUtils.getSqlUrlForOtelFormat(map); | ||
| assertTrue(v.isEmpty()); | ||
|
|
||
| map = | ||
| Map.of( | ||
| OTelSpanSemanticConventions.NET_PEER_IP.getValue(), | ||
| SemanticConventionTestUtil.buildAttributeValue("127.0.0.1"), | ||
| OTelDbSemanticConventions.DB_SYSTEM.getValue(), | ||
| SemanticConventionTestUtil.buildAttributeValue(OTelDbSemanticConventions.MYSQL_DB_SYSTEM_VALUE.getValue()), | ||
| OTelDbSemanticConventions.DB_CONNECTION_STRING.getValue(), | ||
| SemanticConventionTestUtil.buildAttributeValue("jdbc:mysql://mysql:3306/shop")); | ||
| v = DbSemanticConventionUtils.getSqlUrlForOtelFormat(map); | ||
| assertEquals("127.0.0.1", v.get()); | ||
| } | ||
|
|
||
| @Test | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we have to convert this
Stringto URI object?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done