/**
 * Copyright 2017-2018 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.spring.cloud.hystrix;

import com.netflix.hystrix.HystrixCommand;
import io.opentracing.Scope;
import io.opentracing.Tracer;
import io.opentracing.tag.Tags;

/**
 * Implement this class if you need traced variant of {@link HystrixCommand}. It creates a span that
 * wraps the whole invocation.
 */
public abstract class TracedHystrixCommand<R> extends HystrixCommand<R> {

  static final String TAG_HYSTRIX_COMPONENT = "hystrix";
  static final String TAG_COMMAND_KEY = "commandKey";
  static final String TAG_COMMAND_GROUP = "commandGroup";
  static final String TAG_THREAD_POOL_KEY = "threadPoolKey";

  private final Tracer tracer;

  protected TracedHystrixCommand(Tracer tracer, Setter setter) {
    super(setter);
    this.tracer = tracer;
  }

  @Override
  protected R run() throws Exception {
    String commandKeyName = getCommandKey().name();

    try (Scope scope = this.tracer.buildSpan(commandKeyName)
        .withTag(Tags.COMPONENT.getKey(), TAG_HYSTRIX_COMPONENT)
        .withTag(TAG_COMMAND_KEY, commandKeyName)
        .withTag(TAG_COMMAND_GROUP, commandGroup.name())
        .withTag(TAG_THREAD_POOL_KEY, threadPoolKey.name())
        .startActive(true)) {
      return doRun();
    }
  }

  public abstract R doRun() throws Exception;
}

@@ -22,9 +22,9 @@
 -201
-8
+9
  The
@@ -722,16 +722,44 @@
 .Scope;%0A
+import io.opentracing.Span;%0A
 import i
@@ -813,16 +813,65 @@
 .Tags;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A * I
@@ -1562,18 +1562,12 @@
 ();%0A
-%0A
     
-try (
 Scop
@@ -1872,17 +1872,25 @@
 ve(true)
-)
+;%0A    try
  %7B%0A     
@@ -1911,16 +1911,472 @@
 );%0A    %7D
+ catch (Exception e) %7B%0A      onError(e,scope.span());%0A      throw e;%0A    %7D finally %7B%0A      scope.close();%0A    %7D%0A  %7D%0A%0A  private void onError(Exception e, Span span) %7B%0A    Tags.ERROR.set(span, Boolean.TRUE);%0A    span.log(errorLogs(e));%0A  %7D%0A%0A  private Map%3CString, Object%3E errorLogs(Exception e) %7B%0A    Map%3CString, Object%3E errorLogs = new HashMap%3C%3E(3);%0A    errorLogs.put(%22event%22, Tags.ERROR.getKey());%0A    errorLogs.put(%22error.object%22, e);%0A    return errorLogs;
 %0A  %7D%0A%0A  

Annotate span with error when hystrix command fails (#199)