Class MetricsClient (3.8.0)

public class MetricsClient implements BackgroundResource

Service Description: Service for configuring logs-based metrics.

This class provides the ability to make remote calls to the backing service through method calls that map to API methods. Sample code to get started:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   LogMetricName metricName = LogMetricName.of("[PROJECT]", "[METRIC]");
   LogMetric response = metricsClient.getLogMetric(metricName);
 }
 

Note: close() needs to be called on the MetricsClient object to clean up resources such as threads. In the example above, try-with-resources is used, which automatically calls close().

The surface of this class includes several types of Java methods for each of the API's methods:

  1. A "flattened" method. With this type of method, the fields of the request type have been converted into function parameters. It may be the case that not all fields are available as parameters, and not every API method will have a flattened method entry point.
  2. A "request object" method. This type of method only takes one parameter, a request object, which must be constructed before the call. Not every API method will have a request object method.
  3. A "callable" method. This type of method takes no parameters and returns an immutable API callable object, which can be used to initiate calls to the service.

See the individual methods for example code.

Many parameters require resource names to be formatted in a particular way. To assist with these names, this class includes a format method for each type of name, and additionally a parse method to extract the individual identifiers contained within names that are returned.

This class can be customized by passing in a custom instance of MetricsSettings to create(). For example:

To customize credentials:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 MetricsSettings metricsSettings =
     MetricsSettings.newBuilder()
         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
         .build();
 MetricsClient metricsClient = MetricsClient.create(metricsSettings);
 

To customize the endpoint:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 MetricsSettings metricsSettings = MetricsSettings.newBuilder().setEndpoint(myEndpoint).build();
 MetricsClient metricsClient = MetricsClient.create(metricsSettings);
 

Please refer to the GitHub repository's samples for more quickstart code snippets.

Inheritance

java.lang.Object > MetricsClient

Implements

BackgroundResource

Static Methods

create()

public static final MetricsClient create()

Constructs an instance of MetricsClient with default settings.

Returns
TypeDescription
MetricsClient
Exceptions
TypeDescription
IOException

create(MetricsSettings settings)

public static final MetricsClient create(MetricsSettings settings)

Constructs an instance of MetricsClient, using the given settings. The channels are created based on the settings passed in, or defaults for any settings that are not set.

Parameter
NameDescription
settingsMetricsSettings
Returns
TypeDescription
MetricsClient
Exceptions
TypeDescription
IOException

create(MetricsServiceV2Stub stub)

public static final MetricsClient create(MetricsServiceV2Stub stub)

Constructs an instance of MetricsClient, using the given stub for making calls. This is for advanced usage - prefer using create(MetricsSettings).

Parameter
NameDescription
stubMetricsServiceV2Stub
Returns
TypeDescription
MetricsClient

Constructors

MetricsClient(MetricsSettings settings)

protected MetricsClient(MetricsSettings settings)

Constructs an instance of MetricsClient, using the given settings. This is protected so that it is easy to make a subclass, but otherwise, the static factory methods should be preferred.

Parameter
NameDescription
settingsMetricsSettings

MetricsClient(MetricsServiceV2Stub stub)

protected MetricsClient(MetricsServiceV2Stub stub)
Parameter
NameDescription
stubMetricsServiceV2Stub

Methods

awaitTermination(long duration, TimeUnit unit)

public boolean awaitTermination(long duration, TimeUnit unit)
Parameters
NameDescription
durationlong
unitTimeUnit
Returns
TypeDescription
boolean
Exceptions
TypeDescription
InterruptedException

close()

public final void close()

createLogMetric(CreateLogMetricRequest request)

public final LogMetric createLogMetric(CreateLogMetricRequest request)

Creates a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   CreateLogMetricRequest request =
       CreateLogMetricRequest.newBuilder()
           .setParent(ProjectName.of("[PROJECT]").toString())
           .setMetric(LogMetric.newBuilder().build())
           .build();
   LogMetric response = metricsClient.createLogMetric(request);
 }
 
Parameter
NameDescription
requestCreateLogMetricRequest

The request object containing all of the parameters for the API call.

Returns
TypeDescription
LogMetric

createLogMetric(ProjectName parent, LogMetric metric)

public final LogMetric createLogMetric(ProjectName parent, LogMetric metric)

Creates a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   ProjectName parent = ProjectName.of("[PROJECT]");
   LogMetric metric = LogMetric.newBuilder().build();
   LogMetric response = metricsClient.createLogMetric(parent, metric);
 }
 
Parameters
NameDescription
parentProjectName

Required. The resource name of the project in which to create the metric:

"projects/[PROJECT_ID]"

The new metric must be provided in the request.

metricLogMetric

Required. The new logs-based metric, which must not have an identifier that already exists.

Returns
TypeDescription
LogMetric

createLogMetric(String parent, LogMetric metric)

public final LogMetric createLogMetric(String parent, LogMetric metric)

Creates a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   String parent = ProjectName.of("[PROJECT]").toString();
   LogMetric metric = LogMetric.newBuilder().build();
   LogMetric response = metricsClient.createLogMetric(parent, metric);
 }
 
Parameters
NameDescription
parentString

Required. The resource name of the project in which to create the metric:

"projects/[PROJECT_ID]"

The new metric must be provided in the request.

metricLogMetric

Required. The new logs-based metric, which must not have an identifier that already exists.

Returns
TypeDescription
LogMetric

createLogMetricCallable()

public final UnaryCallable<CreateLogMetricRequest,LogMetric> createLogMetricCallable()

Creates a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   CreateLogMetricRequest request =
       CreateLogMetricRequest.newBuilder()
           .setParent(ProjectName.of("[PROJECT]").toString())
           .setMetric(LogMetric.newBuilder().build())
           .build();
   ApiFuture<LogMetric> future = metricsClient.createLogMetricCallable().futureCall(request);
   // Do something.
   LogMetric response = future.get();
 }
 
Returns
TypeDescription
UnaryCallable<CreateLogMetricRequest,LogMetric>

deleteLogMetric(DeleteLogMetricRequest request)

public final void deleteLogMetric(DeleteLogMetricRequest request)

Deletes a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   DeleteLogMetricRequest request =
       DeleteLogMetricRequest.newBuilder()
           .setMetricName(LogMetricName.of("[PROJECT]", "[METRIC]").toString())
           .build();
   metricsClient.deleteLogMetric(request);
 }
 
Parameter
NameDescription
requestDeleteLogMetricRequest

The request object containing all of the parameters for the API call.

deleteLogMetric(LogMetricName metricName)

public final void deleteLogMetric(LogMetricName metricName)

Deletes a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   LogMetricName metricName = LogMetricName.of("[PROJECT]", "[METRIC]");
   metricsClient.deleteLogMetric(metricName);
 }
 
Parameter
NameDescription
metricNameLogMetricName

Required. The resource name of the metric to delete:

"projects/[PROJECT_ID]/metrics/[METRIC_ID]"

deleteLogMetric(String metricName)

public final void deleteLogMetric(String metricName)

Deletes a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   String metricName = LogMetricName.of("[PROJECT]", "[METRIC]").toString();
   metricsClient.deleteLogMetric(metricName);
 }
 
Parameter
NameDescription
metricNameString

Required. The resource name of the metric to delete:

"projects/[PROJECT_ID]/metrics/[METRIC_ID]"

deleteLogMetricCallable()

public final UnaryCallable<DeleteLogMetricRequest,Empty> deleteLogMetricCallable()

Deletes a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   DeleteLogMetricRequest request =
       DeleteLogMetricRequest.newBuilder()
           .setMetricName(LogMetricName.of("[PROJECT]", "[METRIC]").toString())
           .build();
   ApiFuture<Empty> future = metricsClient.deleteLogMetricCallable().futureCall(request);
   // Do something.
   future.get();
 }
 
Returns
TypeDescription
UnaryCallable<DeleteLogMetricRequest,Empty>

getLogMetric(GetLogMetricRequest request)

public final LogMetric getLogMetric(GetLogMetricRequest request)

Gets a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   GetLogMetricRequest request =
       GetLogMetricRequest.newBuilder()
           .setMetricName(LogMetricName.of("[PROJECT]", "[METRIC]").toString())
           .build();
   LogMetric response = metricsClient.getLogMetric(request);
 }
 
Parameter
NameDescription
requestGetLogMetricRequest

The request object containing all of the parameters for the API call.

Returns
TypeDescription
LogMetric

getLogMetric(LogMetricName metricName)

public final LogMetric getLogMetric(LogMetricName metricName)

Gets a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   LogMetricName metricName = LogMetricName.of("[PROJECT]", "[METRIC]");
   LogMetric response = metricsClient.getLogMetric(metricName);
 }
 
Parameter
NameDescription
metricNameLogMetricName

Required. The resource name of the desired metric:

"projects/[PROJECT_ID]/metrics/[METRIC_ID]"

Returns
TypeDescription
LogMetric

getLogMetric(String metricName)

public final LogMetric getLogMetric(String metricName)

Gets a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   String metricName = LogMetricName.of("[PROJECT]", "[METRIC]").toString();
   LogMetric response = metricsClient.getLogMetric(metricName);
 }
 
Parameter
NameDescription
metricNameString

Required. The resource name of the desired metric:

"projects/[PROJECT_ID]/metrics/[METRIC_ID]"

Returns
TypeDescription
LogMetric

getLogMetricCallable()

public final UnaryCallable<GetLogMetricRequest,LogMetric> getLogMetricCallable()

Gets a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   GetLogMetricRequest request =
       GetLogMetricRequest.newBuilder()
           .setMetricName(LogMetricName.of("[PROJECT]", "[METRIC]").toString())
           .build();
   ApiFuture<LogMetric> future = metricsClient.getLogMetricCallable().futureCall(request);
   // Do something.
   LogMetric response = future.get();
 }
 
Returns
TypeDescription
UnaryCallable<GetLogMetricRequest,LogMetric>

getSettings()

public final MetricsSettings getSettings()
Returns
TypeDescription
MetricsSettings

getStub()

public MetricsServiceV2Stub getStub()
Returns
TypeDescription
MetricsServiceV2Stub

isShutdown()

public boolean isShutdown()
Returns
TypeDescription
boolean

isTerminated()

public boolean isTerminated()
Returns
TypeDescription
boolean

listLogMetrics(ListLogMetricsRequest request)

public final MetricsClient.ListLogMetricsPagedResponse listLogMetrics(ListLogMetricsRequest request)

Lists logs-based metrics.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   ListLogMetricsRequest request =
       ListLogMetricsRequest.newBuilder()
           .setParent(ProjectName.of("[PROJECT]").toString())
           .setPageToken("pageToken873572522")
           .setPageSize(883849137)
           .build();
   for (LogMetric element : metricsClient.listLogMetrics(request).iterateAll()) {
     // doThingsWith(element);
   }
 }
 
Parameter
NameDescription
requestListLogMetricsRequest

The request object containing all of the parameters for the API call.

Returns
TypeDescription
MetricsClient.ListLogMetricsPagedResponse

listLogMetrics(ProjectName parent)

public final MetricsClient.ListLogMetricsPagedResponse listLogMetrics(ProjectName parent)

Lists logs-based metrics.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   ProjectName parent = ProjectName.of("[PROJECT]");
   for (LogMetric element : metricsClient.listLogMetrics(parent).iterateAll()) {
     // doThingsWith(element);
   }
 }
 
Parameter
NameDescription
parentProjectName

Required. The name of the project containing the metrics:

"projects/[PROJECT_ID]"

Returns
TypeDescription
MetricsClient.ListLogMetricsPagedResponse

listLogMetrics(String parent)

public final MetricsClient.ListLogMetricsPagedResponse listLogMetrics(String parent)

Lists logs-based metrics.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   String parent = ProjectName.of("[PROJECT]").toString();
   for (LogMetric element : metricsClient.listLogMetrics(parent).iterateAll()) {
     // doThingsWith(element);
   }
 }
 
Parameter
NameDescription
parentString

Required. The name of the project containing the metrics:

"projects/[PROJECT_ID]"

Returns
TypeDescription
MetricsClient.ListLogMetricsPagedResponse

listLogMetricsCallable()

public final UnaryCallable<ListLogMetricsRequest,ListLogMetricsResponse> listLogMetricsCallable()

Lists logs-based metrics.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   ListLogMetricsRequest request =
       ListLogMetricsRequest.newBuilder()
           .setParent(ProjectName.of("[PROJECT]").toString())
           .setPageToken("pageToken873572522")
           .setPageSize(883849137)
           .build();
   while (true) {
     ListLogMetricsResponse response = metricsClient.listLogMetricsCallable().call(request);
     for (LogMetric element : response.getResponsesList()) {
       // doThingsWith(element);
     }
     String nextPageToken = response.getNextPageToken();
     if (!Strings.isNullOrEmpty(nextPageToken)) {
       request = request.toBuilder().setPageToken(nextPageToken).build();
     } else {
       break;
     }
   }
 }
 
Returns
TypeDescription
UnaryCallable<ListLogMetricsRequest,ListLogMetricsResponse>

listLogMetricsPagedCallable()

public final UnaryCallable<ListLogMetricsRequest,MetricsClient.ListLogMetricsPagedResponse> listLogMetricsPagedCallable()

Lists logs-based metrics.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   ListLogMetricsRequest request =
       ListLogMetricsRequest.newBuilder()
           .setParent(ProjectName.of("[PROJECT]").toString())
           .setPageToken("pageToken873572522")
           .setPageSize(883849137)
           .build();
   ApiFuture<LogMetric> future = metricsClient.listLogMetricsPagedCallable().futureCall(request);
   // Do something.
   for (LogMetric element : future.get().iterateAll()) {
     // doThingsWith(element);
   }
 }
 
Returns
TypeDescription
UnaryCallable<ListLogMetricsRequest,ListLogMetricsPagedResponse>

shutdown()

public void shutdown()

shutdownNow()

public void shutdownNow()

updateLogMetric(LogMetricName metricName, LogMetric metric)

public final LogMetric updateLogMetric(LogMetricName metricName, LogMetric metric)

Creates or updates a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   LogMetricName metricName = LogMetricName.of("[PROJECT]", "[METRIC]");
   LogMetric metric = LogMetric.newBuilder().build();
   LogMetric response = metricsClient.updateLogMetric(metricName, metric);
 }
 
Parameters
NameDescription
metricNameLogMetricName

Required. The resource name of the metric to update:

"projects/[PROJECT_ID]/metrics/[METRIC_ID]"

The updated metric must be provided in the request and it's name field must be the same as [METRIC_ID] If the metric does not exist in [PROJECT_ID], then a new metric is created.

metricLogMetric

Required. The updated metric.

Returns
TypeDescription
LogMetric

updateLogMetric(UpdateLogMetricRequest request)

public final LogMetric updateLogMetric(UpdateLogMetricRequest request)

Creates or updates a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   UpdateLogMetricRequest request =
       UpdateLogMetricRequest.newBuilder()
           .setMetricName(LogMetricName.of("[PROJECT]", "[METRIC]").toString())
           .setMetric(LogMetric.newBuilder().build())
           .build();
   LogMetric response = metricsClient.updateLogMetric(request);
 }
 
Parameter
NameDescription
requestUpdateLogMetricRequest

The request object containing all of the parameters for the API call.

Returns
TypeDescription
LogMetric

updateLogMetric(String metricName, LogMetric metric)

public final LogMetric updateLogMetric(String metricName, LogMetric metric)

Creates or updates a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   String metricName = LogMetricName.of("[PROJECT]", "[METRIC]").toString();
   LogMetric metric = LogMetric.newBuilder().build();
   LogMetric response = metricsClient.updateLogMetric(metricName, metric);
 }
 
Parameters
NameDescription
metricNameString

Required. The resource name of the metric to update:

"projects/[PROJECT_ID]/metrics/[METRIC_ID]"

The updated metric must be provided in the request and it's name field must be the same as [METRIC_ID] If the metric does not exist in [PROJECT_ID], then a new metric is created.

metricLogMetric

Required. The updated metric.

Returns
TypeDescription
LogMetric

updateLogMetricCallable()

public final UnaryCallable<UpdateLogMetricRequest,LogMetric> updateLogMetricCallable()

Creates or updates a logs-based metric.

Sample code:


 // This snippet has been automatically generated for illustrative purposes only.
 // It may require modifications to work in your environment.
 try (MetricsClient metricsClient = MetricsClient.create()) {
   UpdateLogMetricRequest request =
       UpdateLogMetricRequest.newBuilder()
           .setMetricName(LogMetricName.of("[PROJECT]", "[METRIC]").toString())
           .setMetric(LogMetric.newBuilder().build())
           .build();
   ApiFuture<LogMetric> future = metricsClient.updateLogMetricCallable().futureCall(request);
   // Do something.
   LogMetric response = future.get();
 }
 
Returns
TypeDescription
UnaryCallable<UpdateLogMetricRequest,LogMetric>