Skip to content

Commit

Permalink
Added missing setup of SimpleTraceContext through SimpleTraceContextB…
Browse files Browse the repository at this point in the history
…uilder

fixes gh-500
  • Loading branch information
marcingrzejszczak committed Dec 15, 2023
1 parent bf0dd60 commit eea0bcc
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,54 @@
*/
public class SimpleTraceContextBuilder implements TraceContext.Builder {

private String traceId;

private String parentId;

private String spanId;

private Boolean sampled;

@Override
public TraceContext.Builder traceId(String traceId) {
this.traceId = traceId;
return this;
}

@Override
public TraceContext.Builder parentId(String parentId) {
this.parentId = parentId;
return this;
}

@Override
public TraceContext.Builder spanId(String spanId) {
this.spanId = spanId;
return this;
}

@Override
public TraceContext.Builder sampled(Boolean sampled) {
this.sampled = sampled;
return this;
}

@Override
public TraceContext build() {
return new SimpleTraceContext();
SimpleTraceContext traceContext = new SimpleTraceContext();
if (this.traceId != null) {
traceContext.setTraceId(this.traceId);
}
if (this.spanId != null) {
traceContext.setSpanId(this.spanId);
}
if (this.parentId != null) {
traceContext.setParentId(this.parentId);
}
if (this.sampled != null) {
traceContext.setSampled(this.sampled);
}
return traceContext;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2023 the original author or authors.
*
* 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
*
* https://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 io.micrometer.tracing.test.simple;

import io.micrometer.tracing.TraceContext;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;

class SimpleTraceContextBuilderTests {

@Test
void should_build_trace_context_through_builder_with_default_values() {
TraceContext traceContext = new SimpleTraceContextBuilder().build();

SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(traceContext.traceId()).isEmpty();
softAssertions.assertThat(traceContext.spanId()).isEmpty();
softAssertions.assertThat(traceContext.parentId()).isEmpty();
softAssertions.assertThat(traceContext.sampled()).isFalse();
});
}

@Test
void should_build_trace_context_through_builder_with_overridden_values() {
TraceContext traceContext = new SimpleTraceContextBuilder().traceId("1")
.spanId("2")
.parentId("3")
.sampled(true)
.build();

SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(traceContext.traceId()).isEqualTo("1");
softAssertions.assertThat(traceContext.spanId()).isEqualTo("2");
softAssertions.assertThat(traceContext.parentId()).isEqualTo("3");
softAssertions.assertThat(traceContext.sampled()).isTrue();
});
}

}

0 comments on commit eea0bcc

Please sign in to comment.