All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.cloud.dataflow.sdk.options.GoogleApiDebugOptions Maven / Gradle / Ivy

/*
 * Copyright (C) 2015 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.cloud.dataflow.sdk.options;

import com.google.api.client.googleapis.services.AbstractGoogleClient;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * These options configure debug settings for Google API clients created within the Dataflow SDK.
 */
public interface GoogleApiDebugOptions extends PipelineOptions {
  /**
   * This option enables tracing of API calls to Google services used within the Dataflow SDK.
   */
  @Description("This option enables tracing of API calls to Google services used within the "
      + "Dataflow SDK. Values are expected in JSON format {\"ApiName\":\"TraceDestination\",...} "
      + "where the ApiName represents the request classes canonical name. The TraceDestination is "
      + "a logical trace consumer to whom the trace will be reported. Typically, \"producer\" is "
      + "the right destination to use: this makes API traces available to the team offering the "
      + "API. Note that by enabling this option, the contents of the requests to and from "
      + "Google Cloud services will be made available to Google. For example, by specifying "
      + "{\"Dataflow\":\"producer\"}, all calls to the Dataflow service will be made available to "
      + "Google, specifically to the Google Cloud Dataflow team.")
  GoogleApiTracer getGoogleApiTrace();
  void setGoogleApiTrace(GoogleApiTracer commands);

  /**
   * A {@link GoogleClientRequestInitializer} that adds the trace destination to Google API calls.
   */
  public static class GoogleApiTracer extends HashMap
      implements GoogleClientRequestInitializer {
    private static final long serialVersionUID = 0;

    /**
     * Creates a {@link GoogleApiTracer} that sets the trace destination on all
     * calls that match the given client type.
     */
    public GoogleApiTracer addTraceFor(AbstractGoogleClient client, String traceDestination) {
      put(client.getClass().getCanonicalName(), traceDestination);
      return this;
    }

    /**
     * Creates a {@link GoogleApiTracer} that sets the trace {@code traceDestination} on all
     * calls that match for the given request type.
     */
    public GoogleApiTracer addTraceFor(
        AbstractGoogleClientRequest request, String traceDestination) {
      put(request.getClass().getCanonicalName(), traceDestination);
      return this;
    }

    @Override
    public void initialize(AbstractGoogleClientRequest request) throws IOException {
      for (Map.Entry entry : this.entrySet()) {
        if (request.getClass().getCanonicalName().contains(entry.getKey())) {
          request.set("$trace", entry.getValue());
        }
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy