Closed wapkch closed 1 year ago
SenderContext#remoteServiceAddress
is nullable and datasource-micrometer does not populate this, nor the http.uri
or uri
tags.
On the other hand, SenderContext#remoteServiceName
is populated with the datasource name by default.
So, instead of remoteServiceAddress
, I suggest using the remoteServiceName
in your logic.
Or, you may add an ObservationFilter
that populates remoteServiceAddress
.
So, instead of remoteServiceAddress, I suggest using the remoteServiceName in your logic.
Actually, it's the logic of apm-toolkit-micrometer-activation in skywalking java agent.
cc @marcingrzejszczak Is there a possibility that we can just use the remoteServiceName in SpanHelper#tryToGetPeer
?
class SpanHelper {
static String tryToGetPeer(String remoteAddress, KeyValues allKeyValues) {
if (remoteAddress != null) {
return remoteAddress;
}
String result = allKeyValues
.stream()
.filter(keyValue -> "http.url".equalsIgnoreCase(
keyValue.getKey()) || "uri".equalsIgnoreCase(keyValue.getKey())
|| keyValue.getKey().contains("uri") || keyValue.getKey().contains("url")
)
.findFirst()
.map(KeyValue::getValue)
.orElse("unknown");
try {
URI uri = URI.create(result);
if (uri.getHost() == null) {
return null;
}
return uri.getHost() + ":" + uri.getPort();
} catch (Exception ex) {
return null;
}
}
}
Or, you may add an ObservationFilter that populates remoteServiceAddress.
ObservationFilter only modify the Observation.Context on stopping the Observation. But it happens here on starting the Observation
public class MicrometerSenderTracingHandlerInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
String methodName = method.getName();
if ("onStart".equals(methodName)) {
SenderContext<Object> context = (SenderContext<Object>) allArguments[0];
final ContextCarrier contextCarrier = new ContextCarrier();
AbstractSpan span = ContextManager.createExitSpan(
getOperationName(context), contextCarrier, SpanHelper.tryToGetPeer(context.getRemoteServiceAddress(), context.getAllKeyValues()));
CarrierItem next = contextCarrier.items();
while (next.hasNext()) {
next = next.next();
context.getSetter().set(context.getCarrier(), next.getHeadKey(), next.getHeadValue());
}
span.setComponent(ComponentsDefine.MICROMETER);
}
...
}
}
Actually, it's the logic of apm-toolkit-micrometer-activation in skywalking java agent. cc @marcingrzejszczak Is there a possibility that we can just use the remoteServiceName in SpanHelper#tryToGetPeer?
Sure, file a PR in skywalking and mention me there so I can review it
I tried to use Micrometer to report datasource traces to Skywalking OAP server. maven dependencies are following:
ObservationRegistryCustomizer:
When i start the application and invoke
repository.saveAll(list);
, the skywalking-api.log has errors:This is because the
new QueryContext()
in DataSourceObservationListener has noremoteServiceAddress
and neitherkeyValues
in QueryObservationConvention, which skywalking agent can not accept.