/*
 * Copyright 2017, Google Inc.
 * 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 com.google.instrumentation.trace;

import java.util.List;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;

/**
 * The main exporting API for the trace library.
 *
 * <p>Implementation MUST ensure that all functions are thread safe.
 */
@ThreadSafe
public abstract class TraceExporter {
  private static final NoopTraceExporter noopTraceExporter = new NoopTraceExporter();

  /**
   * Registers a new service handler that is used by the library to export trace data.
   *
   * <p>Example of usage:
   *
   * <pre>{@code
   * public static void main(String[] args) {
   *   Tracing.getTraceExporter().registerServiceHandler(
   *       "com.google.stackdriver", new StackdriverServiceHandler());
   *   // ...
   * }
   * }</pre>
   *
   * @param name the name of the service handler. Must be unique for each service.
   * @param serviceHandler the service handler that is called for each ended sampled span.
   */
  public abstract void registerServiceHandler(String name, ServiceHandler serviceHandler);

  /**
   * Unregisters the service handler with the provided name.
   *
   * @param name the name of the service handler that will be unregistered.
   */
  public abstract void unregisterServiceHandler(String name);

  /**
   * An abstract class that allows different tracing services to export recorded data for sampled
   * spans in their own format.
   *
   * <p>To export data this MUST be register to to the TraceExporter using {@link
   * #registerServiceHandler(String, ServiceHandler)}.
   */
  public abstract static class ServiceHandler {

    /**
     * Exports a list of sampled (see {@link TraceOptions#isSampled()}) {@link Span}s using the
     * immutable representation {@link SpanData}.
     *
     * <p>This may be called from a different thread than the one that called {@link Span#end()}.
     *
     * <p>Implementation SHOULD not block the calling thread. It should execute the export on a
     * different thread if possible.
     *
     * @param spanDataList a list of {@code SpanData} objects to be exported.
     */
    public abstract void export(List<SpanData> spanDataList);
  }

  /**
   * Returns the no-op implementation of the {@code TraceExporter}.
   *
   * @return the no-op implementation of the {@code TraceExporter}.
   */
  static TraceExporter getNoopTraceExporter() {
    return noopTraceExporter;
  }

  private static final class NoopTraceExporter extends TraceExporter {
    @Override
    public void registerServiceHandler(String name, @Nullable ServiceHandler serviceHandler) {}

    @Override
    public void unregisterServiceHandler(String name) {}
  }
}

@@ -647,16 +647,81 @@
 l.List;%0A
+import java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A
 import j
@@ -2801,16 +2801,1164 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Implementation of the %7B@link ServiceHandler%7D which logs all the exported %7B@link SpanData%7D.%0A   *%0A   * %3Cp%3EExample of usage:%0A   *%0A   * %3Cpre%3E%7B@code%0A   * public static void main(String%5B%5D args) %7B%0A   *   Tracing.getTraceExporter().registerServiceHandler(%0A   *       %22com.google.instrumentation.LoggingServiceHandler%22, LoggingServiceHandler.getInstance());%0A   *   // ...%0A   * %7D%0A   * %7D%3C/pre%3E%0A   *%0A   */%0A  @ThreadSafe%0A  public static final class LoggingServiceHandler extends ServiceHandler %7B%0A    private static final Logger logger = Logger.getLogger(LoggingServiceHandler.class.getName());%0A    private static final LoggingServiceHandler INSTANCE = new LoggingServiceHandler();%0A%0A    /**%0A     * Returns the instance of the %7B@code LoggingServiceHandler%7D.%0A     *%0A     * @return the instance of the %7B@code LoggingServiceHandler%7D.%0A     */%0A    public static LoggingServiceHandler getInstance() %7B%0A      return INSTANCE;%0A    %7D%0A%0A    @Override%0A    public void export(List%3CSpanData%3E spanDataList) %7B%0A      for (SpanData spanData : spanDataList) %7B%0A        logger.log(Level.INFO, spanData.toString());%0A      %7D%0A    %7D%0A%0A    private LoggingServiceHandler() %7B%7D%0A  %7D%0A%0A
   /**%0A  

Add an export service handler which logs all the SpanData. (#286)