Obtén métricas de trabajo

Demuestra cómo obtener una colección de métricas que describen el progreso de un trabajo.

Muestra de código

C#

Para autenticarte en Dataflow, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

using Google.Cloud.Dataflow.V1Beta3;
using Google.Protobuf.WellKnownTypes;

public sealed partial class GeneratedMetricsV1Beta3ClientSnippets
{
    /// <summary>Snippet for GetJobMetrics</summary>
    /// <remarks>
    /// This snippet has been automatically generated and should be regarded as a code template only.
    /// It will require modifications to work:
    /// - It may require correct/in-range values for request initialization.
    /// - It may require specifying regional endpoints when creating the service client as shown in
    ///   https://cloud.google.com/dotnet/docs/reference/help/client-configuration#endpoint.
    /// </remarks>
    public void GetJobMetricsRequestObject()
    {
        // Create client
        MetricsV1Beta3Client metricsV1Beta3Client = MetricsV1Beta3Client.Create();
        // Initialize request argument(s)
        GetJobMetricsRequest request = new GetJobMetricsRequest
        {
            ProjectId = "",
            JobId = "",
            StartTime = new Timestamp(),
            Location = "",
        };
        // Make the request
        JobMetrics response = metricsV1Beta3Client.GetJobMetrics(request);
    }
}

Go

Para autenticarte en Dataflow, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.


import (
	"context"
	"fmt"
	"io"

	dataflow "cloud.google.com/go/dataflow/apiv1beta3"
	dataflowpb "cloud.google.com/go/dataflow/apiv1beta3/dataflowpb"
)

// getJobMetrics retrieves runtime metrics for a specific Dataflow job.
func getJobMetrics(ctx context.Context, w io.Writer, projectID string, locationID string, jobID string) error {
	client, err := dataflow.NewMetricsV1Beta3Client(ctx)
	if err != nil {
		return fmt.Errorf("NewMetricsV1Beta3Client failed: %w", err)
	}
	defer client.Close()

	req := &dataflowpb.GetJobMetricsRequest{
		ProjectId: projectID,
		Location:  locationID,
		JobId:     jobID,
	}

	resp, err := client.GetJobMetrics(ctx, req)
	if err != nil {
		return fmt.Errorf("GetJobMetrics: %w", err)
	}

	fmt.Fprintf(w, "Retrieved %d metrics for job %s\n", len(resp.GetMetrics()), jobID)
	for _, metric := range resp.GetMetrics() {
		fmt.Fprintf(w, "Metric: %s\n", metric.GetName().GetName())
	}

	return nil
}

Java

Para autenticarte en Dataflow, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

import com.google.dataflow.v1beta3.GetJobMetricsRequest;
import com.google.dataflow.v1beta3.JobMetrics;
import com.google.dataflow.v1beta3.MetricUpdate;
import com.google.dataflow.v1beta3.MetricsV1Beta3Client;
import java.io.IOException;

public class GetJobMetrics {

  public static void main(String[] args) throws IOException {
    String projectId = "example-project-id";
    String locationId = "us-central1";
    String jobId = "example-job-id";
    getJobMetrics(projectId, locationId, jobId);
  }

  public static void getJobMetrics(String projectId, String locationId, String jobId)
      throws IOException {
    try (MetricsV1Beta3Client client = MetricsV1Beta3Client.create()) {
      GetJobMetricsRequest request =
          GetJobMetricsRequest.newBuilder()
              .setProjectId(projectId)
              .setLocation(locationId)
              .setJobId(jobId)
              .build();

      JobMetrics response = client.getJobMetrics(request);
      System.out.printf("Retrieved %d metrics for job %s.%n", response.getMetricsCount(), jobId);
      for (MetricUpdate metric : response.getMetricsList()) {
        System.out.printf("Metric name: %s%n", metric.getName().getName());
      }
    }
  }
}

Python

Para autenticarte en Dataflow, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

# This snippet has been automatically generated and should be regarded as a
# code template only.
# It will require modifications to work:
# - It may require correct/in-range values for request initialization.
# - It may require specifying regional endpoints when creating the service
#   client as shown in:
#   https://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import dataflow_v1beta3


def sample_get_job_metrics():
    # Create a client
    client = dataflow_v1beta3.MetricsV1Beta3Client()

    # Initialize request argument(s)
    request = dataflow_v1beta3.GetJobMetricsRequest()

    # Make the request
    response = client.get_job_metrics(request=request)

    # Handle the response
    print(response)

¿Qué sigue?

Si quieres buscar y filtrar muestras de código para otros productos de Google Cloud , consulta el navegador de muestras deGoogle Cloud .