1111import static io .opentelemetry .semconv .ExceptionAttributes .EXCEPTION_MESSAGE ;
1212import static io .opentelemetry .semconv .ExceptionAttributes .EXCEPTION_STACKTRACE ;
1313import static io .opentelemetry .semconv .ExceptionAttributes .EXCEPTION_TYPE ;
14+ import static io .opentelemetry .semconv .incubating .ThreadIncubatingAttributes .THREAD_ID ;
15+ import static io .opentelemetry .semconv .incubating .ThreadIncubatingAttributes .THREAD_NAME ;
1416import static java .util .concurrent .TimeUnit .MILLISECONDS ;
1517
1618import io .opentelemetry .api .common .AttributeKey ;
1719import io .opentelemetry .api .logs .Severity ;
1820import io .opentelemetry .api .trace .SpanContext ;
1921import io .opentelemetry .instrumentation .testing .junit .AgentInstrumentationExtension ;
2022import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
23+ import io .opentelemetry .instrumentation .testing .junit .code .SemconvCodeStabilityUtil ;
2124import io .opentelemetry .sdk .common .InstrumentationScopeInfo ;
2225import io .opentelemetry .sdk .logs .data .LogRecordData ;
2326import io .opentelemetry .sdk .testing .assertj .AttributeAssertion ;
24- import io .opentelemetry .semconv .incubating .ThreadIncubatingAttributes ;
2527import java .lang .reflect .Field ;
2628import java .time .Instant ;
2729import java .util .ArrayList ;
@@ -67,6 +69,26 @@ private static Stream<Arguments> provideParameters() {
6769 Arguments .of (true , true ));
6870 }
6971
72+ @ Test
73+ public void testCodeAttributes () {
74+ logger .info ("this is test message" );
75+ List <AttributeAssertion > assertions =
76+ SemconvCodeStabilityUtil .codeFileAndLineAssertions ("Log4j1Test.java" );
77+ assertions .addAll (
78+ SemconvCodeStabilityUtil .codeFunctionAssertions (Log4j1Test .class , "testCodeAttributes" ));
79+ assertions .add (equalTo (THREAD_NAME , Thread .currentThread ().getName ()));
80+ assertions .add (equalTo (THREAD_ID , Thread .currentThread ().getId ()));
81+
82+ testing .waitAndAssertLogRecords (
83+ logRecord ->
84+ logRecord
85+ .hasBody ("this is test message" )
86+ .hasInstrumentationScope (InstrumentationScopeInfo .builder ("abc" ).build ())
87+ .hasSeverity (Severity .INFO )
88+ .hasSeverityText ("INFO" )
89+ .hasAttributesSatisfyingExactly (assertions ));
90+ }
91+
7092 @ ParameterizedTest
7193 @ MethodSource ("provideParameters" )
7294 public void test (boolean logException , boolean withParent ) throws InterruptedException {
@@ -122,11 +144,8 @@ private static void test(
122144 List <AttributeAssertion > attributeAsserts =
123145 new ArrayList <>(
124146 Arrays .asList (
125- equalTo (
126- ThreadIncubatingAttributes .THREAD_NAME ,
127- Thread .currentThread ().getName ()),
128- equalTo (
129- ThreadIncubatingAttributes .THREAD_ID , Thread .currentThread ().getId ())));
147+ equalTo (THREAD_NAME , Thread .currentThread ().getName ()),
148+ equalTo (THREAD_ID , Thread .currentThread ().getId ())));
130149 if (logException ) {
131150 attributeAsserts .addAll (
132151 Arrays .asList (
@@ -135,6 +154,11 @@ private static void test(
135154 satisfies (
136155 EXCEPTION_STACKTRACE , v -> v .contains (Log4j1Test .class .getName ()))));
137156 }
157+ attributeAsserts .addAll (
158+ SemconvCodeStabilityUtil .codeFunctionAssertions (
159+ Log4j1Test .class , "performLogging" ));
160+ attributeAsserts .addAll (
161+ SemconvCodeStabilityUtil .codeFileAndLineAssertions ("Log4j1Test.java" ));
138162 logRecord .hasAttributesSatisfyingExactly (attributeAsserts );
139163
140164 LogRecordData logRecordData = AssertAccess .getActual (logRecord );
@@ -159,19 +183,22 @@ void testMdc() {
159183 MDC .remove ("key2" );
160184 }
161185
186+ List <AttributeAssertion > assertions =
187+ SemconvCodeStabilityUtil .codeFileAndLineAssertions ("Log4j1Test.java" );
188+ assertions .addAll (SemconvCodeStabilityUtil .codeFunctionAssertions (Log4j1Test .class , "testMdc" ));
189+ assertions .add (equalTo (AttributeKey .stringKey ("key1" ), "val1" ));
190+ assertions .add (equalTo (AttributeKey .stringKey ("key2" ), "val2" ));
191+ assertions .add (equalTo (THREAD_NAME , Thread .currentThread ().getName ()));
192+ assertions .add (equalTo (THREAD_ID , Thread .currentThread ().getId ()));
193+
162194 testing .waitAndAssertLogRecords (
163195 logRecord ->
164196 logRecord
165197 .hasBody ("xyz" )
166198 .hasInstrumentationScope (InstrumentationScopeInfo .builder ("abc" ).build ())
167199 .hasSeverity (Severity .INFO )
168200 .hasSeverityText ("INFO" )
169- .hasAttributesSatisfyingExactly (
170- equalTo (AttributeKey .stringKey ("key1" ), "val1" ),
171- equalTo (AttributeKey .stringKey ("key2" ), "val2" ),
172- equalTo (
173- ThreadIncubatingAttributes .THREAD_NAME , Thread .currentThread ().getName ()),
174- equalTo (ThreadIncubatingAttributes .THREAD_ID , Thread .currentThread ().getId ())));
201+ .hasAttributesSatisfyingExactly (assertions ));
175202 }
176203
177204 private static void performLogging (
0 commit comments