String databaseName = connection.getCatalog(); if (databaseName != null && !databaseName.isEmpty()) { span.remoteServiceName(databaseName); span.remoteServiceName(defaultRemoteServiceName);
/** * MySQL exposes the host connecting to, but not the port. This attempts to get the port from the * JDBC URL. Ex. 5555 from {@code jdbc:mysql://localhost:5555/database}, or 3306 if absent. */ static void parseServerIpAndPort(Connection connection, Span span) { try { URI url = URI.create(connection.getMetaData().getURL().substring(5)); // strip "jdbc:" String remoteServiceName = connection.getProperties().getProperty("zipkinServiceName"); if (remoteServiceName == null || "".equals(remoteServiceName)) { String databaseName = connection.getCatalog(); if (databaseName != null && !databaseName.isEmpty()) { remoteServiceName = "mysql-" + databaseName; } else { remoteServiceName = "mysql"; } } span.remoteServiceName(remoteServiceName); String host = connection.getHost(); if (host != null) { span.remoteIpAndPort(host, url.getPort() == -1 ? 3306 : url.getPort()); } } catch (Exception e) { // remote address is optional } }
/** * MySQL exposes the host connecting to, but not the port. This attempts to get the port from the * JDBC URL. Ex. 5555 from {@code jdbc:mysql://localhost:5555/database}, or 3306 if absent. */ static void parseServerIpAndPort(MysqlConnection connection, Span span) { try { URI url = URI.create(connection.getURL().substring(5)); // strip "jdbc:" String remoteServiceName = connection.getProperties().getProperty("zipkinServiceName"); if (remoteServiceName == null || "".equals(remoteServiceName)) { String databaseName = getDatabaseName(connection); if (databaseName != null && !databaseName.isEmpty()) { remoteServiceName = "mysql-" + databaseName; } else { remoteServiceName = "mysql"; } } span.remoteServiceName(remoteServiceName); String host = getHost(connection); if (host != null) { span.remoteIpAndPort(host, url.getPort() == -1 ? 3306 : url.getPort()); } } catch (Exception e) { // remote address is optional } }
/** * MySQL exposes the host connecting to, but not the port. This attempts to get the port from the * JDBC URL. Ex. 5555 from {@code jdbc:mysql://localhost:5555/database}, or 3306 if absent. */ static void parseServerIpAndPort(MysqlConnection connection, Span span) { try { URI url = URI.create(connection.getURL().substring(5)); // strip "jdbc:" String remoteServiceName = connection.getProperties().getProperty("zipkinServiceName"); if (remoteServiceName == null || "".equals(remoteServiceName)) { String databaseName = getDatabaseName(connection); if (databaseName != null && !databaseName.isEmpty()) { remoteServiceName = "mysql-" + databaseName; } else { remoteServiceName = "mysql"; } } span.remoteServiceName(remoteServiceName); String host = getHost(connection); if (host != null) { span.remoteIpAndPort(host, url.getPort() == -1 ? 3306 : url.getPort()); } } catch (Exception e) { // remote address is optional } }
void setConsumerSpan(String topic, Span span) { span.name("poll").kind(Span.Kind.CONSUMER).tag(KafkaTags.KAFKA_TOPIC_TAG, topic); if (remoteServiceName != null) span.remoteServiceName(remoteServiceName); } }
@Override void parseRequest(Req request, Span span) { span.kind(Span.Kind.CLIENT); if (serverName != null) span.remoteServiceName(serverName); parser.request(adapter, request, span.customizer()); }
span.remoteServiceName(remoteServiceName);
/** * @deprecated Use {@link #remoteServiceName(String)} {@link #remoteIpAndPort(String, int)}. * Will be removed in Brave v6. */ @Deprecated public Span remoteEndpoint(Endpoint endpoint) { if (endpoint == null) return this; if (endpoint.serviceName() != null) remoteServiceName(endpoint.serviceName()); String ip = endpoint.ipv6() != null ? endpoint.ipv6() : endpoint.ipv4(); remoteIpAndPort(ip, endpoint.portAsInt()); return this; }
void setConsumerSpan(Span span, MessageProperties properties) { span.name("next-message").kind(CONSUMER); maybeTag(span, RABBIT_EXCHANGE, properties.getReceivedExchange()); maybeTag(span, RABBIT_ROUTING_KEY, properties.getReceivedRoutingKey()); maybeTag(span, RABBIT_QUEUE, properties.getConsumerQueue()); if (remoteServiceName != null) span.remoteServiceName(remoteServiceName); }
/** * This starts a consumer span as a child of the incoming message or the current trace * context, placing it in scope until the receive completes. */ @Override public Message<?> postReceive(Message<?> message, MessageChannel channel) { if (emptyMessage(message)) { return message; } MessageHeaderAccessor headers = mutableHeaderAccessor(message); TraceContextOrSamplingFlags extracted = this.extractor.extract(headers); Span span = this.threadLocalSpan.next(extracted); MessageHeaderPropagation.removeAnyTraceHeaders(headers, this.tracing.propagation().keys()); this.injector.inject(span.context(), headers); if (!span.isNoop()) { span.kind(Span.Kind.CONSUMER).name("receive").start(); span.remoteServiceName(REMOTE_SERVICE_NAME); addTags(message, span, channel); } if (log.isDebugEnabled()) { log.debug("Created a new span in post receive " + span); } headers.setImmutable(); return new GenericMessage<>(message.getPayload(), headers.getMessageHeaders()); }
if (!consumerSpan.isNoop()) { consumerSpan.kind(Span.Kind.CONSUMER).start(); consumerSpan.remoteServiceName(REMOTE_SERVICE_NAME); addTags(message, consumerSpan, channel); consumerSpan.finish();
/** * Starts and propagates {@link Span.Kind#PRODUCER} span for each message sent. */ @Override public Message<?> preSend(Message<?> message, MessageChannel channel) { if (emptyMessage(message)) { return message; } Message<?> retrievedMessage = getMessage(message); MessageHeaderAccessor headers = mutableHeaderAccessor(retrievedMessage); TraceContextOrSamplingFlags extracted = this.extractor.extract(headers); Span span = this.threadLocalSpan.next(extracted); MessageHeaderPropagation.removeAnyTraceHeaders(headers, this.tracing.propagation().keys()); this.injector.inject(span.context(), headers); if (!span.isNoop()) { span.kind(Span.Kind.PRODUCER).name("send").start(); span.remoteServiceName(REMOTE_SERVICE_NAME); addTags(message, span, channel); } if (log.isDebugEnabled()) { log.debug("Created a new span in pre send" + span); } Message<?> outputMessage = outputMessage(message, retrievedMessage, headers); if (isDirectChannel(channel)) { beforeHandle(outputMessage, channel, null); } return outputMessage; }
void handleReceive(Message message) { if (message == null || tracing.isNoop()) return; // remove prior propagation headers from the message TraceContextOrSamplingFlags extracted = jmsTracing.extractAndClearMessage(message); Span span = tracer.nextSpan(extracted); if (!span.isNoop()) { span.name("receive").kind(Span.Kind.CONSUMER); Destination destination = destination(message); if (destination != null) jmsTracing.tagQueueOrTopic(destination, span); if (remoteServiceName != null) span.remoteServiceName(remoteServiceName); // incur timestamp overhead only once long timestamp = tracing.clock(span.context()).currentTimeMicroseconds(); span.start(timestamp).finish(timestamp); } jmsTracing.setNextParent(message, span.context()); }
Span createAndStartProducerSpan(Destination destination, M message) { TraceContext maybeParent = current.get(); // Unlike message consumers, we try current span before trying extraction. This is the proper // order because the span in scope should take precedence over a potentially stale header entry. // // NOTE: Brave instrumentation used properly does not result in stale header entries, as we // always clear message headers after reading. Span span; if (maybeParent == null) { span = tracer.nextSpan(extractAndClearMessage(message)); } else { // As JMS is sensitive about write access to headers, we defensively clear even if it seems // upstream would have cleared (because there is a span in scope!). span = tracer.newChild(maybeParent); clearPropagationHeaders(message); } if (!span.isNoop()) { span.kind(Span.Kind.PRODUCER).name("send"); if (destination == null) destination = destination(message); if (destination != null) jmsTracing.tagQueueOrTopic(destination, span); if (remoteServiceName != null) span.remoteServiceName(remoteServiceName); span.start(); } addB3SingleHeader(message, span.context()); return span; }
if (remoteServiceName != null) span.remoteServiceName(remoteServiceName); if (record.key() instanceof String && !"".equals(record.key())) { span.tag(KafkaTags.KAFKA_KEY_TAG, record.key().toString());
@Override public Message postProcessMessage(Message message) { TraceContext maybeParent = currentTraceContext.get(); // Unlike message consumers, we try current span before trying extraction. This is the proper // order because the span in scope should take precedence over a potentially stale header entry. // // NOTE: Brave instrumentation used properly does not result in stale header entries, as we // always clear message headers after reading. Span span; if (maybeParent == null) { span = tracer.nextSpan(springRabbitTracing.extractAndClearHeaders(message)); } else { // If we have a span in scope assume headers were cleared before span = tracer.newChild(maybeParent); } if (!span.isNoop()) { span.kind(Span.Kind.PRODUCER).name("publish"); if (remoteServiceName != null) span.remoteServiceName(remoteServiceName); // incur timestamp overhead only once long timestamp = tracing.clock(span.context()).currentTimeMicroseconds(); span.start(timestamp).finish(timestamp); } injector.inject(span.context(), message.getMessageProperties()); return message; } }
Span startMessageListenerSpan(Message message) { if (!addConsumerSpan) return jmsTracing.nextSpan(message).name("on-message").start(); TraceContextOrSamplingFlags extracted = jmsTracing.extractAndClearMessage(message); // JMS has no visibility of the incoming message, which incidentally could be local! Span consumerSpan = tracer.nextSpan(extracted).kind(CONSUMER).name("receive"); Span listenerSpan = tracer.newChild(consumerSpan.context()); if (!consumerSpan.isNoop()) { long timestamp = tracing.clock(consumerSpan.context()).currentTimeMicroseconds(); consumerSpan.start(timestamp); if (remoteServiceName != null) consumerSpan.remoteServiceName(remoteServiceName); jmsTracing.tagQueueOrTopic(message, consumerSpan); long consumerFinish = timestamp + 1L; // save a clock reading consumerSpan.finish(consumerFinish); // not using scoped span as we want to start late listenerSpan.name("on-message").start(consumerFinish); } return listenerSpan; } }
boolean trySetPeer(brave.Span span, String key, String value) { if (Tags.PEER_SERVICE.getKey().equals(key)) { span.remoteServiceName(value); } else if (Tags.PEER_HOST_IPV4.getKey().equals(key)) { remoteIpV4 = value; } else if (Tags.PEER_HOST_IPV6.getKey().equals(key)) { remoteIpV6 = value; } else { return false; } return true; }
void setConsumerSpan(String topic, Span span) { span.name("poll").kind(Span.Kind.CONSUMER).tag(KafkaTags.KAFKA_TOPIC_TAG, topic); if (remoteServiceName != null) span.remoteServiceName(remoteServiceName); } }
/** * @deprecated Use {@link #remoteServiceName(String)} {@link #remoteIpAndPort(String, int)}. * Will be removed in Brave v6. */ @Deprecated public Span remoteEndpoint(Endpoint endpoint) { if (endpoint == null) return this; if (endpoint.serviceName() != null) remoteServiceName(endpoint.serviceName()); String ip = endpoint.ipv6() != null ? endpoint.ipv6() : endpoint.ipv4(); remoteIpAndPort(ip, endpoint.portAsInt()); return this; }