use of org.eclipse.hono.util.TenantTracingConfig in project hono by eclipse.
the class TenantTest method testDecodeTraceSampling.
/**
* Decode tenant with a "tracing" property set.
*/
@Test
public void testDecodeTraceSampling() {
final JsonObject tracingConfigJson = new JsonObject().put(RegistryManagementConstants.FIELD_TRACING_SAMPLING_MODE, TracingSamplingMode.ALL.getFieldValue()).put(RegistryManagementConstants.FIELD_TRACING_SAMPLING_MODE_PER_AUTH_ID, new JsonObject().put("authId1", TracingSamplingMode.ALL.getFieldValue()).put("authId2", TracingSamplingMode.DEFAULT.getFieldValue()));
final JsonObject tenantJson = new JsonObject();
tenantJson.put(RegistryManagementConstants.FIELD_TRACING, tracingConfigJson);
final var tenant = tenantJson.mapTo(Tenant.class);
assertNotNull(tenant);
final TenantTracingConfig tracingConfig = tenant.getTracing();
assertNotNull(tracingConfig);
assertEquals(TracingSamplingMode.ALL, tracingConfig.getSamplingMode());
assertEquals(TracingSamplingMode.ALL, tracingConfig.getSamplingModePerAuthId().get("authId1"));
assertEquals(TracingSamplingMode.DEFAULT, tracingConfig.getSamplingModePerAuthId().get("authId2"));
}
use of org.eclipse.hono.util.TenantTracingConfig in project hono by eclipse.
the class TenantTest method testEncodeTraceSamplingModePerAuthId.
/**
* Encode tenant with a "tracing" value set.
*/
@Test
public void testEncodeTraceSamplingModePerAuthId() {
final var tenant = new Tenant();
final TenantTracingConfig tracingConfig = new TenantTracingConfig();
tracingConfig.setSamplingMode(TracingSamplingMode.ALL);
tracingConfig.setSamplingModePerAuthId(Map.of("authId1", TracingSamplingMode.ALL, "authId2", TracingSamplingMode.DEFAULT));
tenant.setTracing(tracingConfig);
final var json = JsonObject.mapFrom(tenant);
assertNotNull(json);
final JsonObject tracingConfigJson = json.getJsonObject(RegistryManagementConstants.FIELD_TRACING);
assertNotNull(tracingConfigJson);
assertEquals(TracingSamplingMode.ALL.getFieldValue(), tracingConfigJson.getString(RegistryManagementConstants.FIELD_TRACING_SAMPLING_MODE));
final JsonObject traceSamplingModePerAuthIdJson = tracingConfigJson.getJsonObject(RegistryManagementConstants.FIELD_TRACING_SAMPLING_MODE_PER_AUTH_ID);
assertNotNull(traceSamplingModePerAuthIdJson);
assertEquals(TracingSamplingMode.ALL.getFieldValue(), traceSamplingModePerAuthIdJson.getString("authId1"));
assertEquals(TracingSamplingMode.DEFAULT.getFieldValue(), traceSamplingModePerAuthIdJson.getString("authId2"));
}
use of org.eclipse.hono.util.TenantTracingConfig in project hono by eclipse.
the class TracingSupportingHonoResourceTest method testApplyTenantTraceSamplingPrioritySetForAuthId.
/**
* Verifies that the resource sets the trace sampling priority on the newly created Span if the CoAP request
* belongs to a tenant and an auth-id for which a specific sampling priority is configured.
*/
@Test
public void testApplyTenantTraceSamplingPrioritySetForAuthId() {
final TenantObject tenantObject = TenantObject.from(TENANT_ID, true);
final TenantTracingConfig tracingConfig = new TenantTracingConfig();
tracingConfig.setSamplingModePerAuthId(Map.of(AUTH_ID, TracingSamplingMode.ALL));
tenantObject.setTracingConfig(tracingConfig);
when(tenantClient.get(anyString(), (SpanContext) any())).thenReturn(Future.succeededFuture(tenantObject));
final Request request = new Request(Code.POST);
final Exchange exchange = newExchange(request);
resource.handleRequest(exchange);
verify(tracer).buildSpan(eq(Code.POST.toString()));
verify(spanBuilder).withTag(eq(Tags.SPAN_KIND.getKey()), eq(Tags.SPAN_KIND_SERVER.toString()));
verify(spanBuilder).addReference(eq(References.CHILD_OF), isNull());
// verify sampling prio has been set to 1 (corresponding to TracingSamplingMode.ALL)
verify(span).setTag(eq(Tags.SAMPLING_PRIORITY.getKey()), eq(1));
verify(span).setBaggageItem(eq(Tags.SAMPLING_PRIORITY.getKey()), eq("1"));
}
use of org.eclipse.hono.util.TenantTracingConfig in project hono by eclipse.
the class TracingSupportingHonoResourceTest method testApplyTenantTraceSamplingPriority.
/**
* Verifies that the resource sets the trace sampling priority on the newly created Span if the CoAP request
* belongs to a tenant for which a specific sampling priority is configured.
*/
@Test
public void testApplyTenantTraceSamplingPriority() {
final TenantObject tenantObject = TenantObject.from(TENANT_ID, true);
final TenantTracingConfig tracingConfig = new TenantTracingConfig();
tracingConfig.setSamplingMode(TracingSamplingMode.NONE);
tenantObject.setTracingConfig(tracingConfig);
when(tenantClient.get(anyString(), (SpanContext) any())).thenReturn(Future.succeededFuture(tenantObject));
final Request request = new Request(Code.POST);
final Exchange exchange = newExchange(request);
resource.handleRequest(exchange);
verify(tracer).buildSpan(eq(Code.POST.toString()));
verify(spanBuilder).withTag(eq(Tags.SPAN_KIND.getKey()), eq(Tags.SPAN_KIND_SERVER.toString()));
verify(spanBuilder).addReference(eq(References.CHILD_OF), isNull());
// verify sampling prio has been set to 0 (corresponding to TracingSamplingMode.NONE)
verify(span).setTag(eq(Tags.SAMPLING_PRIORITY.getKey()), eq(0));
verify(span).setBaggageItem(eq(Tags.SAMPLING_PRIORITY.getKey()), eq("0"));
}
use of org.eclipse.hono.util.TenantTracingConfig in project hono by eclipse.
the class DeviceRegistryUtilsTest method testTenantConversion.
/**
* Verifies the conversion of a {@link Tenant} instance to a {@link org.eclipse.hono.util.TenantObject}.
*/
@Test
public void testTenantConversion() {
final TenantTracingConfig tracingConfig = new TenantTracingConfig();
tracingConfig.setSamplingMode(TracingSamplingMode.ALL);
tracingConfig.setSamplingModePerAuthId(Map.of("authId1", TracingSamplingMode.ALL, "authId2", TracingSamplingMode.DEFAULT));
final TrustedCertificateAuthority ca1 = new TrustedCertificateAuthority().setSubjectDn("CN=test.org").setKeyAlgorithm("EC").setPublicKey("NOT_A_PUBLIC_KEY".getBytes()).setNotBefore(Instant.now().minus(1, ChronoUnit.DAYS)).setNotAfter(Instant.now().plus(2, ChronoUnit.DAYS)).setAuthIdTemplate("auth-{{subject-cn}}").setAutoProvisioningAsGatewayEnabled(true).setAutoProvisioningDeviceIdTemplate("device-{{subject-dn}}");
final TrustedCertificateAuthority ca2 = new TrustedCertificateAuthority().setSubjectDn("CN=test.org").setKeyAlgorithm("RSA").setPublicKey("NOT_A_PUBLIC_KEY".getBytes()).setNotBefore(Instant.now().plus(1, ChronoUnit.DAYS)).setNotAfter(Instant.now().plus(20, ChronoUnit.DAYS)).setAuthIdTemplate("auth-{{subject-cn}}").setAutoProvisioningAsGatewayEnabled(true).setAutoProvisioningDeviceIdTemplate("device-{{subject-dn}}");
final Tenant source = new Tenant();
source.setEnabled(true);
source.setTracing(tracingConfig);
source.setDefaults(Map.of("ttl", 30));
source.setExtensions(Map.of("custom", "value"));
source.setTrustedCertificateAuthorities(List.of(ca1, ca2));
final JsonObject tracingConfigJsonObject = new JsonObject();
tracingConfigJsonObject.put(TenantConstants.FIELD_TRACING_SAMPLING_MODE, "all");
final JsonObject tracingSamplingModeJsonObject = new JsonObject().put("authId1", "all").put("authId2", "default");
tracingConfigJsonObject.put(TenantConstants.FIELD_TRACING_SAMPLING_MODE_PER_AUTH_ID, tracingSamplingModeJsonObject);
final JsonArray expectedAuthorities = new JsonArray().add(new JsonObject().put(TenantConstants.FIELD_PAYLOAD_SUBJECT_DN, "CN=test.org").put(TenantConstants.FIELD_PAYLOAD_PUBLIC_KEY, "NOT_A_PUBLIC_KEY".getBytes()).put(TenantConstants.FIELD_PAYLOAD_KEY_ALGORITHM, "EC").put(TenantConstants.FIELD_PAYLOAD_AUTH_ID_TEMPLATE, "auth-{{subject-cn}}").put(TenantConstants.FIELD_AUTO_PROVISIONING_ENABLED, false));
final JsonObject target = DeviceRegistryUtils.convertTenant("4711", source, true);
assertThat(target.getString(TenantConstants.FIELD_PAYLOAD_TENANT_ID)).isEqualTo("4711");
assertThat(target.getBoolean(TenantConstants.FIELD_ENABLED)).isTrue();
assertThat(target.getJsonObject(TenantConstants.FIELD_TRACING)).isEqualTo(tracingConfigJsonObject);
assertThat(target.getJsonArray(TenantConstants.FIELD_PAYLOAD_TRUSTED_CA)).isEqualTo(expectedAuthorities);
assertThat(target.getJsonArray(TenantConstants.FIELD_ADAPTERS)).isNull();
final JsonObject defaults = target.getJsonObject(TenantConstants.FIELD_PAYLOAD_DEFAULTS);
assertThat(defaults).isNotNull();
assertThat(defaults.getInteger("ttl")).isEqualTo(30);
final JsonObject extensions = target.getJsonObject(RegistryManagementConstants.FIELD_EXT);
assertThat(extensions).isNotNull();
assertThat(extensions.getString("custom")).isEqualTo("value");
// Verify that the internal attributes to the device registry are not transferred to the TenantObject
assertThat(expectedAuthorities.getJsonObject(0).containsKey(RegistryManagementConstants.FIELD_AUTO_PROVISION_AS_GATEWAY)).isFalse();
assertThat(expectedAuthorities.getJsonObject(0).containsKey(RegistryManagementConstants.FIELD_AUTO_PROVISIONING_DEVICE_ID_TEMPLATE)).isFalse();
}
Aggregations