Skip to content

Commit

Permalink
support long, double and boolean for tags from on Observation layer #234
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikita Karaliou committed Apr 20, 2023
1 parent 0faf66f commit eb11083
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* 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;

import io.micrometer.common.KeyValue;

public abstract class TypedKeyValue<T> implements KeyValue {

protected final String key;

protected final T value;

TypedKeyValue(String key, T value) {
this.key = key;
this.value = value;
}

@Override
public String getKey() {
return key;
}

@Override
public String getValue() {
return String.valueOf(value);
}

public abstract void setAttribute(Span span);

public static TypedKeyValue<Long> of(String key, long value) {
return new TypedKeyValue<Long>(key, value) {
@Override
public void setAttribute(Span span) {
span.tag(getKey(), value);
}
};
}

public static TypedKeyValue<String> of(String key, String value) {
return new TypedKeyValue<String>(key, value) {
@Override
public void setAttribute(Span span) {
span.tag(getKey(), value);
}
};
}

public static TypedKeyValue<Double> of(String key, double value) {
return new TypedKeyValue<Double>(key, value) {
@Override
public void setAttribute(Span span) {
span.tag(getKey(), value);
}
};

}

public static TypedKeyValue<Boolean> of(String key, boolean value) {
return new TypedKeyValue<Boolean>(key, value) {
@Override
public void setAttribute(Span span) {
span.tag(getKey(), value);
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import io.micrometer.observation.Observation.Event;
import io.micrometer.observation.ObservationHandler;
import io.micrometer.observation.ObservationView;
import io.micrometer.tracing.CurrentTraceContext;
import io.micrometer.tracing.Span;
import io.micrometer.tracing.TraceContext;
import io.micrometer.tracing.Tracer;
import io.micrometer.tracing.*;
import io.micrometer.tracing.internal.SpanNameUtil;

import java.util.Map;
Expand All @@ -48,11 +45,14 @@ public interface TracingObservationHandler<T extends Observation.Context> extend
*/
default void tagSpan(T context, Span span) {
for (KeyValue keyValue : context.getAllKeyValues()) {
if (!keyValue.getKey().equalsIgnoreCase("ERROR")) {
span.tag(keyValue.getKey(), keyValue.getValue());
if (keyValue.getKey().equalsIgnoreCase("ERROR")) {
span.error(new RuntimeException(keyValue.getValue()));
}
else if (keyValue instanceof TypedKeyValue) {
((TypedKeyValue<?>) keyValue).setAttribute(span);
}
else {
span.error(new RuntimeException(keyValue.getValue()));
span.tag(keyValue.getKey(), keyValue.getValue());
}
}
}
Expand Down

0 comments on commit eb11083

Please sign in to comment.