com.google.api.services.monitoring.v3.model.MonitoredResource Java Examples
The following examples show how to use
com.google.api.services.monitoring.v3.model.MonitoredResource.
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: MetricsModule.java From nomulus with Apache License 2.0 | 6 votes |
/** * Provides a {@link MonitoredResource} appropriate for environment tha proxy runs in. * * <p>When running locally, the type of the monitored resource is set to {@code global}, otherwise * it is {@code gke_container}. * * @see <a * href="https://cloud.google.com/monitoring/custom-metrics/creating-metrics#which-resource"> * Choosing a monitored resource type</a> */ @Singleton @Provides static MonitoredResource provideMonitoredResource( Environment env, ProxyConfig config, MetricParameters metricParameters) { MonitoredResource monitoredResource = new MonitoredResource(); if (env == Environment.LOCAL) { monitoredResource .setType("global") .setLabels(ImmutableMap.of("project_id", config.projectId)); } else { monitoredResource.setType("gke_container").setLabels(metricParameters.makeLabelsMap()); } logger.atInfo().log("Monitored resource: %s", monitoredResource); return monitoredResource; }
Example #2
Source File: StackdriverWriter.java From kork with Apache License 2.0 | 6 votes |
/** * Get the monitoredResource for the TimeSeries data. * * <p>This will return null if the resource cannot be determined. */ MonitoredResource determineMonitoredResource() { if (monitoredResource == null) { String project = projectResourceName.substring(projectResourceName.indexOf('/') + 1); try { monitoredResource = new MonitoredResourceBuilder().setStackdriverProject(project).build(); if (monitoredResource.getType().equals("global")) { cache.addExtraTimeSeriesLabel(MetricDescriptorCache.INSTANCE_LABEL, instanceId); } log.info( "Using monitoredResource={} with extraTimeSeriesLabels={}", monitoredResource, cache.getExtraTimeSeriesLabels()); } catch (IOException ioex) { log.error("Unable to determine monitoredResource at this time.", ioex); } } return monitoredResource; }
Example #3
Source File: MonitoredResourceBuilder.java From kork with Apache License 2.0 | 6 votes |
/** * Create a MonitoredResource that describes this deployment. * * <p>This will throw an IOException if the resource could not be created. In practice this * exception is not currently thrown. * * <p>However the expectation is that custom types will be added in the future and there may be * transient IO errors interacting with Stackdriver to create the type. */ public MonitoredResource build() throws IOException { HashMap<String, String> labels = new HashMap<String, String>(); MonitoredResource resource = new MonitoredResource(); if (maybeCollectGceInstanceLabels(labels)) { resource.setType("gce_instance"); } else if (maybeCollectGkeInstanceLabels(labels)) { resource.setType("gke_container"); } else if (maybeCollectEc2InstanceLabels(labels)) { resource.setType("aws_ec2_instance"); } else if (stackdriverProjectId.isEmpty()) { throw new IllegalStateException("stackdriverProjectId was not set."); } else { labels.put("project_id", stackdriverProjectId); resource.setType("global"); } resource.setLabels(labels); return resource; }
Example #4
Source File: MonitoredResourceBuilderTest.java From kork with Apache License 2.0 | 6 votes |
@Test public void testGceInstance() throws IOException { builder.setStackdriverProject("UNUSED"); String instance = "MY INSTANCE"; String zone = "us-central1-f"; String zone_path = "path/to/" + zone; String project = "MY PROJECT"; doReturn(instance).when(builder).getGoogleMetadataValue("instance/id"); doReturn(zone_path).when(builder).getGoogleMetadataValue("instance/zone"); doReturn(project).when(builder).getGoogleMetadataValue("project/project-id"); Map<String, String> labels = new HashMap<String, String>(); labels.put("instance_id", instance); labels.put("zone", zone); MonitoredResource resource = builder.build(); Assert.assertEquals("gce_instance", resource.getType()); Assert.assertEquals(labels, resource.getLabels()); }
Example #5
Source File: StackdriverWriter.java From java-monitoring-client-library with Apache License 2.0 | 5 votes |
/** * 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 #6
Source File: MetricsModule.java From nomulus with Apache License 2.0 | 5 votes |
@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 #7
Source File: StackdriverModule.java From nomulus with Apache License 2.0 | 5 votes |
@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 #8
Source File: StackdriverWriter.java From kork with Apache License 2.0 | 5 votes |
/** Implementation of writeRegistry wrapped for timing. */ private void writeRegistryHelper(Registry registry) { MonitoredResource resource = determineMonitoredResource(); if (resource == null) { log.warn("Cannot determine the managed resource - not flushing metrics."); return; } List<TimeSeries> tsList = registryToTimeSeries(registry); if (tsList.isEmpty()) { log.debug("No metric data points."); return; } CreateTimeSeriesRequest tsRequest = new CreateTimeSeriesRequest(); int offset = 0; int failed = 0; List<TimeSeries> nextN; log.debug("Writing metrics..."); while (offset < tsList.size()) { if (offset + MAX_TS_PER_REQUEST < tsList.size()) { nextN = tsList.subList(offset, offset + MAX_TS_PER_REQUEST); offset += MAX_TS_PER_REQUEST; } else { nextN = tsList.subList(offset, tsList.size()); offset = tsList.size(); } tsRequest.setTimeSeries(nextN); try { service.projects().timeSeries().create(projectResourceName, tsRequest).execute(); } catch (HttpResponseException rex) { handleTimeSeriesResponseException(rex, "creating time series", nextN); failed += nextN.size(); } catch (IOException ioex) { log.error("Caught Exception creating time series " + ioex); failed += nextN.size(); } } log.debug("Wrote {} values", tsList.size() - failed); }
Example #9
Source File: MonitoredResourceBuilderTest.java From kork with Apache License 2.0 | 5 votes |
@Test public void testGlobal() throws IOException { String project = "StackdriverProject"; builder.setStackdriverProject(project); doThrow(new IOException()).when(builder).getGoogleMetadataValue(any(String.class)); doThrow(new IOException()).when(builder).getAwsIdentityDocument(); Map<String, String> labels = new HashMap<String, String>(); labels.put("project_id", project); MonitoredResource resource = builder.build(); Assert.assertEquals("global", resource.getType()); Assert.assertEquals(labels, resource.getLabels()); }
Example #10
Source File: SheepCounterExample.java From java-monitoring-client-library with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws Exception { if (args.length < 1) { System.err.println("Missing required project argument"); System.err.printf( "Usage: java %s gcp-project-id [verbose]\n", SheepCounterExample.class.getName()); return; } String project = args[0]; // Turn up the logging verbosity if (args.length > 1) { Logger log = LogManager.getLogManager().getLogger(""); log.setLevel(Level.ALL); for (Handler h : log.getHandlers()) { h.setLevel(Level.ALL); } } // Create a sample resource. In this case, a GCE Instance. // See https://cloud.google.com/monitoring/api/resources for a list of resource types. MonitoredResource monitoredResource = new MonitoredResource() .setType("gce_instance") .setLabels( ImmutableMap.of( "instance_id", "test-instance", "zone", "us-central1-f")); // Set up the Metrics infrastructure. MetricWriter stackdriverWriter = new StackdriverWriter( createAuthorizedMonitoringClient(), project, monitoredResource, STACKDRIVER_MAX_QPS, STACKDRIVER_MAX_POINTS_PER_REQUEST); final MetricReporter reporter = new MetricReporter( stackdriverWriter, METRICS_REPORTING_INTERVAL, Executors.defaultThreadFactory()); reporter.startAsync().awaitRunning(); // Set up a handler to stop sleeping on SIGINT. Runtime.getRuntime() .addShutdownHook( new Thread( () -> { reporter.stopAsync().awaitTerminated(); // Allow the LogManager to cleanup the loggers. DelayedShutdownLogManager.resetFinally(); })); System.err.println("Send SIGINT (Ctrl+C) to stop sleeping."); while (true) { // Count some Googley sheep. int colorIndex = new Random().nextInt(SHEEP_COLORS.size()); int speciesIndex = new Random().nextInt(SHEEP_SPECIES.size()); sheepCounter.incrementBy(1, SHEEP_COLORS.get(colorIndex), SHEEP_SPECIES.get(speciesIndex)); sheepFluffiness.record( new Random().nextDouble() * 200, SHEEP_COLORS.get(colorIndex), SHEEP_SPECIES.get(speciesIndex)); isSleeping.set(true); logger.info("zzz..."); Thread.sleep(5000); } }
Example #11
Source File: MonitoredResourceBuilderTest.java From kork with Apache License 2.0 | 4 votes |
@Test public void testEc2Instance() throws IOException { String region = "us-east-1"; String instanceId = "i-abcdef"; String accountId = "12345"; String project = "StackdriverProject"; builder.setStackdriverProject(project); String awsIdentityDoc = "{\n" + "\"privateIp\" : \"123.45.67.89\",\n" + "\"devpayProductCodes\" : null,\n" + "\"availabilityZone\" : \"us-east-1d\",\n" + "\"accountId\" : \"" + accountId + "\",\n" + "\"version\" : \"2010-08-31\",\n" + "\"instanceId\" : \"" + instanceId + "\",\n" + "\"billingProducts\" : null,\n" + "\"region\" : \"" + region + "\"\n" + "}"; doThrow(new IOException()).when(builder).getGoogleMetadataValue(any(String.class)); doReturn(awsIdentityDoc).when(builder).getAwsIdentityDocument(); Map<String, String> labels = new HashMap<String, String>(); labels.put("instance_id", instanceId); labels.put("aws_account", accountId); labels.put("region", region); labels.put("project_id", project); MonitoredResource resource = builder.build(); Assert.assertEquals("aws_ec2_instance", resource.getType()); Assert.assertEquals(labels, resource.getLabels()); }