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
Address comments
  • Loading branch information
losalex committed Jun 25, 2022
commit 7072d7da64edd625c1e49b9a4d52ca1fab88ac95
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class Instrumentation {
*/
public static Tuple<Boolean, Iterable<LogEntry>> populateInstrumentationInfo(
losalex marked this conversation as resolved.
Show resolved Hide resolved
Iterable<LogEntry> logEntries) {
boolean isWritten = setInstrumentationStatus(true);
boolean isWritten = setInstrumentationStatus();
if (isWritten) return Tuple.of(false, logEntries);
List<LogEntry> entries = new ArrayList<>();

Expand Down Expand Up @@ -96,6 +96,7 @@ public static Tuple<Boolean, Iterable<LogEntry>> populateInstrumentationInfo(
* true
*/
public static WriteOption[] addPartialSuccessOption(WriteOption[] options) {
losalex marked this conversation as resolved.
Show resolved Hide resolved
if (options == null) return options;
List<WriteOption> writeOptions = new ArrayList<WriteOption>();
writeOptions.addAll(Arrays.asList(options));
// Make sure we remove all partial success flags if any exist
Expand Down Expand Up @@ -186,21 +187,27 @@ private static Struct createInfoStruct(String libraryName, String libraryVersion
}

/**
* The helper method used to set a status of a flag which indicates if instrumentation info
* already written or not.
* The helper method used to set to true the flag which indicates if instrumentation info already
* written or not.
*
* @param value {boolean} The value to be set.
* @returns The value of the flag before it was set.
*/
public static boolean setInstrumentationStatus(boolean value) {
if (instrumentationAdded == value) return instrumentationAdded;
public static boolean setInstrumentationStatus() {
if (instrumentationAdded) return instrumentationAdded;
synchronized (instrumentationLock) {
boolean current = instrumentationAdded;
instrumentationAdded = value;
return current;
instrumentationAdded = true;
return false;
}
}

/**
* The package-private method to reset an instrumentation status to false to be used for testing
* purposes
*/
static void resetInstrumentationStatus() {
instrumentationAdded = false;
}

/**
* Returns a library version associated with given class
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class InstrumentationTest {

@Test
public void testInstrumentationGenerated() {
Instrumentation.setInstrumentationStatus(false);
Instrumentation.resetInstrumentationStatus();
verifyEntries(
Instrumentation.populateInstrumentationInfo(ImmutableList.of(STRING_ENTRY)),
1,
Expand All @@ -54,7 +54,7 @@ public void testInstrumentationGenerated() {

@Test
public void testNoInstrumentationGenerated() {
Instrumentation.setInstrumentationStatus(true);
Instrumentation.setInstrumentationStatus();
Tuple<Boolean, Iterable<LogEntry>> pair =
Instrumentation.populateInstrumentationInfo(ImmutableList.of(STRING_ENTRY));
ArrayList<LogEntry> entries = Lists.newArrayList(pair.y());
Expand All @@ -65,7 +65,7 @@ public void testNoInstrumentationGenerated() {

@Test
public void testInstrumentationUpdated() {
Instrumentation.setInstrumentationStatus(false);
Instrumentation.resetInstrumentationStatus();
LogEntry json_entry =
LogEntry.newBuilder(generateInstrumentationPayload(JAVA_OTHER_NAME, JAVA_OTHER_VERSION))
.build();
Expand All @@ -82,7 +82,7 @@ public void testInstrumentationUpdated() {

@Test
public void testInvalidInstrumentationRemoved() {
Instrumentation.setInstrumentationStatus(false);
Instrumentation.resetInstrumentationStatus();
LogEntry json_entry =
LogEntry.newBuilder(generateInstrumentationPayload(JAVA_INVALID_NAME, JAVA_OTHER_VERSION))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public void enhanceLogEntry(LogEntry.Builder builder) {

@Before
public void setUp() {
Instrumentation.setInstrumentationStatus(true);
Instrumentation.setInstrumentationStatus();
logging = EasyMock.createMock(Logging.class);
options = EasyMock.createMock(LoggingOptions.class);
expect(options.getProjectId()).andStubReturn(PROJECT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private void configureListLogsTests(

@Before
public void setUp() {
Instrumentation.setInstrumentationStatus(true);
Instrumentation.setInstrumentationStatus();
rpcFactoryMock = EasyMock.createStrictMock(LoggingRpcFactory.class);
loggingRpcMock = EasyMock.createStrictMock(LoggingRpc.class);
EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(LoggingOptions.class)))
Expand Down Expand Up @@ -2300,7 +2300,7 @@ public void testDiagnosticInfoWithPartialSuccess() {
}

private void testDiagnosticInfoGeneration(boolean addPartialSuccessOption) {
Instrumentation.setInstrumentationStatus(false);
Instrumentation.resetInstrumentationStatus();
LogEntry json_entry =
LogEntry.newBuilder(
InstrumentationTest.generateInstrumentationPayload(
Expand Down