Search in sources :

Example 36 with ProjectName

use of com.google.devtools.clouderrorreporting.v1beta1.ProjectName in project java-docs-samples by GoogleCloudPlatform.

the class Snippets method listTimeSeries.

/**
 * Demonstrates listing time series using a filter.
 */
void listTimeSeries(String filter) throws IOException {
    // [START monitoring_read_timeseries_simple]
    MetricServiceClient metricServiceClient = MetricServiceClient.create();
    String projectId = System.getProperty("projectId");
    ProjectName name = ProjectName.of(projectId);
    // Restrict time to last 20 minutes
    long startMillis = System.currentTimeMillis() - ((60 * 20) * 1000);
    TimeInterval interval = TimeInterval.newBuilder().setStartTime(Timestamps.fromMillis(startMillis)).setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build();
    ListTimeSeriesRequest.Builder requestBuilder = ListTimeSeriesRequest.newBuilder().setName(name.toString()).setFilter(filter).setInterval(interval);
    ListTimeSeriesRequest request = requestBuilder.build();
    ListTimeSeriesPagedResponse response = metricServiceClient.listTimeSeries(request);
    System.out.println("Got timeseries: ");
    for (TimeSeries ts : response.iterateAll()) {
        System.out.println(ts);
    }
// [END monitoring_read_timeseries_simple]
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) TimeInterval(com.google.monitoring.v3.TimeInterval) ProjectName(com.google.monitoring.v3.ProjectName) ListTimeSeriesPagedResponse(com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse) ListTimeSeriesRequest(com.google.monitoring.v3.ListTimeSeriesRequest)

Example 37 with ProjectName

use of com.google.devtools.clouderrorreporting.v1beta1.ProjectName in project java-docs-samples by GoogleCloudPlatform.

the class ErrorReportingExample method logCustomErrorEvent.

private void logCustomErrorEvent() {
    try (ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.create()) {
        // Custom error events require an error reporting location as well.
        ErrorContext errorContext = ErrorContext.newBuilder().setReportLocation(SourceLocation.newBuilder().setFilePath("Test.java").setLineNumber(10).setFunctionName("myMethod").build()).build();
        // Report a custom error event
        ReportedErrorEvent customErrorEvent = ReportedErrorEvent.getDefaultInstance().toBuilder().setMessage("custom error event").setContext(errorContext).build();
        // default project id
        ProjectName projectName = ProjectName.of(ServiceOptions.getDefaultProjectId());
        reportErrorsServiceClient.reportErrorEvent(projectName, customErrorEvent);
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Exception encountered logging custom event", e);
    }
}
Also used : ProjectName(com.google.devtools.clouderrorreporting.v1beta1.ProjectName) ErrorContext(com.google.devtools.clouderrorreporting.v1beta1.ErrorContext) ReportedErrorEvent(com.google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent) ReportErrorsServiceClient(com.google.cloud.errorreporting.v1beta1.ReportErrorsServiceClient) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 38 with ProjectName

use of com.google.devtools.clouderrorreporting.v1beta1.ProjectName in project java-docs-samples by GoogleCloudPlatform.

the class QuickstartSample method main.

public static void main(String... args) throws Exception {
    // Your Google Cloud Platform project ID
    String projectId = System.getProperty("projectId");
    if (projectId == null) {
        System.err.println("Usage: QuickstartSample -DprojectId=YOUR_PROJECT_ID");
        return;
    }
    // Instantiates a client
    MetricServiceClient metricServiceClient = MetricServiceClient.create();
    // Prepares an individual data point
    TimeInterval interval = TimeInterval.newBuilder().setEndTime(Timestamps.fromMillis(System.currentTimeMillis())).build();
    TypedValue value = TypedValue.newBuilder().setDoubleValue(123.45).build();
    Point point = Point.newBuilder().setInterval(interval).setValue(value).build();
    List<Point> pointList = new ArrayList<>();
    pointList.add(point);
    ProjectName name = ProjectName.of(projectId);
    // Prepares the metric descriptor
    Map<String, String> metricLabels = new HashMap<String, String>();
    metricLabels.put("store_id", "Pittsburg");
    Metric metric = Metric.newBuilder().setType("custom.googleapis.com/stores/daily_sales").putAllLabels(metricLabels).build();
    // Prepares the monitored resource descriptor
    Map<String, String> resourceLabels = new HashMap<String, String>();
    resourceLabels.put("project_id", projectId);
    MonitoredResource resource = MonitoredResource.newBuilder().setType("global").putAllLabels(resourceLabels).build();
    // Prepares the time series request
    TimeSeries timeSeries = TimeSeries.newBuilder().setMetric(metric).setResource(resource).addAllPoints(pointList).build();
    List<TimeSeries> timeSeriesList = new ArrayList<>();
    timeSeriesList.add(timeSeries);
    CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder().setName(name.toString()).addAllTimeSeries(timeSeriesList).build();
    // Writes time series data
    metricServiceClient.createTimeSeries(request);
    System.out.printf("Done writing time series data.%n");
    metricServiceClient.close();
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) MetricServiceClient(com.google.cloud.monitoring.v3.MetricServiceClient) TimeInterval(com.google.monitoring.v3.TimeInterval) ProjectName(com.google.monitoring.v3.ProjectName) HashMap(java.util.HashMap) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) ArrayList(java.util.ArrayList) MonitoredResource(com.google.api.MonitoredResource) Point(com.google.monitoring.v3.Point) Metric(com.google.api.Metric) TypedValue(com.google.monitoring.v3.TypedValue)

Example 39 with ProjectName

use of com.google.devtools.clouderrorreporting.v1beta1.ProjectName in project java-docs-samples by GoogleCloudPlatform.

the class QuickStart method main.

public static void main(String[] args) throws Exception {
    // Google Cloud Platform Project ID
    String projectId = (args.length > 0) ? args[0] : ServiceOptions.getDefaultProjectId();
    ProjectName projectName = ProjectName.of(projectId);
    // Instantiate an Error Reporting Client
    try (ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.create()) {
        // Custom error events require an error reporting location as well.
        ErrorContext errorContext = ErrorContext.newBuilder().setReportLocation(SourceLocation.newBuilder().setFilePath("Test.java").setLineNumber(10).setFunctionName("myMethod").build()).build();
        // Report a custom error event
        ReportedErrorEvent customErrorEvent = ReportedErrorEvent.getDefaultInstance().toBuilder().setMessage("custom error event").setContext(errorContext).build();
        // Report an event synchronously, use .reportErrorEventCallable for asynchronous reporting.
        reportErrorsServiceClient.reportErrorEvent(projectName, customErrorEvent);
    }
}
Also used : ProjectName(com.google.devtools.clouderrorreporting.v1beta1.ProjectName) ErrorContext(com.google.devtools.clouderrorreporting.v1beta1.ErrorContext) ReportedErrorEvent(com.google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent) ReportErrorsServiceClient(com.google.cloud.errorreporting.v1beta1.ReportErrorsServiceClient)

Example 40 with ProjectName

use of com.google.devtools.clouderrorreporting.v1beta1.ProjectName in project spring-cloud-gcp by spring-cloud.

the class PubSubSampleApplicationTests method getMessagesFromSubscription.

private List<String> getMessagesFromSubscription(String subscriptionName) {
    String projectSubscriptionName = ProjectSubscriptionName.format(projectName, subscriptionName);
    PullRequest pullRequest = PullRequest.newBuilder().setReturnImmediately(true).setMaxMessages(10).setSubscription(projectSubscriptionName).build();
    PullResponse pullResponse = subscriptionAdminClient.getStub().pullCallable().call(pullRequest);
    return pullResponse.getReceivedMessagesList().stream().map((message) -> message.getMessage().getData().toStringUtf8()).collect(Collectors.toList());
}
Also used : Topic(com.google.pubsub.v1.Topic) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Assume.assumeThat(org.junit.Assume.assumeThat) BeforeClass(org.junit.BeforeClass) WebEnvironment(org.springframework.boot.test.context.SpringBootTest.WebEnvironment) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) ListTopicsPagedResponse(com.google.cloud.pubsub.v1.TopicAdminClient.ListTopicsPagedResponse) ServiceOptions(com.google.cloud.ServiceOptions) SubscriptionAdminClient(com.google.cloud.pubsub.v1.SubscriptionAdminClient) StreamSupport(java.util.stream.StreamSupport) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) SpringRunner(org.springframework.test.context.junit4.SpringRunner) PullResponse(com.google.pubsub.v1.PullResponse) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Awaitility.await(org.awaitility.Awaitility.await) IOException(java.io.IOException) Test(org.junit.Test) PullRequest(com.google.pubsub.v1.PullRequest) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) LocalServerPort(org.springframework.boot.web.server.LocalServerPort) List(java.util.List) Rule(org.junit.Rule) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) OutputCaptureRule(org.springframework.boot.test.system.OutputCaptureRule) TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) ListSubscriptionsPagedResponse(com.google.cloud.pubsub.v1.SubscriptionAdminClient.ListSubscriptionsPagedResponse) Subscription(com.google.pubsub.v1.Subscription) ResponseEntity(org.springframework.http.ResponseEntity) Matchers.is(org.hamcrest.Matchers.is) PushConfig(com.google.pubsub.v1.PushConfig) TestRestTemplate(org.springframework.boot.test.web.client.TestRestTemplate) ProjectName(com.google.pubsub.v1.ProjectName) PullResponse(com.google.pubsub.v1.PullResponse) PullRequest(com.google.pubsub.v1.PullRequest)

Aggregations

Test (org.junit.Test)27 ProjectName (com.google.monitoring.v3.ProjectName)21 ApiException (com.google.api.gax.grpc.ApiException)13 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)13 StatusRuntimeException (io.grpc.StatusRuntimeException)13 ProjectName (com.google.devtools.clouderrorreporting.v1beta1.ProjectName)10 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)9 TimeSeries (com.google.monitoring.v3.TimeSeries)9 TimeInterval (com.google.monitoring.v3.TimeInterval)8 ProjectName (com.google.pubsub.v1.ProjectName)8 ListTimeSeriesRequest (com.google.monitoring.v3.ListTimeSeriesRequest)6 MetricDescriptor (com.google.api.MetricDescriptor)5 ListTimeSeriesPagedResponse (com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse)4 ReportedErrorEvent (com.google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent)4 CreateTimeSeriesRequest (com.google.monitoring.v3.CreateTimeSeriesRequest)3 ArrayList (java.util.ArrayList)3 Metric (com.google.api.Metric)2 MonitoredResource (com.google.api.MonitoredResource)2 MonitoredResourceDescriptor (com.google.api.MonitoredResourceDescriptor)2 ReportErrorsServiceClient (com.google.cloud.errorreporting.v1beta1.ReportErrorsServiceClient)2