|
| 1 | +package datadog.trace.instrumentation.elasticsearch5_3; |
| 2 | + |
| 3 | +import static io.opentracing.log.Fields.ERROR_OBJECT; |
| 4 | + |
| 5 | +import com.google.common.base.Joiner; |
| 6 | +import io.opentracing.Span; |
| 7 | +import io.opentracing.tag.Tags; |
| 8 | +import java.util.Collections; |
| 9 | +import org.elasticsearch.action.ActionListener; |
| 10 | +import org.elasticsearch.action.ActionRequest; |
| 11 | +import org.elasticsearch.action.ActionResponse; |
| 12 | +import org.elasticsearch.action.DocWriteRequest; |
| 13 | +import org.elasticsearch.action.IndicesRequest; |
| 14 | +import org.elasticsearch.action.bulk.BulkShardResponse; |
| 15 | +import org.elasticsearch.action.get.GetResponse; |
| 16 | +import org.elasticsearch.action.index.IndexResponse; |
| 17 | +import org.elasticsearch.action.search.SearchRequest; |
| 18 | +import org.elasticsearch.action.support.broadcast.BroadcastResponse; |
| 19 | +import org.elasticsearch.action.support.nodes.BaseNodesResponse; |
| 20 | +import org.elasticsearch.action.support.replication.ReplicationResponse; |
| 21 | + |
| 22 | +public class TransportActionListener<T extends ActionResponse> implements ActionListener<T> { |
| 23 | + |
| 24 | + private final ActionListener<T> listener; |
| 25 | + private final Span span; |
| 26 | + |
| 27 | + public TransportActionListener( |
| 28 | + final ActionRequest actionRequest, final ActionListener<T> listener, final Span span) { |
| 29 | + this.listener = listener; |
| 30 | + this.span = span; |
| 31 | + onRequest(actionRequest); |
| 32 | + } |
| 33 | + |
| 34 | + private void onRequest(final ActionRequest request) { |
| 35 | + if (request != null) { |
| 36 | + span.setTag("elasticsearch.request.description", request.getDescription()); |
| 37 | + } |
| 38 | + if (request instanceof IndicesRequest) { |
| 39 | + final IndicesRequest req = (IndicesRequest) request; |
| 40 | + if (req.indices() != null) { |
| 41 | + span.setTag("elasticsearch.request.indices", Joiner.on(",").join(req.indices())); |
| 42 | + } |
| 43 | + } |
| 44 | + if (request instanceof SearchRequest) { |
| 45 | + final SearchRequest req = (SearchRequest) request; |
| 46 | + span.setTag("elasticsearch.request.search.types", Joiner.on(",").join(req.types())); |
| 47 | + } |
| 48 | + if (request instanceof DocWriteRequest) { |
| 49 | + final DocWriteRequest req = (DocWriteRequest) request; |
| 50 | + span.setTag("elasticsearch.request.write.type", req.type()); |
| 51 | + span.setTag("elasticsearch.request.write.routing", req.routing()); |
| 52 | + span.setTag("elasticsearch.request.write.version", req.version()); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void onResponse(final T response) { |
| 58 | + if (response.remoteAddress() != null) { |
| 59 | + Tags.PEER_HOSTNAME.set(span, response.remoteAddress().getHost()); |
| 60 | + Tags.PEER_HOST_IPV4.set(span, response.remoteAddress().getAddress()); |
| 61 | + Tags.PEER_PORT.set(span, response.remoteAddress().getPort()); |
| 62 | + } |
| 63 | + |
| 64 | + if (response instanceof GetResponse) { |
| 65 | + final GetResponse resp = (GetResponse) response; |
| 66 | + span.setTag("elasticsearch.type", resp.getType()); |
| 67 | + span.setTag("elasticsearch.id", resp.getId()); |
| 68 | + span.setTag("elasticsearch.version", resp.getVersion()); |
| 69 | + } |
| 70 | + |
| 71 | + if (response instanceof BroadcastResponse) { |
| 72 | + final BroadcastResponse resp = (BroadcastResponse) response; |
| 73 | + span.setTag("elasticsearch.shard.broadcast.total", resp.getTotalShards()); |
| 74 | + span.setTag("elasticsearch.shard.broadcast.successful", resp.getSuccessfulShards()); |
| 75 | + span.setTag("elasticsearch.shard.broadcast.failed", resp.getFailedShards()); |
| 76 | + } |
| 77 | + |
| 78 | + if (response instanceof ReplicationResponse) { |
| 79 | + final ReplicationResponse resp = (ReplicationResponse) response; |
| 80 | + span.setTag("elasticsearch.shard.replication.total", resp.getShardInfo().getTotal()); |
| 81 | + span.setTag( |
| 82 | + "elasticsearch.shard.replication.successful", resp.getShardInfo().getSuccessful()); |
| 83 | + span.setTag("elasticsearch.shard.replication.failed", resp.getShardInfo().getFailed()); |
| 84 | + } |
| 85 | + |
| 86 | + if (response instanceof IndexResponse) { |
| 87 | + span.setTag("elasticsearch.response.status", ((IndexResponse) response).status().getStatus()); |
| 88 | + } |
| 89 | + |
| 90 | + if (response instanceof BulkShardResponse) { |
| 91 | + final BulkShardResponse resp = (BulkShardResponse) response; |
| 92 | + span.setTag("elasticsearch.shard.bulk.id", resp.getShardId().getId()); |
| 93 | + span.setTag("elasticsearch.shard.bulk.index", resp.getShardId().getIndexName()); |
| 94 | + } |
| 95 | + |
| 96 | + if (response instanceof BaseNodesResponse) { |
| 97 | + final BaseNodesResponse resp = (BaseNodesResponse) response; |
| 98 | + if (resp.hasFailures()) { |
| 99 | + span.setTag("elasticsearch.node.failures", resp.failures().size()); |
| 100 | + } |
| 101 | + span.setTag("elasticsearch.node.cluster.name", resp.getClusterName().value()); |
| 102 | + } |
| 103 | + |
| 104 | + try { |
| 105 | + listener.onResponse(response); |
| 106 | + } finally { |
| 107 | + span.finish(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void onFailure(final Exception e) { |
| 113 | + Tags.ERROR.set(span, true); |
| 114 | + span.log(Collections.singletonMap(ERROR_OBJECT, e)); |
| 115 | + |
| 116 | + try { |
| 117 | + listener.onFailure(e); |
| 118 | + } finally { |
| 119 | + span.finish(); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments