Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ pom.xml
</dependency>
```

#### Elasticsearch 7

```xml
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-elasticsearch7-client</artifactId>
<version>VERSION</version>
</dependency>
```

## Usage

```java
Expand Down
50 changes: 50 additions & 0 deletions opentracing-elasticsearch7-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2017-2019 The OpenTracing 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

http://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.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>opentracing-elasticsearch-client-parent</artifactId>
<groupId>io.opentracing.contrib</groupId>
<version>0.1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>opentracing-elasticsearch7-client</artifactId>
<name>OpenTracing Instrumentation for Elasticsearch 7 Client</name>
<description>OpenTracing Instrumentation for Elasticsearch 7 Client</description>

<dependencies>
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-elasticsearch-client-common</artifactId>
<version>0.1.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch7.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2017-2019 The OpenTracing 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
*
* http://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.opentracing.contrib.elasticsearch7;

import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.contrib.elasticsearch.common.SpanDecorator;
import io.opentracing.tag.Tags;
import io.opentracing.util.GlobalTracer;
import java.util.Collection;
import org.elasticsearch.action.Action;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.transport.client.PreBuiltTransportClient;


public class TracingPreBuiltTransportClient extends PreBuiltTransportClient {

private final Tracer tracer;

@SafeVarargs
public TracingPreBuiltTransportClient(Tracer tracer, Settings settings,
Class<? extends Plugin>... plugins) {
super(settings, plugins);
this.tracer = tracer;
}

/**
* GlobalTracer is used to get tracer
*/
@SafeVarargs
public TracingPreBuiltTransportClient(Settings settings,
Class<? extends Plugin>... plugins) {
this(GlobalTracer.get(), settings, plugins);
}

public TracingPreBuiltTransportClient(Tracer tracer, Settings settings,
Collection<Class<? extends Plugin>> plugins) {
super(settings, plugins);
this.tracer = tracer;
}

/**
* GlobalTracer is used to get tracer
*/
public TracingPreBuiltTransportClient(Settings settings,
Collection<Class<? extends Plugin>> plugins) {
this(GlobalTracer.get(), settings, plugins);
}

public TracingPreBuiltTransportClient(Tracer tracer, Settings settings,
Collection<Class<? extends Plugin>> plugins,
HostFailureListener hostFailureListener) {
super(settings, plugins, hostFailureListener);
this.tracer = tracer;
}

/**
* GlobalTracer is used to get tracer
*/
public TracingPreBuiltTransportClient(Settings settings,
Collection<Class<? extends Plugin>> plugins,
HostFailureListener hostFailureListener) {
this(GlobalTracer.get(), settings, plugins, hostFailureListener);
}

@Override
protected <Request extends ActionRequest, Response extends ActionResponse>
void doExecute(Action<Response> action, Request request, ActionListener<Response> listener) {
Tracer.SpanBuilder spanBuilder = tracer.buildSpan(request.getClass().getSimpleName())
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);

Span span = spanBuilder.start();
SpanDecorator.onRequest(span);

ActionListener<Response> actionFuture = new TracingResponseListener<>(listener, span);
super.doExecute(action, request, actionFuture);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2017-2019 The OpenTracing 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
*
* http://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.opentracing.contrib.elasticsearch7;

import io.opentracing.Span;
import io.opentracing.contrib.elasticsearch.common.SpanDecorator;
import io.opentracing.tag.Tags;
import java.net.InetSocketAddress;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionResponse;

public class TracingResponseListener<T extends ActionResponse> implements ActionListener<T> {

private final ActionListener<T> listener;
private final Span span;

public TracingResponseListener(ActionListener<T> listener, Span span) {
this.listener = listener;
this.span = span;
}

@Override
public void onResponse(T t) {
if (t.remoteAddress() != null) {
InetSocketAddress address = t.remoteAddress().address();
if (address != null) {
Tags.PEER_HOSTNAME.set(span, address.getHostName());
Tags.PEER_PORT.set(span, address.getPort());
}
}

try {
listener.onResponse(t);
} finally {
span.finish();
}
}

@Override
public void onFailure(Exception e) {
SpanDecorator.onError(e, span);

try {
listener.onFailure(e);
} finally {
span.finish();
}
}
}
Loading