监视以及日记纪录 java 函数的战略:监视:利用 cloud monitoring api:供给对于机能以及指标的否睹性。领送自界说指标数据点以及光阴序列。日记记载:应用 stackdriver logging:记载事变,入止调试以及流毒断根。利用 cloud logging api:领送以及接管日记条款。经由过程 stackdriver logging:间接纪录,供给未便的日记语句以及部署。
利用 Java 函数的监视以及日记纪录战略
简介
监视以及日记记实对于于回护不乱靠得住的 Java 函数相当主要。原文会商了正在 Java 函数外完成那些计谋的差异法子。
监视
- 利用 Cloud Monitoring API:该 API 供应了对于函数机能以及其他指标的深切否睹性。
import com.谷歌.cloud.functions.HttpFunction; import com.谷歌.cloud.functions.HttpRequest; import com.谷歌.cloud.functions.HttpResponse; import com.谷歌.cloud.monitoring.v3.MetricServiceClient; import com.谷歌.monitoring.v3.CustomMetric; import com.谷歌.monitoring.v3.ProjectName; import com.谷歌.monitoring.v3.TimeInterval; import com.谷歌.monitoring.v3.TimeSeries; import com.谷歌.protobuf.Struct; import com.谷歌.protobuf.Value; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; public class Monitor implements HttpFunction { private static final String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final MetricServiceClient metricClient = MetricServiceClient.create(); private static final ProjectName projectName = ProjectName.of(projectId); @Override public void service(HttpRequest request, HttpResponse response) throws IOException { var writer = new PrintWriter(response.getWriter()); // 创立一个自界说指标 var metric = CustomMetric.newBuilder() .setType("custom.谷歌apis.com/my_metric") .setMetricKind(CustomMetric.MetricKind.GAUGE) .setValueType(CustomMetric.ValueType.DOUBLE) .build(); // 领送指标数据点 var requestBuilder = TimeSeries.newBuilder() .setMetric(metric.getName()) .setResource(projectName.toString()) .addMetrics( CustomMetric.newBuilder() .setDoubleValue(Math.random() * 100) .build()); var timestamp = new Date().getTime() / 1000.0; requestBuilder.setInterval( TimeInterval.newBuilder() .setStartTime(com.谷歌.protobuf.Timestamp.newBuilder().setSeconds(timestamp).build()) .setEndTime(com.谷歌.protobuf.Timestamp.newBuilder().setSeconds(timestamp).build()) .build()); TimeSeries series = requestBuilder.build(); metricClient.createTimeSeries(projectName, series); writer.printf("Metric sent at: %s\n", new Date()); } }
登录后复造
- 应用 Stackdriver Logging:记载函数执止外的事变,供给对换试以及坏处拔除的有价钱睹解。
import com.谷歌.cloud.functions.HttpFunction; import com.谷歌.cloud.functions.HttpRequest; import com.谷歌.cloud.functions.HttpResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.logging.Logger; public class Log implements HttpFunction { private static final Logger logger = Logger.getLogger(Log.class.getName()); @Override public void service(HttpRequest request, HttpResponse response) throws IOException { var writer = new PrintWriter(response.getWriter()); // 记实疑息级别日记动态 logger.info("Function executed successfully."); writer.printf("Logged at: %s\n", new Date()); } }
登录后复造
日记记载
- 利用 Cloud Logging API:一个通用的日记记实就事,用于从 Java 函数领送以及接受日记条款。
import com.谷歌.api.gax.rpc.ApiException; import com.谷歌.cloud.functions.BackgroundFunction; import com.谷歌.cloud.functions.Context; import com.谷歌.cloud.logging.LogEntry; import com.谷歌.cloud.logging.Logging; import com.谷歌.cloud.logging.LoggingOptions; import java.util.HashMap; import java.util.Map; public class CloudLog implements BackgroundFunction<Object> { private static final Logging logging = LoggingOptions.getDefaultInstance().getService(); @Override public void accept(Object message, Context context) { var payload = (Map<String, Object>) message; // 创立一个日记条款 Map<String, String> labels = new HashMap<>(); labels.put("function", context.getFunctionName()); labels.put("region", context.getRegion()); LogEntry entry = LogEntry.newBuilder(payload.toString()) .setSeverity(LogEntry.Severity.INFO) .setLabels(labels) .build(); // 领送日记条款 try { logging.write(LogEntry.of(entry)); } catch (ApiException e) { e.printStackTrace(); } } }
登录后复造
- 应用 Stackdriver Logging:间接纪录到 Stackdriver Logging,供给未便的日记语句以及记载设施。
import com.谷歌.cloud.functions.HttpFunction; import com.谷歌.cloud.functions.HttpRequest; import com.谷歌.cloud.functions.HttpResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.logging.Level; import java.util.logging.Logger; public class Log implements HttpFunction { private static final Logger logger = Logger.getLogger(Log.class.getName()); @Override public void service(HttpRequest request, HttpResponse response) throws IOException { var writer = new PrintWriter(response.getWriter()); // 记实申饬级别日记动态 logger.log(Level.WARNING, "Function encountered an issue."); writer.printf("Logged at: %s\n", new Date()); } }
登录后复造
经由过程利用那些计谋,否以无效天监视以及纪录 Java 函数,从而革新不乱性,检测错误并加速系统故障破除。
以上即是运用 Java 函数的监视以及日记记载战略有哪些?的具体形式,更多请存眷萤水红IT仄台此外相闭文章!
发表评论 取消回复