Skip to content

Commit

Permalink
Add tests for the Span and Baggage beans (#94)
Browse files Browse the repository at this point in the history
* Start adding constants for test properties

* Add a test for the Baggage bean

Ensure the Baggage bean continues to reflect the current baggage even
when the current baggage changes.

* Add a test for the Span bean

Ensure the Span bean continues to reflect the current span when we
change which is the current span.
  • Loading branch information
Azquelt committed Jun 16, 2023
1 parent bac6230 commit f9c2cae
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 1 deletion.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2022-2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down Expand Up @@ -29,6 +29,8 @@

public class ConfigAsset implements Asset {

public static final String SDK_DISABLED = "otel.sdk.disabled";

private Properties properties = new Properties();

@Override
Expand Down
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.eclipse.microprofile.telemetry.tracing.tck.cdi;

import static org.eclipse.microprofile.telemetry.tracing.tck.ConfigAsset.SDK_DISABLED;

import org.eclipse.microprofile.telemetry.tracing.tck.ConfigAsset;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.Assert;
import org.testng.annotations.Test;

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.context.Scope;
import jakarta.inject.Inject;

public class BaggageBeanTest extends Arquillian {

@Deployment
public static WebArchive createDeployment() {
ConfigAsset config = new ConfigAsset().add(SDK_DISABLED, "false");

return ShrinkWrap.create(WebArchive.class)
.addAsResource(config, "META-INF/microprofile-config.properties");
}

@Inject
private Baggage injectedBaggage;

/**
* Test that the injected {@link Baggage} bean always reflects the current {@link Baggage} instance
*/
@Test
public void baggageBeanChange() {
String key = "testKey";

// Default Baggage should not have our key
Assert.assertNull(injectedBaggage.getEntryValue(key));

Baggage baggage1 = Baggage.builder().put(key, "value1").build();

// After creating a new baggage, the current baggage still doesn't have our key
Assert.assertNull(injectedBaggage.getEntryValue(key));

// Make baggage1 the current baggage
try (Scope s = baggage1.makeCurrent()) {
// Baggage bean should now return our test key
Assert.assertEquals(injectedBaggage.getEntryValue(key), "value1");

// Creating another new baggage does not change the current baggage
Baggage baggage2 = Baggage.builder().put(key, "value2").build();
Assert.assertEquals(injectedBaggage.getEntryValue(key), "value1");

try (Scope s2 = baggage2.makeCurrent()) {
// but if we make baggage2 current, then the bean now has the value from baggage2
Assert.assertEquals(injectedBaggage.getEntryValue(key), "value2");
}

// After reverting to baggage1 as the current baggage
Assert.assertEquals(injectedBaggage.getEntryValue(key), "value1");
}

// After reverting to the original baggage
Assert.assertNull(injectedBaggage.getEntryValue(key));
}
}
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.eclipse.microprofile.telemetry.tracing.tck.cdi;

import static org.eclipse.microprofile.telemetry.tracing.tck.ConfigAsset.SDK_DISABLED;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;

import org.eclipse.microprofile.telemetry.tracing.tck.ConfigAsset;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.annotations.Test;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Scope;
import jakarta.inject.Inject;

public class SpanBeanTest extends Arquillian {

@Deployment
public static WebArchive createDeployment() {
ConfigAsset config = new ConfigAsset().add(SDK_DISABLED, "false");

return ShrinkWrap.create(WebArchive.class)
.addAsResource(config, "META-INF/microprofile-config.properties");
}

@Inject
private Span injectedSpan;

@Inject
private Tracer tracer;

@Test
public void spanBeanChange() {
Span originalSpan = Span.current();
// Check the injected span reflects the current span initially
assertEquals(originalSpan.getSpanContext().getSpanId(), injectedSpan.getSpanContext().getSpanId());

// Create a new span
Span span1 = tracer.spanBuilder("span1").startSpan();
// Check we have a real span with a different spanId
assertNotEquals(originalSpan.getSpanContext().getSpanId(), span1.getSpanContext().getSpanId());

// The original span should still be "current", so the injected span should still reflect it
assertEquals(originalSpan.getSpanContext().getSpanId(), injectedSpan.getSpanContext().getSpanId());

// Make span1 current
try (Scope s = span1.makeCurrent()) {
// Now the injected span should reflect span1
assertEquals(span1.getSpanContext().getSpanId(), injectedSpan.getSpanContext().getSpanId());

// Make a new span
Span span2 = tracer.spanBuilder("span2").startSpan();
// Injected span should still reflect span1
assertEquals(span1.getSpanContext().getSpanId(), injectedSpan.getSpanContext().getSpanId());

// Make span2 current
try (Scope s2 = span2.makeCurrent()) {
// Now the injected span should reflect span2
assertEquals(span2.getSpanContext().getSpanId(), injectedSpan.getSpanContext().getSpanId());
} finally {
span2.end();
}

// After closing the scope, span1 is current again and the injected bean should reflect that
assertEquals(span1.getSpanContext().getSpanId(), injectedSpan.getSpanContext().getSpanId());
} finally {
span1.end();
}

// After closing the scope, the original span is current again
assertEquals(originalSpan.getSpanContext().getSpanId(), injectedSpan.getSpanContext().getSpanId());
}

}

0 comments on commit f9c2cae

Please sign in to comment.