Supprimer une métrique personnalisée
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Explique comment supprimer une métrique personnalisée.
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez les articles suivants :
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],[],[],[],null,["Demonstrates how to delete a custom metric.\n\nExplore further\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Create user-defined metrics with the API](/monitoring/custom-metrics/creating-metrics)\n\nCode sample \n\nC#\n\n\nTo authenticate to Monitoring, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n public static object DeleteMetric(string projectId, string metricType)\n {\n // Create client.\n MetricServiceClient metricServiceClient = MetricServiceClient.Create();\n // Initialize request argument(s).\n MetricDescriptorName name = new MetricDescriptorName(projectId, metricType);\n // Make the request.\n metricServiceClient.DeleteMetricDescriptor(name);\n Console.WriteLine($\"Done deleting metric descriptor: {name}\");\n return 0;\n }\n\nGo\n\n\nTo authenticate to Monitoring, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tmonitoring \"cloud.google.com/go/monitoring/apiv3\"\n \t\"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb\"\n )\n\n // deleteMetric deletes the given metric. name should be of the form\n // \"projects/PROJECT_ID/metricDescriptors/METRIC_TYPE\".\n func deleteMetric(w io.Writer, name string) error {\n \tctx := context.Background()\n \tc, err := monitoring.NewMetricClient(ctx)\n \tif err != nil {\n \t\treturn err\n \t}\n \tdefer c.Close()\n \treq := &monitoringpb.DeleteMetricDescriptorRequest{\n \t\tName: name,\n \t}\n\n \tif err := c.DeleteMetricDescriptor(ctx, req); err != nil {\n \t\treturn fmt.Errorf(\"could not delete metric: %w\", err)\n \t}\n \tfmt.Fprintf(w, \"Deleted metric: %q\\n\", name)\n \treturn nil\n }\n\nJava\n\n\nTo authenticate to Monitoring, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n final String projectId = System.getProperty(\"projectId\");\n try (final MetricServiceClient client = MetricServiceClient.create();) {\n MetricDescriptorName metricName = MetricDescriptorName.of(projectId, type);\n client.deleteMetricDescriptor(metricName);\n System.out.println(\"Deleted descriptor \" + type);\n }\n\nNode.js\n\n\nTo authenticate to Monitoring, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n // Imports the Google Cloud client library\n const monitoring = require('https://cloud.google.com/nodejs/docs/reference/monitoring/latest/overview.html');\n\n // Creates a client\n const client = new monitoring.https://cloud.google.com/nodejs/docs/reference/monitoring/latest/overview.html();\n\n async function deleteMetricDescriptor() {\n /**\n * TODO(developer): Uncomment and edit the following lines of code.\n */\n // const projectId = 'YOUR_PROJECT_ID';\n // const metricId = 'custom.googleapis.com/stores/daily_sales';\n\n const request = {\n name: client.https://cloud.google.com/nodejs/docs/reference/monitoring/latest/monitoring/v3.metricserviceclient.html(projectId, metricId),\n };\n\n // Deletes a metric descriptor\n const [result] = await client.deleteMetricDescriptor(request);\n console.log(`Deleted ${metricId}`, result);\n }\n deleteMetricDescriptor();\n\nPHP\n\n\nTo authenticate to Monitoring, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n use Google\\Cloud\\Monitoring\\V3\\Client\\MetricServiceClient;\n use Google\\Cloud\\Monitoring\\V3\\DeleteMetricDescriptorRequest;\n\n /**\n * Example:\n * ```\n * delete_metric($projectId, $databaseId);\n * ```\n *\n * @param string $projectId Your project ID\n * @param string $metricId The ID of the Metric Descriptor to delete\n */\n function delete_metric($projectId, $metricId)\n {\n $metrics = new MetricServiceClient([\n 'projectId' =\u003e $projectId,\n ]);\n\n $metricPath = $metrics-\u003emetricDescriptorName($projectId, $metricId);\n $deleteMetricDescriptorRequest = (new DeleteMetricDescriptorRequest())\n -\u003esetName($metricPath);\n $metrics-\u003edeleteMetricDescriptor($deleteMetricDescriptorRequest);\n\n printf('Deleted a metric: ' . $metricPath . PHP_EOL);\n }\n\nPython\n\n\nTo authenticate to Monitoring, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/monitoring/latest/\n\n client = https://cloud.google.com/python/docs/reference/monitoring/latest/.https://cloud.google.com/python/docs/reference/monitoring/latest/google.cloud.monitoring_v3.services.metric_service.MetricServiceClient.html()\n client.https://cloud.google.com/python/docs/reference/monitoring/latest/google.cloud.monitoring_v3.services.metric_service.MetricServiceClient.html#google_cloud_monitoring_v3_services_metric_service_MetricServiceClient_delete_metric_descriptor(name=descriptor_name)\n print(\"Deleted metric descriptor {}.\".format(descriptor_name))\n\nRuby\n\n\nTo authenticate to Monitoring, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n gem \"google-cloud-monitoring\"\n require \"google/cloud/monitoring\"\n\n # Your Google Cloud Platform project ID\n # project_id = \"YOUR_PROJECT_ID\"\n\n # Example metric type\n # metric_type = \"custom.googleapis.com/my_metric\"\n\n client = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-monitoring/latest/Google-Cloud-Monitoring.html.https://cloud.google.com/ruby/docs/reference/google-cloud-monitoring/latest/Google-Cloud-Monitoring.html\n metric_name = client.https://cloud.google.com/ruby/docs/reference/google-cloud-monitoring-v3/latest/Google-Cloud-Monitoring-V3-MetricService-Paths.html project: project_id,\n metric_descriptor: metric_type\n\n client.delete_metric_descriptor name: metric_name\n p \"Deleted metric descriptor #{metric_name}.\"\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=monitoring)."]]