Skip to content

Commit b673c01

Browse files
authored
Capture sessionId (#63)
* Capture sessionId Signed-off-by: Pavol Loffay <[email protected]> * Add to helper classes Signed-off-by: Pavol Loffay <[email protected]>
1 parent 6e2aef7 commit b673c01

File tree

7 files changed

+59
-0
lines changed

7 files changed

+59
-0
lines changed

instrumentation/servlet/servlet-2.3/src/main/java/io/opentelemetry/instrumentation/hypertrace/servlet/v2_3/Servlet2BodyInstrumentation.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
2828

2929
import com.google.auto.service.AutoService;
30+
import io.opentelemetry.instrumentation.hypertrace.servlet.common.ServletSpanDecorator;
3031
import io.opentelemetry.javaagent.tooling.Instrumenter;
3132
import io.opentelemetry.trace.Span;
3233
import java.util.Enumeration;
@@ -102,6 +103,7 @@ public String[] helperClassNames() {
102103
"io.opentelemetry.instrumentation.hypertrace.servlet.common.CharBufferData",
103104
"io.opentelemetry.instrumentation.hypertrace.servlet.common.BufferedWriterWrapper",
104105
"io.opentelemetry.instrumentation.hypertrace.servlet.common.BufferedReaderWrapper",
106+
"io.opentelemetry.instrumentation.hypertrace.servlet.common.ServletSpanDecorator",
105107
packageName + ".InstrumentationName",
106108
packageName + ".BufferingHttpServletResponse",
107109
packageName + ".BufferingHttpServletResponse$BufferingServletOutputStream",
@@ -153,6 +155,9 @@ public static Object start(
153155
rootStart = true;
154156
response = new BufferingHttpServletResponse(httpResponse);
155157
request = new BufferingHttpServletRequest(httpRequest, (HttpServletResponse) response);
158+
159+
ServletSpanDecorator.addSessionId(currentSpan, httpRequest);
160+
156161
// set request headers
157162
Enumeration<String> headerNames = httpRequest.getHeaderNames();
158163
Map<String, String> headers = new HashMap<>();

instrumentation/servlet/servlet-3.0/src/main/java/io/opentelemetry/instrumentation/hypertrace/servlet/v3_0/Servlet30BodyInstrumentation.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
2828

2929
import com.google.auto.service.AutoService;
30+
import io.opentelemetry.instrumentation.hypertrace.servlet.common.ServletSpanDecorator;
3031
import io.opentelemetry.javaagent.tooling.Instrumenter;
3132
import io.opentelemetry.trace.Span;
3233
import java.util.Enumeration;
@@ -100,6 +101,7 @@ public String[] helperClassNames() {
100101
"io.opentelemetry.instrumentation.hypertrace.servlet.common.CharBufferData",
101102
"io.opentelemetry.instrumentation.hypertrace.servlet.common.BufferedWriterWrapper",
102103
"io.opentelemetry.instrumentation.hypertrace.servlet.common.BufferedReaderWrapper",
104+
"io.opentelemetry.instrumentation.hypertrace.servlet.common.ServletSpanDecorator",
103105
packageName + ".InstrumentationName",
104106
packageName + ".BufferingHttpServletResponse",
105107
packageName + ".BufferingHttpServletResponse$BufferingServletOutputStream",
@@ -152,6 +154,9 @@ public static Object start(
152154
rootStart = true;
153155
response = new BufferingHttpServletResponse(httpResponse);
154156
request = new BufferingHttpServletRequest(httpRequest, (HttpServletResponse) response);
157+
158+
ServletSpanDecorator.addSessionId(currentSpan, httpRequest);
159+
155160
// set request headers
156161
Enumeration<String> headerNames = httpRequest.getHeaderNames();
157162
Map<String, String> headers = new HashMap<>();

instrumentation/servlet/servlet-3.1/src/main/java/io/opentelemetry/instrumentation/hypertrace/servlet/v3_1/Servlet31BodyInstrumentation.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
2727

2828
import com.google.auto.service.AutoService;
29+
import io.opentelemetry.instrumentation.hypertrace.servlet.common.ServletSpanDecorator;
2930
import io.opentelemetry.javaagent.tooling.Instrumenter;
3031
import io.opentelemetry.trace.Span;
3132
import java.util.Enumeration;
@@ -97,6 +98,7 @@ public String[] helperClassNames() {
9798
"io.opentelemetry.instrumentation.hypertrace.servlet.common.CharBufferData",
9899
"io.opentelemetry.instrumentation.hypertrace.servlet.common.BufferedWriterWrapper",
99100
"io.opentelemetry.instrumentation.hypertrace.servlet.common.BufferedReaderWrapper",
101+
"io.opentelemetry.instrumentation.hypertrace.servlet.common.ServletSpanDecorator",
100102
packageName + ".InstrumentationName",
101103
packageName + ".BufferingHttpServletResponse",
102104
packageName + ".BufferingHttpServletResponse$BufferingServletOutputStream",
@@ -149,6 +151,9 @@ public static Object start(
149151
rootStart = true;
150152
response = new BufferingHttpServletResponse(httpResponse);
151153
request = new BufferingHttpServletRequest(httpRequest, (HttpServletResponse) response);
154+
155+
ServletSpanDecorator.addSessionId(currentSpan, httpRequest);
156+
152157
// set request headers
153158
Enumeration<String> headerNames = httpRequest.getHeaderNames();
154159
Map<String, String> headers = new HashMap<>();

instrumentation/servlet/servlet-common/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ plugins {
44

55
dependencies {
66
compileOnly("javax.servlet:servlet-api:2.3")
7+
api("io.opentelemetry:opentelemetry-api:0.9.1")
78
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright The Hypertrace Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.opentelemetry.instrumentation.hypertrace.servlet.common;
18+
19+
import io.opentelemetry.trace.Span;
20+
import javax.servlet.http.HttpServletRequest;
21+
import javax.servlet.http.HttpSession;
22+
import org.hypertrace.agent.core.HypertraceSemanticAttributes;
23+
24+
public class ServletSpanDecorator {
25+
26+
private ServletSpanDecorator() {}
27+
28+
public static void addSessionId(Span span, HttpServletRequest httpRequest) {
29+
if (httpRequest.isRequestedSessionIdValid()) {
30+
HttpSession session = httpRequest.getSession();
31+
if (session != null && session.getId() != "") {
32+
span.setAttribute(HypertraceSemanticAttributes.HTTP_REQUEST_SESSION_ID, session.getId());
33+
}
34+
}
35+
}
36+
}

instrumentation/spark-web-framework-2.3/src/main/java/io/opentelemetry/instrumentation/hypertrace/sparkjava/SparkJavaBodyInstrumentation.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
2525

2626
import com.google.auto.service.AutoService;
27+
import io.opentelemetry.instrumentation.hypertrace.servlet.common.ServletSpanDecorator;
2728
import io.opentelemetry.instrumentation.hypertrace.servlet.v3_1.BufferingHttpServletRequest;
2829
import io.opentelemetry.instrumentation.hypertrace.servlet.v3_1.BufferingHttpServletResponse;
2930
import io.opentelemetry.javaagent.tooling.Instrumenter;
@@ -91,6 +92,7 @@ public String[] helperClassNames() {
9192
"io.opentelemetry.instrumentation.hypertrace.servlet.common.CharBufferData",
9293
"io.opentelemetry.instrumentation.hypertrace.servlet.common.BufferedWriterWrapper",
9394
"io.opentelemetry.instrumentation.hypertrace.servlet.common.BufferedReaderWrapper",
95+
"io.opentelemetry.instrumentation.hypertrace.servlet.common.ServletSpanDecorator",
9496
"io.opentelemetry.instrumentation.hypertrace.servlet.v3_1.BufferingHttpServletResponse",
9597
"io.opentelemetry.instrumentation.hypertrace.servlet.v3_1.BufferingHttpServletResponse$BufferingServletOutputStream",
9698
"io.opentelemetry.instrumentation.hypertrace.servlet.v3_1.BufferingHttpServletRequest",
@@ -119,6 +121,8 @@ public static Object onEnter(
119121
response = new BufferingHttpServletResponse(httpResponse);
120122
request = new BufferingHttpServletRequest(httpRequest, (HttpServletResponse) response);
121123

124+
ServletSpanDecorator.addSessionId(currentSpan, httpRequest);
125+
122126
Enumeration<String> headerNames = httpRequest.getHeaderNames();
123127
Map<String, String> headers = new HashMap<>();
124128
while (headerNames.hasMoreElements()) {

javaagent-core/src/main/java/org/hypertrace/agent/core/HypertraceSemanticAttributes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ public static AttributeKey<String> httpResponseHeader(String header) {
3939
public static final AttributeKey<Boolean> OPA_RESULT = booleanKey("hypertrace.opa.result");
4040

4141
public static final AttributeKey<String> OPA_REASON = stringKey("hypertrace.opa.reason");
42+
43+
public static final AttributeKey<String> HTTP_REQUEST_SESSION_ID =
44+
stringKey("http.request.session_id");
4245
}

0 commit comments

Comments
 (0)