Skip to content

Commit

Permalink
change naming and util test
Browse files Browse the repository at this point in the history
  • Loading branch information
siyuniu-ms committed Feb 27, 2023
1 parent 0aa85b0 commit 175c2f4
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@

package io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class TestUtil {

public static byte[] readFileBytes(String resourceName, Charset charsetName) throws IOException {
return readFile(resourceName).getBytes(charsetName);
}

public static String readFile(String resourceName) throws IOException {
InputStream in =
SnippetPrintWriterTest.class.getClassLoader().getResourceAsStream(resourceName);
ByteArrayOutputStream result = new ByteArrayOutputStream();
Expand All @@ -26,7 +23,11 @@ public static String readFile(String resourceName) throws IOException {
while ((length = in.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString(StandardCharsets.UTF_8.name());
return result.toByteArray();
}

public static String readFile(String resourceName) throws IOException {
return new String(readFileBytes(resourceName, UTF_8), UTF_8);
}

private TestUtil() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package io.opentelemetry.javaagent.instrumentation.servlet.v3_0;

import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.helper;
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.SnippetInjectingResponseWrapper.FAKE_SNIPPET_HEADER;

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
Expand Down Expand Up @@ -45,7 +44,8 @@ public static void onEnter(

String snippet = ExperimentalSnippetHolder.getSnippet();
if (!snippet.isEmpty()
&& !((HttpServletResponse) response).containsHeader(FAKE_SNIPPET_HEADER)) {
&& !((HttpServletResponse) response)
.containsHeader(SnippetInjectingResponseWrapper.FAKE_SNIPPET_HEADER)) {
response = new SnippetInjectingResponseWrapper((HttpServletResponse) response, snippet);
}
callDepth = CallDepth.forClass(AppServerBridge.getCallDepthKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
package io.opentelemetry.javaagent.instrumentation.servlet.v3_0;

import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.getSnippetInjectionHelper;
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.Injection.getInjectionState;

import io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.InjectionState;
import io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.ServletOutputStreamInjectionState;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import net.bytebuddy.asm.Advice;
Expand All @@ -19,7 +19,7 @@ public class Servlet3OutputStreamWriteBytesAdvice {
public static boolean methodEnter(
@Advice.This ServletOutputStream servletOutputStream, @Advice.Argument(0) byte[] write)
throws IOException {
InjectionState state = getInjectionState(servletOutputStream);
InjectionState state = ServletOutputStreamInjectionState.getInjectionState(servletOutputStream);
if (state == null) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
package io.opentelemetry.javaagent.instrumentation.servlet.v3_0;

import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.getSnippetInjectionHelper;
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.Injection.getInjectionState;

import io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.InjectionState;
import io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.ServletOutputStreamInjectionState;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import net.bytebuddy.asm.Advice;
Expand All @@ -21,7 +21,7 @@ public static boolean methodEnter(
@Advice.Argument(value = 1) int off,
@Advice.Argument(value = 2) int len)
throws IOException {
InjectionState state = getInjectionState(servletOutputStream);
InjectionState state = ServletOutputStreamInjectionState.getInjectionState(servletOutputStream);
if (state == null) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
package io.opentelemetry.javaagent.instrumentation.servlet.v3_0;

import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.Servlet3Singletons.getSnippetInjectionHelper;
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.Injection.getInjectionState;

import io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.InjectionState;
import io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.ServletOutputStreamInjectionState;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import net.bytebuddy.asm.Advice;
Expand All @@ -19,7 +19,7 @@ public class Servlet3OutputStreamWriteIntAdvice {
public static boolean methodEnter(
@Advice.This ServletOutputStream servletOutputStream, @Advice.Argument(0) int write)
throws IOException {
InjectionState state = getInjectionState(servletOutputStream);
InjectionState state = ServletOutputStreamInjectionState.getInjectionState(servletOutputStream);
if (state == null) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.opentelemetry.instrumentation.api.util.VirtualField;
import javax.servlet.ServletOutputStream;

public class Injection {
public class ServletOutputStreamInjectionState {

private static final VirtualField<ServletOutputStream, InjectionState> virtualField =
VirtualField.find(ServletOutputStream.class, InjectionState.class);
Expand All @@ -30,5 +30,5 @@ public static InjectionState getInjectionState(ServletOutputStream servletOutput
return virtualField.get(servletOutputStream);
}

private Injection() {}
private ServletOutputStreamInjectionState() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet;

import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.Injection.initializeInjectionStateIfNeeded;
import static io.opentelemetry.javaagent.instrumentation.servlet.v3_0.snippet.ServletOutputStreamInjectionState.initializeInjectionStateIfNeeded;
import static java.util.logging.Level.FINE;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasSuperType;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;

Expand Down Expand Up @@ -42,7 +41,7 @@ public ElementMatcher<ClassLoader> classLoaderOptimization() {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return hasSuperType(namedOneOf(basePackageName + ".ServletOutputStream"));
return hasSuperType(named(basePackageName + ".ServletOutputStream"));
}

@Override
Expand Down

0 comments on commit 175c2f4

Please sign in to comment.