com.google.api.services.monitoring.v3.MonitoringScopes Java Examples

The following examples show how to use com.google.api.services.monitoring.v3.MonitoringScopes. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigParams.java    From kork with Apache License 2.0 6 votes vote down vote up
/** Helper function for the validator that reads our credentials for talking to Stackdriver. */
private static GoogleCredential loadCredential(
    HttpTransport transport, JsonFactory factory, String credentialsPath) throws IOException {
  final Logger log = LoggerFactory.getLogger("StackdriverWriter");

  GoogleCredential credential;
  if (credentialsPath != null && !credentialsPath.isEmpty()) {
    FileInputStream stream = new FileInputStream(credentialsPath);
    try {
      credential =
          GoogleCredential.fromStream(stream, transport, factory)
              .createScoped(Collections.singleton(MonitoringScopes.MONITORING));
      log.info("Loaded credentials from from {}", credentialsPath);
    } finally {
      stream.close();
    }
  } else {
    log.info(
        "spectator.stackdriver.monitoring.enabled without"
            + " spectator.stackdriver.credentialsPath. "
            + " Using default application credentials.");
    credential = GoogleCredential.getApplicationDefault();
  }
  return credential;
}
 
Example #2
Source File: SheepCounterExample.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
private static Monitoring createAuthorizedMonitoringClient() throws IOException {
  // Grab the Application Default Credentials from the environment.
  // Generate these with 'gcloud beta auth application-default login'
  GoogleCredential credential =
      GoogleCredential.getApplicationDefault().createScoped(MonitoringScopes.all());

  // Create and return the CloudMonitoring service object
  HttpTransport httpTransport = new NetHttpTransport();
  JsonFactory jsonFactory = new JacksonFactory();
  return new Monitoring.Builder(httpTransport, jsonFactory, credential)
      .setApplicationName("Monitoring Sample")
      .build();
}
 
Example #3
Source File: GoogleClientFactory.java    From kayenta with Apache License 2.0 5 votes vote down vote up
public Monitoring getMonitoring() throws IOException {
  HttpTransport httpTransport = buildHttpTransport();
  JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
  GoogleCredentials credentials = getCredentials(MonitoringScopes.all());
  HttpRequestInitializer reqInit = setHttpTimeout(credentials);
  String applicationName = "Spinnaker/" + applicationVersion;

  return new Monitoring.Builder(httpTransport, jsonFactory, reqInit)
      .setApplicationName(applicationName)
      .build();
}
 
Example #4
Source File: GoogleMonitoringIngester.java    From macrobase with Apache License 2.0 5 votes vote down vote up
/**
 * Establishes an authenticated client using Application Default Credentials.
 *
 * @see "https://cloud.google.com/monitoring/demos/run_samples#before_you_begin"
 */
private Monitoring buildClient() throws GeneralSecurityException, IOException {
    // Grab the Application Default Credentials from the environment.
    GoogleCredential credential = GoogleCredential.getApplicationDefault()
        .createScoped(MonitoringScopes.all());

    // Create and return the CloudMonitoring service object
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    return new Monitoring.Builder(httpTransport, jsonFactory, credential)
        .setApplicationName("MacroBase Ingester")
        .build();
}