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

feat: Add support for library instrumentation #979

Merged
merged 9 commits into from
Jun 25, 2022
Prev Previous commit
Next Next commit
Fix tests
  • Loading branch information
losalex committed Jun 22, 2022
commit 553ce1f6eb3e214de27fa9a50881c38d5670ad21
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ private static ListValue generateLibrariesList(
if (Strings.isNullOrEmpty(libraryName) || !libraryName.startsWith(JAVA_LIBRARY_NAME_PREFIX))
losalex marked this conversation as resolved.
Show resolved Hide resolved
libraryName = JAVA_LIBRARY_NAME_PREFIX;
if (Strings.isNullOrEmpty(libraryVersion)) {
libraryVersion = GaxProperties.getLibraryVersion(Instrumentation.class.getClass());
if (Strings.isNullOrEmpty(libraryVersion)) libraryVersion = DEFAULT_INSTRUMENTATION_VERSION;
libraryVersion = getLibraryVersion(Instrumentation.class.getClass());
}
Struct libraryInfo = createInfoStruct(libraryName, libraryVersion);
ListValue.Builder libraryList = ListValue.newBuilder();
Expand Down Expand Up @@ -171,6 +170,19 @@ public static boolean setInstrumentationStatus(boolean value) {
return setAndGetInstrumentationStatus(value);
}

/**
* Returns a library version associated with given class
*
* @param libraryClass {Class<?>} The class to be used to determine a library version
* @return The version number string for given class or "UNKNOWN" if class library version cannot
* be detected
*/
public static String getLibraryVersion(Class<?> libraryClass) {
String libraryVersion = GaxProperties.getLibraryVersion(libraryClass);
if (Strings.isNullOrEmpty(libraryVersion)) libraryVersion = DEFAULT_INSTRUMENTATION_VERSION;
return libraryVersion;
}

private static synchronized boolean setAndGetInstrumentationStatus(boolean value) {
boolean current = instrumentationAdded;
instrumentationAdded = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public void testInstrumentationGenerated() {
1,
2,
new HashSet<>(Arrays.asList(Instrumentation.JAVA_LIBRARY_NAME_PREFIX)),
new HashSet<>(Arrays.asList(Instrumentation.DEFAULT_INSTRUMENTATION_VERSION)));
new HashSet<>(
Arrays.asList(Instrumentation.getLibraryVersion(Instrumentation.class.getClass()))));
}

@Test
Expand All @@ -74,7 +75,9 @@ public void testInstrumentationUpdated() {
1,
new HashSet<>(Arrays.asList(Instrumentation.JAVA_LIBRARY_NAME_PREFIX, JAVA_OTHER_NAME)),
new HashSet<>(
Arrays.asList(Instrumentation.DEFAULT_INSTRUMENTATION_VERSION, JAVA_OTHER_VERSION)));
Arrays.asList(
Instrumentation.getLibraryVersion(Instrumentation.class.getClass()),
JAVA_OTHER_VERSION)));
}

@Test
Expand All @@ -88,7 +91,8 @@ public void testInvalidInstrumentationRemoved() {
0,
1,
new HashSet<>(Arrays.asList(Instrumentation.JAVA_LIBRARY_NAME_PREFIX)),
new HashSet<>(Arrays.asList(Instrumentation.DEFAULT_INSTRUMENTATION_VERSION)));
new HashSet<>(
Arrays.asList(Instrumentation.getLibraryVersion(Instrumentation.class.getClass()))));
}

public static JsonPayload generateInstrumentationPayload(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ public void testDiagnosticInfo() {
LogEntry.newBuilder(
InstrumentationTest.generateInstrumentationPayload(
Instrumentation.JAVA_LIBRARY_NAME_PREFIX,
Instrumentation.DEFAULT_INSTRUMENTATION_VERSION))
Instrumentation.getLibraryVersion(Instrumentation.class.getClass())))
.build();
logging.write(
ImmutableList.of(FINEST_ENTRY, json_entry),
Expand Down