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

The following examples show how to use com.google.api.services.monitoring.v3.Monitoring. 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: MetricDescriptorCacheTest.java    From kork with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddLabelAlreadyExists() throws IOException {
  String label = "newTag";
  List<String> origTags = Arrays.asList("tagA", "tagB", label);
  MetricDescriptor origDescriptor = makeDescriptor(idA, origTags, "GAUGE");
  String type = origDescriptor.getType();

  Monitoring.Projects.MetricDescriptors.Get mockGetMethod =
      Mockito.mock(Monitoring.Projects.MetricDescriptors.Get.class);
  Monitoring.Projects.MetricDescriptors.Delete mockDeleteMethod =
      Mockito.mock(Monitoring.Projects.MetricDescriptors.Delete.class);
  Monitoring.Projects.MetricDescriptors.Create mockCreateMethod =
      Mockito.mock(Monitoring.Projects.MetricDescriptors.Create.class);

  String descriptorName = "projects/test-project/metricDescriptors/" + type;
  when(descriptorsApi.get(eq(descriptorName))).thenReturn(mockGetMethod);
  when(descriptorsApi.delete(any())).thenReturn(mockDeleteMethod);
  when(descriptorsApi.create(any(), any())).thenReturn(mockCreateMethod);

  when(mockGetMethod.execute()).thenReturn(origDescriptor);
  Assert.assertEquals(origDescriptor, cache.addLabel(type, label));

  verify(mockGetMethod, times(1)).execute();
  verify(mockDeleteMethod, times(0)).execute();
  verify(mockCreateMethod, times(0)).execute();
}
 
Example #2
Source File: StackdriverWriterTest.java    From kork with Apache License 2.0 6 votes vote down vote up
@Test
public void writeRegistryWithSmallRegistry() throws IOException {
  TestableStackdriverWriter spy = spy(new TestableStackdriverWriter(writerConfig.build()));
  Monitoring.Projects.TimeSeries.Create mockCreateMethod =
      Mockito.mock(Monitoring.Projects.TimeSeries.Create.class);

  DefaultRegistry registry = new DefaultRegistry(clock);
  Counter counterA = registry.counter(idAXY);
  Counter counterB = registry.counter(idBXY);
  counterA.increment(4);
  counterB.increment(10);

  when(timeseriesApi.create(eq("projects/test-project"), any(CreateTimeSeriesRequest.class)))
      .thenReturn(mockCreateMethod);
  when(mockCreateMethod.execute()).thenReturn(null);

  spy.writeRegistry(registry);
  verify(mockCreateMethod, times(1)).execute();

  ArgumentCaptor<CreateTimeSeriesRequest> captor =
      ArgumentCaptor.forClass(CreateTimeSeriesRequest.class);
  verify(timeseriesApi, times(1)).create(eq("projects/test-project"), captor.capture());
  // A, B, timer count and totalTime.
  Assert.assertEquals(4, captor.getValue().getTimeSeries().size());
}
 
Example #3
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 #4
Source File: MetricDescriptorCacheTest.java    From kork with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddLabelWithCreateFailure() throws IOException {
  List<String> origTags = Arrays.asList("tagA", "tagB");
  MetricDescriptor origDescriptor = makeDescriptor(idA, origTags, "GAUGE");

  String type = origDescriptor.getType();
  String label = "newTag";
  List<String> updatedTags = Arrays.asList("tagA", "tagB", label);
  MetricDescriptor updatedDescriptor = makeDescriptor(idA, updatedTags, "GAUGE");

  Monitoring.Projects.MetricDescriptors.Get mockGetMethod =
      Mockito.mock(Monitoring.Projects.MetricDescriptors.Get.class);
  Monitoring.Projects.MetricDescriptors.Delete mockDeleteMethod =
      Mockito.mock(Monitoring.Projects.MetricDescriptors.Delete.class);
  Monitoring.Projects.MetricDescriptors.Create mockCreateMethod =
      Mockito.mock(Monitoring.Projects.MetricDescriptors.Create.class);

  String descriptorName = "projects/test-project/metricDescriptors/" + type;
  when(descriptorsApi.get(eq(descriptorName))).thenReturn(mockGetMethod);
  when(descriptorsApi.delete(eq(descriptorName))).thenReturn(mockDeleteMethod);
  when(descriptorsApi.create(eq("projects/test-project"), eq(updatedDescriptor)))
      .thenReturn(mockCreateMethod);

  when(mockGetMethod.execute()).thenReturn(origDescriptor);
  when(mockCreateMethod.execute()).thenThrow(new IOException("Not Found"));

  Assert.assertNull(cache.addLabel(type, label));

  verify(mockGetMethod, times(1)).execute();
  verify(mockDeleteMethod, times(1)).execute();
  verify(mockCreateMethod, times(1)).execute();
}
 
Example #5
Source File: MetricDescriptorCacheTest.java    From kork with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddLabelWithDeleteFailure() throws IOException {
  List<String> origTags = Arrays.asList("tagA", "tagB");
  MetricDescriptor origDescriptor = makeDescriptor(idA, origTags, "GAUGE");

  String type = origDescriptor.getType();
  String label = "newTag";
  List<String> updatedTags = Arrays.asList("tagA", "tagB", label);
  MetricDescriptor updatedDescriptor = makeDescriptor(idA, updatedTags, "GAUGE");

  Monitoring.Projects.MetricDescriptors.Get mockGetMethod =
      Mockito.mock(Monitoring.Projects.MetricDescriptors.Get.class);
  Monitoring.Projects.MetricDescriptors.Delete mockDeleteMethod =
      Mockito.mock(Monitoring.Projects.MetricDescriptors.Delete.class);
  Monitoring.Projects.MetricDescriptors.Create mockCreateMethod =
      Mockito.mock(Monitoring.Projects.MetricDescriptors.Create.class);

  String descriptorName = "projects/test-project/metricDescriptors/" + type;
  when(descriptorsApi.get(eq(descriptorName))).thenReturn(mockGetMethod);
  when(descriptorsApi.delete(eq(descriptorName))).thenReturn(mockDeleteMethod);
  when(descriptorsApi.create(eq("projects/test-project"), eq(updatedDescriptor)))
      .thenReturn(mockCreateMethod);

  when(mockGetMethod.execute()).thenReturn(origDescriptor);
  when(mockDeleteMethod.execute()).thenThrow(new IOException("Not Found"));
  when(mockCreateMethod.execute()).thenReturn(updatedDescriptor);

  Assert.assertEquals(updatedDescriptor, cache.addLabel(type, label));
  verify(mockGetMethod, times(1)).execute();
  verify(mockDeleteMethod, times(1)).execute();
  verify(mockCreateMethod, times(1)).execute();
}
 
Example #6
Source File: MetricDescriptorCacheTest.java    From kork with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddLabel() throws IOException {
  List<String> origTags = Arrays.asList("tagA", "tagB");
  MetricDescriptor origDescriptor = makeDescriptor(idA, origTags, "GAUGE");

  String type = origDescriptor.getType();
  String label = "newTag";
  List<String> updatedTags = Arrays.asList("tagA", "tagB", label);
  MetricDescriptor updatedDescriptor = makeDescriptor(idA, updatedTags, "GAUGE");

  Monitoring.Projects.MetricDescriptors.Get mockGetMethod =
      Mockito.mock(Monitoring.Projects.MetricDescriptors.Get.class);
  Monitoring.Projects.MetricDescriptors.Delete mockDeleteMethod =
      Mockito.mock(Monitoring.Projects.MetricDescriptors.Delete.class);
  Monitoring.Projects.MetricDescriptors.Create mockCreateMethod =
      Mockito.mock(Monitoring.Projects.MetricDescriptors.Create.class);

  String descriptorName = "projects/test-project/metricDescriptors/" + type;
  when(descriptorsApi.get(eq(descriptorName))).thenReturn(mockGetMethod);
  when(descriptorsApi.delete(eq(descriptorName))).thenReturn(mockDeleteMethod);
  when(descriptorsApi.create(eq("projects/test-project"), eq(updatedDescriptor)))
      .thenReturn(mockCreateMethod);

  when(mockGetMethod.execute()).thenReturn(origDescriptor);
  when(mockCreateMethod.execute()).thenReturn(updatedDescriptor);

  Assert.assertEquals(updatedDescriptor, cache.addLabel(type, label));
  verify(mockGetMethod, times(1)).execute();
  verify(mockDeleteMethod, times(1)).execute();
  verify(mockCreateMethod, times(1)).execute();
}
 
Example #7
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();
}
 
Example #8
Source File: StackdriverModule.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Provides
static MetricWriter provideMetricWriter(
    Monitoring monitoringClient,
    @Config("projectId") String projectId,
    ModulesService modulesService,
    @Config("stackdriverMaxQps") int maxQps,
    @Config("stackdriverMaxPointsPerRequest") int maxPointsPerRequest) {
  // The MonitoredResource for GAE apps is not writable (and missing fields anyway) so we just
  // use the gce_instance resource type instead.
  return new StackdriverWriter(
      monitoringClient,
      projectId,
      new MonitoredResource()
          .setType("gce_instance")
          .setLabels(
              ImmutableMap.of(
                  // The "zone" field MUST be a valid GCE zone, so we fake one.
                  "zone",
                  SPOOFED_GCE_ZONE,
                  // Overload the GCE "instance_id" field with the GAE module name, version and
                  // instance_id.
                  "instance_id",
                  modulesService.getCurrentModule()
                      + ":"
                      + modulesService.getCurrentVersion()
                      + ":"
                      + modulesService.getCurrentInstanceId())),
      maxQps,
      maxPointsPerRequest);
}
 
Example #9
Source File: StackdriverModule.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Provides
static Monitoring provideMonitoring(
    @JsonCredential GoogleCredentialsBundle credentialsBundle,
    @Config("projectId") String projectId) {
  return new Monitoring.Builder(
          credentialsBundle.getHttpTransport(),
          credentialsBundle.getJsonFactory(),
          credentialsBundle.getHttpRequestInitializer())
      .setApplicationName(projectId)
      .build();
}
 
Example #10
Source File: MetricsModule.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Singleton
@Provides
static MetricWriter provideMetricWriter(
    Monitoring monitoringClient, MonitoredResource monitoredResource, ProxyConfig config) {
  return new StackdriverWriter(
      monitoringClient,
      config.projectId,
      monitoredResource,
      config.metrics.stackdriverMaxQps,
      config.metrics.stackdriverMaxPointsPerRequest);
}
 
Example #11
Source File: MetricsModule.java    From nomulus with Apache License 2.0 5 votes vote down vote up
@Singleton
@Provides
static Monitoring provideMonitoring(GoogleCredentialsBundle credentialsBundle,
    ProxyConfig config) {
  return new Monitoring.Builder(
          credentialsBundle.getHttpTransport(),
          credentialsBundle.getJsonFactory(),
          credentialsBundle.getHttpRequestInitializer())
      .setApplicationName(config.projectId)
      .build();
}
 
Example #12
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 #13
Source File: StackdriverWriter.java    From java-monitoring-client-library with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a {@link StackdriverWriter}.
 *
 * <p>The monitoringClient must have read and write permissions to the Cloud Monitoring API v3 on
 * the provided project.
 */
public StackdriverWriter(
    Monitoring monitoringClient,
    String project,
    MonitoredResource monitoredResource,
    int maxQps,
    int maxPointsPerRequest) {
  this.monitoringClient = checkNotNull(monitoringClient);
  this.projectResource = "projects/" + checkNotNull(project);
  this.monitoredResource = monitoredResource;
  this.maxPointsPerRequest = maxPointsPerRequest;
  this.timeSeriesBuffer = new ArrayDeque<>(maxPointsPerRequest);
  this.rateLimiter = RateLimiter.create(maxQps);
}
 
Example #14
Source File: GoogleMonitoringIngester.java    From macrobase with Apache License 2.0 4 votes vote down vote up
@Override
public MBStream<Datum> getStream() throws Exception {
    if (!loaded) {
        QueryConf queryConf = getQueries(conf.getString(GOOGLE_MONITORING_QUERIES));
        String queryStart = conf.getString(GOOGLE_MONITORING_START_TIME);
        String queryEnd = conf.getString(GOOGLE_MONITORING_END_TIME);

        if (metrics.size() == 0) {
            throw new IllegalArgumentException("No metrics selected.");
        }

        // Record the attribute names.
        int idx = 1;
        Set<String> sortedAttributes = new TreeSet<>(attributes);
        for (String a : sortedAttributes) {
            conf.getEncoder().recordAttributeName(idx, a);
            ++idx;
        }

        // Each TimeSeries returned has a unique set of metric/resource labels and a stream
        // of values. Restructure the data to correlate by time so that we can ensure each Datum
        // contains the requested metrics and attributes. To ensure that the streams returned
        // by the API can be correlated by time, supply a per-series aligner.
        //
        // {timestamp, {attr_key, record}}
        Map<String, Map<String, Record>> byTime = new TreeMap<>();

        Monitoring client = buildClient();
        for (QueryConf.Query query : queryConf.getQueries()) {
            String pageToken = "";

            do {
                Projects.TimeSeries.List request = client.projects().timeSeries()
                    .list("projects/" + query.getProject())
                    .setFilter(query.getFilter())
                    .setIntervalStartTime(queryStart)
                    .setIntervalEndTime(queryEnd)
                    .setPageToken(pageToken)
                    .setAggregationAlignmentPeriod(query.getAlignmentPeriod())
                    .setAggregationPerSeriesAligner(query.getPerSeriesAligner())
                    .setAggregationCrossSeriesReducer(query.getCrossSeriesReducer())
                    .setAggregationGroupByFields(query.getGroupByFields());
                log.trace("Request: {}", request.toString());

                ListTimeSeriesResponse response = request.execute();
                log.trace("Response: {}", response.toPrettyString());

                processResponse(response, metrics, byTime);
                pageToken = response.getNextPageToken();
            } while (pageToken != null && !pageToken.isEmpty());
        }

        dataStream = convertToStream(byTime);

        log.info("Loaded {} points. Skipped {} TimeSeries and {} partial records.",
                 pointsAdded, skippedTimeSeries, skippedPoints);
        loaded = true;
    }

    return dataStream;
}
 
Example #15
Source File: ConfigParams.java    From kork with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure the configuration parameters are complete and valid.
 *
 * <p>If some required parameters are not yet explicitly set then this may initialize them.
 */
public ConfigParams build() {
  ConfigParams result = new ConfigParams();

  String actualProjectName = determineProjectName.apply(projectName);
  result.projectName = validateString(actualProjectName, "stackdriver projectName");
  result.applicationName = validateString(applicationName, "applicationName");
  result.customTypeNamespace =
      validateString(customTypeNamespace, "stackdriver customTypeNamespace");

  result.counterStartTime = counterStartTime;
  result.measurementFilter = measurementFilter;

  result.instanceId = instanceId;
  result.monitoring = monitoring;
  result.descriptorCache = descriptorCache;

  if (result.instanceId == null || result.instanceId.isEmpty()) {
    UUID uuid = UUID.randomUUID();
    byte[] uuidBytes = new byte[16];
    addLong(uuidBytes, 0, uuid.getLeastSignificantBits());
    addLong(uuidBytes, 8, uuid.getMostSignificantBits());
    result.instanceId = java.util.Base64.getEncoder().encodeToString(uuidBytes);
  }

  if (result.monitoring == null) {
    try {
      HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
      JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
      GoogleCredential credential = loadCredential(transport, jsonFactory, credentialsPath);
      String version = getClass().getPackage().getImplementationVersion();
      if (version == null) {
        version = "Unknown";
      }

      result.monitoring =
          new Monitoring.Builder(transport, jsonFactory, credential)
              .setApplicationName("Spinnaker/" + version)
              .build();
    } catch (IOException | java.security.GeneralSecurityException e) {
      final Logger log = LoggerFactory.getLogger("StackdriverWriter");
      log.error("Caught exception initializing client: " + e);
      throw new IllegalStateException(e);
    }
  }

  if (result.descriptorCache == null) {
    result.descriptorCache = new MetricDescriptorCache(result);
  }
  return result;
}
 
Example #16
Source File: ConfigParams.java    From kork with Apache License 2.0 4 votes vote down vote up
/**
 * Overrides the normal Stackdriver Monitoring client to use when interacting with Stackdriver.
 */
public Builder setStackdriverStub(Monitoring stub) {
  monitoring = stub;
  return this;
}
 
Example #17
Source File: ConfigParams.java    From kork with Apache License 2.0 4 votes vote down vote up
/** The Stackdriver Monitoring client stub. */
public Monitoring getStackdriverStub() {
  return monitoring;
}
 
Example #18
Source File: MetricDescriptorCacheTest.java    From kork with Apache License 2.0 4 votes vote down vote up
public ReturnExecuteDescriptorArg(
    Monitoring.Projects.MetricDescriptors.Create mockCreateMethod) {
  this.mockCreateMethod = mockCreateMethod;
}