Skip to content

Commit

Permalink
Merge pull request #224 from solarwinds-cloud/cc/NH-77733
Browse files Browse the repository at this point in the history
NH-77733: Fix trace context in queries lambda edition
  • Loading branch information
cleverchuk authored Apr 22, 2024
2 parents a32e6b1 + f6e5234 commit 70734df
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 256 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ jobs:
- run:
name: Build agent
command: ./gradlew clean build -x test
- run:
name: Muzzle check
command: ./gradlew muzzle
- run:
name: Execute tests
command: ./gradlew test
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
}
dependencies {
classpath "com.diffplug.spotless:spotless-plugin-gradle:6.14.0"
classpath "gradle.plugin.com.github.johnrengelman:shadow:7.1.2"
classpath "com.github.johnrengelman:shadow:8.1.1"
classpath "io.opentelemetry.instrumentation:gradle-plugins:2.3.0-alpha"
}
}
Expand Down
12 changes: 3 additions & 9 deletions instrumentation/jdbc/build.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
apply plugin: 'groovy'
apply from: "$rootDir/gradle/instrumentation.gradle"

//apply plugin: "otel.javaagent-instrumentation"

//muzzle {
// pass {
// coreJdk()
// }
//}

dependencies {
compileOnly project(":bootstrap")
compileOnly "com.solarwinds.joboe:core:${versions.joboe}"
compileOnly "com.solarwinds.joboe:config:${versions.joboe}"
compileOnly "io.opentelemetry:opentelemetry-sdk-trace:${versions.opentelemetry}"

compileOnly "io.opentelemetry.semconv:opentelemetry-semconv:${versions.opentelemetrySemconv}"
compileOnly "com.github.ben-manes.caffeine:caffeine:${versions.caffeine}"

testImplementation project(path: ":instrumentation:jdbc")
testImplementation("io.opentelemetry.javaagent:opentelemetry-testing-common:${versions.opentelemetryJavaagentAlpha}") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.solarwinds.opentelemetry.instrumentation;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.time.Duration;
import java.util.List;

public class BackTraceCache {
private static final Cache<List<StackTraceElement>, String> backTraceCache =
Caffeine.newBuilder()
.maximumSize(20)
.expireAfterAccess(Duration.ofHours(1L))
.build(); // 1 hour cache;

static String getBackTraceString(List<StackTraceElement> stackTrace) {
return backTraceCache.getIfPresent(stackTrace);
}

static void putBackTraceString(List<StackTraceElement> stackTrace, String stackTraceString) {
backTraceCache.put(stackTrace, stackTraceString);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.solarwinds.opentelemetry.instrumentation;

import com.solarwinds.joboe.logging.Logger;
import com.solarwinds.joboe.logging.LoggerFactory;
import java.util.Arrays;
import java.util.List;

public class BackTraceUtil {
private static final int MAX_BACK_TRACE_TOP_LINE_COUNT = 100;

private static final int MAX_BACK_TRACE_BOTTOM_LINE_COUNT = 20;

private static final int MAX_BACK_TRACE_LINE_COUNT =
MAX_BACK_TRACE_TOP_LINE_COUNT + MAX_BACK_TRACE_BOTTOM_LINE_COUNT;

private static final Logger logger = LoggerFactory.getLogger();

public static StackTraceElement[] getBackTrace(int skipElements) {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();

int startPosition =
2 + skipElements; // Starts with 2: To exclude the getStackTrace() and addBackTrace calls
// themselves. Also adds the number of skipElements provided in the
// argument to skip elements

if (startPosition >= stackTrace.length) {
logger.debug(
"Attempt to skip ["
+ skipElements
+ "] elements in addBackTrace is invalid, no stack trace element is left!");
return new StackTraceElement[0];
}

int targetStackTraceLength = stackTrace.length - startPosition;
StackTraceElement[] targetStackTrace = new StackTraceElement[targetStackTraceLength];
System.arraycopy(stackTrace, startPosition, targetStackTrace, 0, targetStackTraceLength);

return targetStackTrace;
}

public static String backTraceToString(StackTraceElement[] stackTrace) {
List<StackTraceElement> wrappedStackTrace =
Arrays.asList(stackTrace); // wrap it so hashCode and equals work

String cachedValue = BackTraceCache.getBackTraceString(wrappedStackTrace);
if (cachedValue != null) {
return cachedValue;
}

StringBuffer stringBuffer = new StringBuffer();
if (stackTrace.length > MAX_BACK_TRACE_LINE_COUNT) { // then we will have to skip some lines
appendStackTrace(
stackTrace, 0, MAX_BACK_TRACE_TOP_LINE_COUNT, stringBuffer); // add the top lines

stringBuffer
.append("...Skipped ")
.append(stackTrace.length - MAX_BACK_TRACE_LINE_COUNT)
.append(" line(s)\n");

appendStackTrace(
stackTrace,
stackTrace.length - MAX_BACK_TRACE_BOTTOM_LINE_COUNT,
MAX_BACK_TRACE_BOTTOM_LINE_COUNT,
stringBuffer); // add the bottom lines

} else {
appendStackTrace(stackTrace, 0, stackTrace.length, stringBuffer); // add everything
}

String value = stringBuffer.toString();
BackTraceCache.putBackTraceString(wrappedStackTrace, value);
return value;
}

/**
* Build the stackTrace output and append the result to the buffer provided
*
* @param stackTrace The source of the stack trace array
* @param buffer The buffer that stores the result
*/
private static void appendStackTrace(
StackTraceElement[] stackTrace, int startPosition, int lineCount, StringBuffer buffer) {
for (int i = startPosition; i < startPosition + lineCount && i < stackTrace.length; i++) {
buffer.append(stackTrace[i].toString()).append("\n");
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@

import com.solarwinds.joboe.config.ConfigManager;
import com.solarwinds.joboe.config.ConfigProperty;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.util.List;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -43,10 +41,6 @@ public void transform(TypeTransformer transformer) {
SwoPreparedStatementInstrumentation.class.getName() + "$PreparedStatementExecuteAdvice");
}

public static class QueryArgsAttributeKey {
public static final AttributeKey<List<String>> KEY = AttributeKey.stringArrayKey("QueryArgs");
}

@SuppressWarnings("unused")
public static class PreparedStatementExecuteAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
Expand Down
Loading

0 comments on commit 70734df

Please sign in to comment.