1111import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .equalTo ;
1212import static io .opentelemetry .sdk .testing .assertj .OpenTelemetryAssertions .satisfies ;
1313import static java .nio .charset .StandardCharsets .UTF_8 ;
14- import static java .util .Arrays .asList ;
1514
1615import com .sun .net .httpserver .HttpServer ;
1716import io .opentelemetry .api .trace .SpanKind ;
2827import java .net .InetSocketAddress ;
2928import java .nio .file .Files ;
3029import java .nio .file .Path ;
30+ import java .util .ArrayList ;
3131import java .util .List ;
3232import java .util .Locale ;
33+ import javax .annotation .Nullable ;
3334import org .apache .shardingsphere .elasticjob .api .JobConfiguration ;
3435import org .apache .shardingsphere .elasticjob .http .props .HttpJobProperties ;
3536import org .apache .shardingsphere .elasticjob .infra .env .IpUtils ;
@@ -55,6 +56,9 @@ class ElasticJobTest {
5556 private static CoordinatorRegistryCenter regCenter ;
5657 private static HttpServer httpServer ;
5758
59+ private static final boolean EXPERIMENTAL_ATTRIBUTES_ENABLED =
60+ Boolean .getBoolean ("otel.instrumentation.apache-elasticjob.experimental-span-attributes" );
61+
5862 @ BeforeAll
5963 static void init () throws Exception {
6064 // ip address detection in ElasticJob may fail to find a valid address, we skip the detection
@@ -150,7 +154,7 @@ void testSimpleJob() {
150154 regCenter ,
151155 job ,
152156 JobConfiguration .newBuilder ("simpleElasticJob" , 2 )
153- .cron ("0/5 * * * * ?" )
157+ .cron ("0/30 * * * * ?" )
154158 .shardingItemParameters ("0=A,1=B" )
155159 .build ());
156160
@@ -350,6 +354,11 @@ private static String buildScriptCommandLine() throws IOException {
350354 return tempScript .toString ();
351355 }
352356
357+ @ Nullable
358+ private static <T > T experimental (T value ) {
359+ return EXPERIMENTAL_ATTRIBUTES_ENABLED ? value : null ;
360+ }
361+
353362 private static List <AttributeAssertion > elasticJobAttributes (
354363 String jobName ,
355364 long item ,
@@ -358,16 +367,35 @@ private static List<AttributeAssertion> elasticJobAttributes(
358367 String codeFunction ,
359368 String codeNamespace ,
360369 String jobType ) {
361- return asList (
362- equalTo (stringKey ("code.function" ), codeFunction ),
363- equalTo (stringKey ("code.namespace" ), codeNamespace ),
364- equalTo (stringKey ("job.system" ), "elasticjob" ),
365- equalTo (stringKey ("scheduling.apache-elasticjob.job.name" ), jobName ),
366- equalTo (stringKey ("scheduling.apache-elasticjob.job.type" ), jobType ),
367- equalTo (longKey ("scheduling.apache-elasticjob.sharding.item.index" ), item ),
368- equalTo (longKey ("scheduling.apache-elasticjob.sharding.total.count" ), totalCount ),
369- equalTo (stringKey ("scheduling.apache-elasticjob.sharding.item.parameter" ), parameter ),
370+ List <AttributeAssertion > assertions = new ArrayList <>();
371+
372+ assertions .add (equalTo (stringKey ("code.function" ), codeFunction ));
373+ assertions .add (equalTo (stringKey ("code.namespace" ), codeNamespace ));
374+
375+ assertions .add (equalTo (stringKey ("job.system" ), experimental ("elasticjob" )));
376+ assertions .add (
377+ equalTo (stringKey ("scheduling.apache-elasticjob.job.name" ), experimental (jobName )));
378+ assertions .add (
379+ equalTo (stringKey ("scheduling.apache-elasticjob.job.type" ), experimental (jobType )));
380+ assertions .add (
381+ equalTo (longKey ("scheduling.apache-elasticjob.sharding.item.index" ), experimental (item )));
382+ assertions .add (
383+ equalTo (
384+ longKey ("scheduling.apache-elasticjob.sharding.total.count" ),
385+ experimental (totalCount )));
386+ assertions .add (
387+ equalTo (
388+ stringKey ("scheduling.apache-elasticjob.sharding.item.parameter" ),
389+ experimental (parameter )));
390+ assertions .add (
370391 satisfies (
371- stringKey ("scheduling.apache-elasticjob.task.id" ), taskId -> taskId .contains (jobName )));
392+ stringKey ("scheduling.apache-elasticjob.task.id" ),
393+ taskId -> {
394+ if (EXPERIMENTAL_ATTRIBUTES_ENABLED ) {
395+ taskId .contains (jobName );
396+ }
397+ }));
398+
399+ return assertions ;
372400 }
373401}
0 commit comments