Skip to content

Commit

Permalink
support long, double and boolean for tags micrometer-metrics#234
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikita Karaliou committed Apr 19, 2023
1 parent 7ff0829 commit 8fc2f0d
Show file tree
Hide file tree
Showing 16 changed files with 374 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ public Span tag(String key, String value) {
return this;
}

@Override
public Span tag(String key, long value) {
return tag(key, String.valueOf(value));
}

@Override
public Span tag(String key, double value) {
return tag(key, String.valueOf(value));
}

@Override
public Span tag(String key, boolean value) {
return tag(key, String.valueOf(value));
}

@Override
public Span error(Throwable throwable) {
String message = throwable.getMessage() == null ? throwable.getClass().getSimpleName() : throwable.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ public Span.Builder tag(String key, String value) {
return this;
}

@Override
public Span.Builder tag(String key, long value) {
return tag(key, String.valueOf(value));
}

@Override
public Span.Builder tag(String key, double value) {
return tag(key, String.valueOf(value));
}

@Override
public Span.Builder tag(String key, boolean value) {
return tag(key, String.valueOf(value));
}

@Override
public Span.Builder error(Throwable throwable) {
this.error = throwable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ public SpanCustomizer tag(String key, String value) {
return new BraveSpanCustomizer(this.spanCustomizer.tag(key, value));
}

@Override
public SpanCustomizer tag(String key, long value) {
return tag(key, String.valueOf(value));
}

@Override
public SpanCustomizer tag(String key, double value) {
return tag(key, String.valueOf(value));
}

@Override
public SpanCustomizer tag(String key, boolean value) {
return tag(key, String.valueOf(value));
}

@Override
public SpanCustomizer event(String value) {
return new BraveSpanCustomizer(this.spanCustomizer.annotate(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;

class BraveSpanBuilderTests {
Expand Down Expand Up @@ -70,6 +71,21 @@ void should_set_links() {
.containsEntry("links[1].spanId", span1.context().spanId());
}

@Test
void should_set_non_string_tags() {
new BraveSpanBuilder(tracing.tracer()).tag("string", "string")
.tag("double", 2.5)
.tag("long", 2)
.tag("boolean", true)
.start()
.end();

assertThat(handler.get(0).tags()).containsEntry("string", "string")
.containsEntry("double", "2.5")
.containsEntry("long", "2")
.containsEntry("boolean", "true");
}

private Map<String, String> tags() {
Map<String, String> map = new HashMap<>();
map.put("tag1", "value1");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2022 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.brave.bridge;

import brave.Tracing;
import brave.test.TestSpanHandler;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class BraveSpanTest {

TestSpanHandler handler = new TestSpanHandler();

Tracing tracing = Tracing.newBuilder().addSpanHandler(handler).build();

@Test
void should_set_non_string_tags() {
new BraveSpan(tracing.tracer().nextSpan()).start()
.tag("string", "string")
.tag("double", 2.5)
.tag("long", 2)
.tag("boolean", true)
.end();

assertThat(handler.get(0).tags()).containsEntry("string", "string")
.containsEntry("double", "2.5")
.containsEntry("long", "2")
.containsEntry("boolean", "true");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ public Span tag(String key, String value) {
return new OtelSpan(this.delegate);
}

@Override
public Span tag(String key, long value) {
this.delegate.setAttribute(key, value);
return new OtelSpan(this.delegate);
}

@Override
public Span tag(String key, double value) {
this.delegate.setAttribute(key, value);
return new OtelSpan(this.delegate);
}

@Override
public Span tag(String key, boolean value) {
this.delegate.setAttribute(key, value);
return new OtelSpan(this.delegate);
}

@Override
public void end(long time, TimeUnit timeUnit) {
this.delegate.end(time, timeUnit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ public Span.Builder tag(String key, String value) {
return this;
}

@Override
public Span.Builder tag(String key, long value) {
this.delegate.setAttribute(key, value);
return this;
}

@Override
public Span.Builder tag(String key, double value) {
this.delegate.setAttribute(key, value);
return this;
}

@Override
public Span.Builder tag(String key, boolean value) {
this.delegate.setAttribute(key, value);
return this;
}

@Override
public Span.Builder error(Throwable throwable) {
this.error = throwable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ public SpanCustomizer tag(String key, String value) {
return this;
}

@Override
public SpanCustomizer tag(String key, long value) {
currentSpan().setAttribute(key, value);
return this;
}

@Override
public SpanCustomizer tag(String key, double value) {
currentSpan().setAttribute(key, value);
return this;
}

@Override
public SpanCustomizer tag(String key, boolean value) {
currentSpan().setAttribute(key, value);
return this;
}

@Override
public SpanCustomizer event(String value) {
currentSpan().addEvent(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.micrometer.tracing.Link;
import io.micrometer.tracing.Span;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.extension.trace.propagation.B3Propagator;
import io.opentelemetry.sdk.OpenTelemetrySdk;
Expand All @@ -29,6 +30,8 @@
import java.util.stream.Collectors;

import static org.assertj.core.api.BDDAssertions.then;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class OtelSpanBuilderTests {

Expand Down Expand Up @@ -80,6 +83,22 @@ void should_set_links() {
.collect(Collectors.toMap(e -> e.getKey().getKey(), e -> e.getValue().toString()))).isEqualTo(tags());
}

@Test
void should_set_non_string_tags() {
new OtelSpanBuilder(otelTracer.spanBuilder("foo")).tag("string", "string")
.tag("double", 2.5)
.tag("long", 2)
.tag("boolean", true)
.start()
.end();

SpanData poll = processor.spans().poll();
assertEquals("string", poll.getAttributes().get(AttributeKey.stringKey("string")));
assertEquals(2.5, poll.getAttributes().get(AttributeKey.doubleKey("double")));
assertEquals(2, poll.getAttributes().get(AttributeKey.longKey("long")));
assertTrue(poll.getAttributes().get(AttributeKey.booleanKey("boolean")));
}

private Map<String, String> tags() {
Map<String, String> map = new HashMap<>();
map.put("tag1", "value1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,27 @@

class OtelSpanTests {

ArrayListSpanProcessor processor = new ArrayListSpanProcessor();

SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
.setSampler(io.opentelemetry.sdk.trace.samplers.Sampler.alwaysOn())
.addSpanProcessor(processor)
.build();

OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder()
.setTracerProvider(sdkTracerProvider)
.setPropagators(ContextPropagators.create(B3Propagator.injectingSingleHeader()))
.build();

io.opentelemetry.api.trace.Tracer otelTracer = openTelemetrySdk.getTracer("io.micrometer.micrometer-tracing");

@Test
void should_set_status_to_error_when_recording_exception() {
ArrayListSpanProcessor arrayListSpanProcessor = new ArrayListSpanProcessor();
SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
.setSampler(io.opentelemetry.sdk.trace.samplers.Sampler.alwaysOn())
.addSpanProcessor(arrayListSpanProcessor)
.build();
OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder()
.setTracerProvider(sdkTracerProvider)
.setPropagators(ContextPropagators.create(B3Propagator.injectingSingleHeader()))
.build();
io.opentelemetry.api.trace.Tracer otelTracer = openTelemetrySdk.getTracer("io.micrometer.micrometer-tracing");
OtelSpan otelSpan = new OtelSpan(otelTracer.spanBuilder("foo").startSpan());

otelSpan.error(new RuntimeException("boom!")).end();

SpanData poll = arrayListSpanProcessor.spans().poll();
SpanData poll = processor.spans().poll();
then(poll.getStatus()).isEqualTo(StatusData.create(StatusCode.ERROR, "boom!"));
then(poll.getEvents()).hasSize(1);
then(poll.getEvents().get(0).getAttributes().asMap().values()).containsAnyOf("boom!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ public SimpleSpan tag(String key, String value) {
return this;
}

@Override
public Span tag(String key, long value) {
return tag(key, String.valueOf(value));
}

@Override
public Span tag(String key, double value) {
return tag(key, String.valueOf(value));
}

@Override
public Span tag(String key, boolean value) {
return tag(key, String.valueOf(value));
}

@Override
public SimpleSpan error(Throwable throwable) {
this.throwable = throwable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ public Span.Builder tag(String key, String value) {
return this;
}

@Override
public Span.Builder tag(String key, long value) {
return tag(key, String.valueOf(value));
}

@Override
public Span.Builder tag(String key, double value) {
return tag(key, String.valueOf(value));
}

@Override
public Span.Builder tag(String key, boolean value) {
return tag(key, String.valueOf(value));
}

@Override
public Span.Builder error(Throwable throwable) {
this.throwable = throwable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ public SpanCustomizer tag(String key, String value) {
return this;
}

@Override
public SpanCustomizer tag(String key, long value) {
currentSpan().tag(key, value);
return this;
}

@Override
public SpanCustomizer tag(String key, double value) {
currentSpan().tag(key, value);
return this;
}

@Override
public SpanCustomizer tag(String key, boolean value) {
currentSpan().tag(key, value);
return this;
}

@Override
public SpanCustomizer event(String value) {
currentSpan().event(value);
Expand Down
Loading

0 comments on commit 8fc2f0d

Please sign in to comment.