Search in sources :

Example 1 with Builder

use of zipkin2.elasticsearch.ElasticsearchStorage.Builder in project brave by openzipkin.

the class TracingStatementInterceptor method parseServerAddress.

/**
 * 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 parseServerAddress(Connection connection, Span span) {
    try {
        // strip "jdbc:"
        URI url = URI.create(connection.getMetaData().getURL().substring(5));
        int port = url.getPort() == -1 ? 3306 : url.getPort();
        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";
            }
        }
        Endpoint.Builder builder = Endpoint.newBuilder().serviceName(remoteServiceName).port(port);
        if (!builder.parseIp(connection.getHost()))
            return;
        span.remoteEndpoint(builder.build());
    } catch (Exception e) {
    // remote address is optional
    }
}
Also used : Endpoint(zipkin2.Endpoint) URI(java.net.URI) Endpoint(zipkin2.Endpoint) SQLException(java.sql.SQLException)

Example 2 with Builder

use of zipkin2.elasticsearch.ElasticsearchStorage.Builder in project brave by openzipkin.

the class TracingJdbcEventListener method parseServerAddress.

/**
 * This attempts to get the ip and port from the JDBC URL. Ex. localhost and 5555 from {@code
 * jdbc:mysql://localhost:5555/mydatabase}.
 */
void parseServerAddress(Connection connection, Span span) {
    try {
        // strip "jdbc:"
        final String urlAsString = connection.getMetaData().getURL().substring(5);
        // Remove all white space according to RFC 2396
        URI url = URI.create(urlAsString.replace(" ", ""));
        String defaultRemoteServiceName = remoteServiceName;
        Matcher matcher = URL_SERVICE_NAME_FINDER.matcher(url.toString());
        if (matcher.find() && matcher.groupCount() == 1) {
            String parsedServiceName = matcher.group(1);
            if (parsedServiceName != null && !parsedServiceName.isEmpty()) {
                // Do not override global service name if parsed service name is invalid
                defaultRemoteServiceName = parsedServiceName;
            }
        }
        Endpoint.Builder builder = Endpoint.newBuilder();
        int port = url.getPort();
        if (port > 0)
            builder.port(port);
        boolean parsed = builder.parseIp(url.getHost());
        if (defaultRemoteServiceName == null || "".equals(defaultRemoteServiceName)) {
            String databaseName = connection.getCatalog();
            if (databaseName != null && !databaseName.isEmpty()) {
                builder.serviceName(databaseName);
            } else {
                if (!parsed)
                    return;
            }
        } else {
            builder.serviceName(defaultRemoteServiceName);
        }
        span.remoteEndpoint(builder.build());
    } catch (Exception e) {
    // remote address is optional
    }
}
Also used : Endpoint(zipkin2.Endpoint) Matcher(java.util.regex.Matcher) URI(java.net.URI) Endpoint(zipkin2.Endpoint) SQLException(java.sql.SQLException)

Example 3 with Builder

use of zipkin2.elasticsearch.ElasticsearchStorage.Builder in project brave by openzipkin.

the class Platform method produceEndpoint.

Endpoint produceEndpoint() {
    Endpoint.Builder builder = Endpoint.newBuilder().serviceName("unknown");
    try {
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
        if (nics == null)
            return builder.build();
        while (nics.hasMoreElements()) {
            NetworkInterface nic = nics.nextElement();
            Enumeration<InetAddress> addresses = nic.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();
                if (address.isSiteLocalAddress()) {
                    builder.ip(address);
                    break;
                }
            }
        }
    } catch (Exception e) {
        // don't crash the caller if there was a problem reading nics.
        if (logger.isLoggable(Level.FINE)) {
            logger.log(Level.FINE, "error reading nics", e);
        }
    }
    return builder.build();
}
Also used : Endpoint(zipkin2.Endpoint) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 4 with Builder

use of zipkin2.elasticsearch.ElasticsearchStorage.Builder in project instrumentation-java by census-instrumentation.

the class ZipkinExporterHandler method produceLocalEndpoint.

/**
 * Logic borrowed from brave.internal.Platform.produceLocalEndpoint
 */
static Endpoint produceLocalEndpoint(String serviceName) {
    Endpoint.Builder builder = Endpoint.newBuilder().serviceName(serviceName);
    try {
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
        if (nics == null) {
            return builder.build();
        }
        while (nics.hasMoreElements()) {
            NetworkInterface nic = nics.nextElement();
            Enumeration<InetAddress> addresses = nic.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();
                if (address.isSiteLocalAddress()) {
                    builder.ip(address);
                    break;
                }
            }
        }
    } catch (Exception e) {
        // don't crash the caller if there was a problem reading nics.
        if (logger.isLoggable(Level.FINE)) {
            logger.log(Level.FINE, "error reading nics", e);
        }
    }
    return builder.build();
}
Also used : Endpoint(zipkin2.Endpoint) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) IOException(java.io.IOException)

Example 5 with Builder

use of zipkin2.elasticsearch.ElasticsearchStorage.Builder in project zipkin by openzipkin.

the class ITActiveMQCollector method skipsOnSpanStorageException.

/**
 * Guards against errors that leak from storage, such as InvalidQueryException
 */
@Test
public void skipsOnSpanStorageException() throws Exception {
    collector.close();
    AtomicInteger counter = new AtomicInteger();
    consumer = (input) -> new Call.Base<Void>() {

        @Override
        protected Void doExecute() {
            throw new AssertionError();
        }

        @Override
        protected void doEnqueue(Callback<Void> callback) {
            if (counter.getAndIncrement() == 1) {
                callback.onError(new RuntimeException("storage fell over"));
            } else {
                receivedSpans.add(spans);
                callback.onSuccess(null);
            }
        }

        @Override
        public Call<Void> clone() {
            throw new AssertionError();
        }
    };
    activemq.pushMessage(collector.queue, PROTO3.encodeList(spans));
    // tossed on error
    activemq.pushMessage(collector.queue, PROTO3.encodeList(spans));
    activemq.pushMessage(collector.queue, PROTO3.encodeList(spans));
    collector = builder().storage(buildStorage(consumer)).build().start();
    assertThat(receivedSpans.take()).containsExactlyElementsOf(spans);
    // the only way we could read this, is if the malformed span was skipped.
    assertThat(receivedSpans.take()).containsExactlyElementsOf(spans);
    assertThat(activemqMetrics.messages()).isEqualTo(3);
    // storage failure not message failure
    assertThat(activemqMetrics.messagesDropped()).isZero();
    assertThat(activemqMetrics.bytes()).isEqualTo(PROTO3.encodeList(spans).length * 3);
    assertThat(activemqMetrics.spans()).isEqualTo(spans.size() * 3);
    // only one dropped
    assertThat(activemqMetrics.spansDropped()).isEqualTo(spans.size());
}
Also used : Call(zipkin2.Call) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

Endpoint (zipkin2.Endpoint)8 Test (org.junit.Test)4 URI (java.net.URI)3 SQLException (java.sql.SQLException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Test (org.junit.jupiter.api.Test)3 Call (zipkin2.Call)3 HttpRequestWriter (com.linecorp.armeria.common.HttpRequestWriter)2 InetAddress (java.net.InetAddress)2 NetworkInterface (java.net.NetworkInterface)2 Benchmark (org.openjdk.jmh.annotations.Benchmark)2 HttpCall (zipkin2.elasticsearch.internal.client.HttpCall)2 V1Span (zipkin2.v1.V1Span)2 ClientFactory (com.linecorp.armeria.client.ClientFactory)1 ClientFactoryBuilder (com.linecorp.armeria.client.ClientFactoryBuilder)1 ClientOptions (com.linecorp.armeria.client.ClientOptions)1 ClientOptionsBuilder (com.linecorp.armeria.client.ClientOptionsBuilder)1 WebClient (com.linecorp.armeria.client.WebClient)1 WebClientBuilder (com.linecorp.armeria.client.WebClientBuilder)1 ContentPreviewingClient (com.linecorp.armeria.client.logging.ContentPreviewingClient)1