|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.datastore.samples; |
| 18 | + |
| 19 | +import com.google.cloud.datastore.Datastore; |
| 20 | +import com.google.cloud.datastore.DatastoreOptions; |
| 21 | +import com.google.cloud.datastore.Entity; |
| 22 | +import com.google.cloud.datastore.Key; |
| 23 | + |
| 24 | +/** A simple sample to verify that Datastore metrics are being recorded and exported correctly. */ |
| 25 | +public class MetricsSample { |
| 26 | + |
| 27 | + public static void main(String[] args) throws Exception { |
| 28 | + String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 29 | + if (projectId == null || projectId.isEmpty()) { |
| 30 | + System.err.println("Error: GOOGLE_CLOUD_PROJECT environment variable is not set."); |
| 31 | + System.exit(1); |
| 32 | + } |
| 33 | + |
| 34 | + System.out.println("Starting MetricsSample for project: " + projectId); |
| 35 | + |
| 36 | + // Initialize Datastore client with metrics enabled by default. |
| 37 | + Datastore datastore = |
| 38 | + DatastoreOptions.newBuilder().setProjectId(projectId).build().getService(); |
| 39 | + |
| 40 | + System.out.println("Executing operations to generate metrics..."); |
| 41 | + |
| 42 | + // 1. Basic Key Lookup |
| 43 | + Key key = datastore.newKeyFactory().setKind("MetricsTask").newKey("sample-task"); |
| 44 | + datastore.get(key); |
| 45 | + System.out.println("Performed lookup."); |
| 46 | + |
| 47 | + // 2. Transactional Read/Write |
| 48 | + datastore.runInTransaction( |
| 49 | + tx -> { |
| 50 | + Entity entity = |
| 51 | + Entity.newBuilder(key) |
| 52 | + .set("description", "Recorded via MetricsSample") |
| 53 | + .set("timestamp", System.currentTimeMillis()) |
| 54 | + .build(); |
| 55 | + tx.put(entity); |
| 56 | + return null; |
| 57 | + }); |
| 58 | + System.out.println("Performed transaction."); |
| 59 | + |
| 60 | + // 3. Batch operation |
| 61 | + datastore.delete(key); |
| 62 | + System.out.println("Performed delete."); |
| 63 | + |
| 64 | + System.out.println("Waiting 65 seconds for metrics to be flushed to Cloud Monitoring..."); |
| 65 | + // PeriodicMetricReader default interval is 60s. |
| 66 | + Thread.sleep(65000); |
| 67 | + |
| 68 | + System.out.println("Sample finished. Check the Cloud Monitoring dashboard for:"); |
| 69 | + System.out.println(" - firestore.googleapis.com/internal/client/operation_latency"); |
| 70 | + System.out.println(" - firestore.googleapis.com/internal/client/transaction_latency"); |
| 71 | + } |
| 72 | +} |
0 commit comments