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 0faf66f
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 11 deletions.
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
60 changes: 60 additions & 0 deletions micrometer-tracing/src/main/java/io/micrometer/tracing/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,36 @@ public Span remoteIpAndPort(String ip, int port) {
*/
Span tag(String key, String value);

/**
* Sets a tag on this span.
* @param key tag key
* @param value tag value
* @return this span
*/
default Span tag(String key, long value) {
return tag(key, String.valueOf(value));
}

/**
* Sets a tag on this span.
* @param key tag key
* @param value tag value
* @return this span
*/
default Span tag(String key, double value) {
return tag(key, String.valueOf(value));
}

/**
* Sets a tag on this span.
* @param key tag key
* @param value tag value
* @return this span
*/
default Span tag(String key, boolean value) {
return tag(key, String.valueOf(value));
}

/**
* Records an exception for this span.
* @param throwable to record
Expand Down Expand Up @@ -333,6 +363,36 @@ public Span start() {
*/
Builder tag(String key, String value);

/**
* Sets a tag on the span.
* @param key tag key
* @param value tag value
* @return this
*/
default Builder tag(String key, long value) {
return tag(key, String.valueOf(value));
}

/**
* Sets a tag on the span.
* @param key tag key
* @param value tag value
* @return this
*/
default Builder tag(String key, double value) {
return tag(key, String.valueOf(value));
}

/**
* Sets a tag on the span.
* @param key tag key
* @param value tag value
* @return this
*/
default Builder tag(String key, boolean value) {
return tag(key, String.valueOf(value));
}

/**
* Sets an error on the span.
* @param throwable error to set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,36 @@ public SpanCustomizer event(String value) {
*/
SpanCustomizer tag(String key, String value);

/**
* Sets a tag on a span.
* @param key tag key
* @param value tag value
* @return this, for chaining
*/
default SpanCustomizer tag(String key, long value) {
return tag(key, String.valueOf(value));
}

/**
* Sets a tag on a span.
* @param key tag key
* @param value tag value
* @return this, for chaining
*/
default SpanCustomizer tag(String key, double value) {
return tag(key, String.valueOf(value));
}

/**
* Sets a tag on a span.
* @param key tag key
* @param value tag value
* @return this, for chaining
*/
default SpanCustomizer tag(String key, boolean value) {
return tag(key, String.valueOf(value));
}

/**
* Sets an event on a span.
* @param value event name
Expand Down

0 comments on commit 0faf66f

Please sign in to comment.