gcloud alpha monitoring policies list
gcloud alpha monitoring policies update \ "projects/my-project/alertPolicies/12345" \ --update-user-labels=team=myteamname
gcloud alpha monitoring policies list --filter='user_label.team="myteamname"'
gcloud alpha monitoring channels list
gcloud alpha monitoring channels create \ --display-name="Anastasia Alertmaestro" \ --type="email" \ --channel-labels=email_address=aamaestro@alertme.tld
gcloud alpha monitoring policies update \ "projects/my-project/alertPolicies/12345" \ --add-notification-channels="projects/my-project/notificationChannels/56789"
gcloud alpha monitoring channels update \ "projects/my-project/notificationChannels/9817323" \ --enabled=false
「当社ではサービスのスケーラビリティを向上させる取り組みの一環として Stackdriver Profiler を利用しました。これにより、CPU 時間を最適化してコストを削減できる箇所を特定できるようになり、非常に役に立ちました。」 —Evan Yin 氏、Snap のソフトウェア エンジニア
「Stackdriver Profiler によって、コード内のどこが非常に遅いのかを特定できました。巨大で複雑なバッチ プロセスの中に隠れていたのです。日々、何百ものバッチを実行しており、それぞれデータセットや設定が異なるため、クライアント固有の設定に関するパフォーマンスの問題を追跡することは難しいのですが、Stackdriver Profiler のおかげでとても助かりました。」 —Nicolas Fonrose 氏、Teevity の CEO
gcloud beta compute instance-groups managed set-autoscaling \ my-queue-based-scaling-group --zone us-central1-b --project my-project \ --min-num-replicas 0 \ --max-num-replicas 20 \ --update-stackdriver-metric \ pubsub.googleapis.com/subscription/num_undelivered_messages \ --stackdriver-metric-single-instance-assignment 2 \ --stackdriver-metric-filter \ 'resource.type = pubsub_subscription AND resource.label.subscription_id = "MY_SUBSCRIPTION"'
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) { // Initialize Stackdriver Logging loggerFactory.AddGoogle("YOUR-GOOGLE-PROJECT-ID"); ... } public void LogMessage(ILoggerFactory loggerFactory) { // Send a log to Stackdriver Logging var logger = loggerFactory.CreateLogger("NetworkLog"); logger.LogInformation("This is a log message."); }
public void ConfigureServices(IServiceCollection services) { services.AddGoogleExceptionLogging(options => { options.ProjectId = "YOUR-GOOGLE-PROJECT-ID"; options.ServiceName = "ImageGenerator"; options.Version = "1.0.2"; }); ... } public void Configure(IApplicationBuilder app) { // Use before handling any requests to ensure all unhandled exceptions are reported. app.UseGoogleExceptionLogging(); ... }
public void ReadFile(IExceptionLogger exceptionLogger) { try { string scores = File.ReadAllText(@"C:\Scores.txt"); Console.WriteLine(scores); } catch (IOException e) { exceptionLogger.Log(e); } }
public void ConfigureServices(IServiceCollection services) { string projectId = "YOUR-GOOGLE-PROJECT-ID"; services.AddGoogleTrace(options => { options.ProjectId = projectId; }); ... } public void Configure(IApplicationBuilder app) { // Use at the start of the request pipeline to ensure the entire request is traced. app.UseGoogleTrace(); ... }
public void TraceHelloWorld(IManagedTracer tracer) { using (tracer.StartSpan(nameof(TraceHelloWorld))) { Console.Out.WriteLine("Hello, World!"); } }
Private Logs Viewer
「Google Cloud Audit Logging は本当に使いやすく、BigQuery と統合すれば、すべてのアプリケーションを 1 か所で監視できる強力なツールになります」 — Darren Cibis, Shine Solutions
SetIamPolicy
Copyright 2017 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 'use strict'; exports.processFirewallAuditLogs = (event) => { const msg = JSON.parse(Buffer.from(event.data.data, 'base64').toString()); const logEntry = msg.protoPayload; if (logEntry && logEntry.request && logEntry.methodName === 'v1.compute.firewalls.insert') { let cancelFirewall = false; const allowed = logEntry.request.alloweds; if (allowed) { for (let key in allowed) { const entry = allowed[key]; for (let port in entry.ports) { if (parseInt(entry.ports[port], 10) !== 22) { cancelFirewall = true; break; } } } } if (cancelFirewall) { const resourceArray = logEntry.resourceName.split('/'); const resourceName = resourceArray[resourceArray.length - 1]; const compute = require('@google-cloud/compute')(); return compute.firewall(resourceName).delete(); } } return true; };
index.js
package.json
{ "name" : "audit-log-monitoring", "version" : "1.0.0", "description" : "monitor my audit logs", "main" : "index.js", "dependencies" : { "@google-cloud/compute" : "^0.4.1" } }
textPayload: “Successfully sent to Google Cloud Logging API”
<configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <appender name="CloudLogger" type="Google.Cloud.Logging.Log4Net.GoogleStackdriverAppender,Google.Cloud.Logging.Log4Net"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message" /> </layout> <projectId value="YOUR-PROJECT-ID" /> <logId value="mySampleLog" /> </appender> <root> <level value="ALL" /> <appender-ref ref="CloudLogger" /> </root> </log4net>
log4net.Config.XmlConfigurator.Configure();
protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); // Configure log4net to use Stackdriver logging from the XML configuration file. log4net.Config.XmlConfigurator.Configure(); }
using log4net;
// Retrieve a logger for this context. ILog log = LogManager.GetLogger(typeof(WebApiConfig)); // Log some information to Google Stackdriver Logging. log.Info("Hello World.");
using Google.Cloud.Diagnostics.AspNet;
public static void Register(HttpConfiguration config) { // Add a catch all for the uncaught exceptions. string projectId = "YOUR-PROJECT-ID"; string serviceName = "NAME-OF-YOUR-SERVICE"; string version = "VERSION-OF-YOUR-SERVICE"; // Add a catch all for the uncaught exceptions. config.Services.Add(typeof(IExceptionLogger), ErrorReportingExceptionLogger.Create(projectId, serviceName, version)); }