在Stackdriver记录中标记正确的GCE实例名称 [英] Tagging proper GCE Instance name in Stackdriver logging

本文介绍了在Stackdriver记录中标记正确的GCE实例名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用在Debian上运行的Google Compute Engine实例来运行用Node.js编写的服务.

I am using Google Compute Engine Instance running on Debian for running a service written in Node.js.

出于日志记录的目的,我正在使用Stackdriver日志记录.正在生成日志,但是,未以正确的GCE实例名称过滤日志条目.这是日志条目中资源标志的方式

For logging purpose, I am using Stackdriver logging. The logs are getting generated, however, The log entries are not getting filtered under proper GCE Instance name. Here's how the resource flags are in a log entry

resource: {
  labels: {
   instance_id:  "6000000000000000000" //A numeric id
   project_id:  "my-project"
   zone:  ""
  }
  type:  "gce_instance"   
 }

实例ID正在作为数字ID生成,但是实例名称未在资源标签内生成,因此,我必须在Stackdriver中选择GCE实例后检查数字ID,并且它不在下面实例的名称.另外,如果我选择实例并单击查看日志"选项,则会为实例名称而不是实例ID设置stackdriver标志过滤器,因此我也无法获得正确的日志. name参数不会自动生成.这就是应该的

The instance id is getting generated as a numeric id, but the name of the instance is not generated inside the resource labels, thus, I have to check for numeric id after selecting GCE Instance in Stackdriver and it doesn't come under the name of the instance. Also if I select the instance and click on the View logs option, it set the stackdriver flag filter for the instance name, not the instance id, thus I don't get proper logs too. The name parameter is not getting generated automatically. Here's what it should be

resource: {
  labels: {
   instance_id:  "6000000000000000000" //A numeric id
   name: "instance-name"
   project_id:  "my-project"
   zone:  ""
  }
  type:  "gce_instance"   
 }

我在写日志条目时没有添加任何自定义标签,因此我假设它应该自动生成.

I am not adding any custom labels while writing a log entry, thus I am assuming it should generate automatically.

这是我的日志记录服务代码.

Here's my logging service code.

const Logging = require('@google-cloud/logging');

function write(data) {
  const logging = new Logging();
  const logObj = logging.log('my-service');
  const logData = data.logData;
  const logText = data.logText;
  const severity = data.severity || 'DEFAULT';
  const httpRequest = data.httpRequest;

  // The metadata associated with the entry
  const metadata = {
    severity: severity,
    httpRequest: httpRequest
  };

  const logPayload = {
    text: logText,
    data: logData
  };

  // Prepares a log entry
  const entry = logObj.entry(metadata, logPayload);
  await logObj.write(entry);
}

这就是我的称呼-

loggingService.write({
  httpRequest: httpRequest,
  logText: 'Text Data',
  logData: logDataDump.dump,
  severity: loggingService.DEBUG
});

那么,有什么方法可以在登录Stackdriver时自动在资源标志中生成实例名称吗?

So, is there any way to auto-generate the instance name in the resource flag while logging to Stackdriver?

推荐答案

默认情况下,仅有instance_id和project_id可用于资源标记.我在他们的Github存储库上提出了问题,也在那里添加了实例名称.

By default, there's just instance_id and project_id available for resource flag. I have raised an issue on their Github repo to add instance name also there.

这是当代码在GCE实例上运行时自动设置资源标志时的代码.

This is the code they have when setting resource flag automatically when the code runs on GCE instance.

Metadata.getGCEDescriptor = function(callback) {
  gcpMetadata
    .instance('id')
    .then(function(resp) {
      callback(null, {
        type: 'gce_instance',
        labels: {
          instance_id: resp.data,
        },
      });
    })
    .catch(callback);
};

但是,在添加资源标志之前,有两种选择.我们可以直接使用curl命令(或Node.js中的请求模块)使用REST API,如 Nakilon 在其回答如下(文档)-

However, until they add the resource flag, there're two options to do it. We can either use the REST API directly using curl command (or request module in Node.js) as mentioned by Nakilon in their answer as follows (Documentation) -

instace_id = curl http://metadata.google.internal/computeMetadata/v1/instance/id -H "Metadata-Flavor: Google"
zone = curl http://metadata.google.internal/computeMetadata/v1/instance/zone -H "Metadata-Flavor: Google"
instance_name = curl http://metadata.google.internal/computeMetadata/v1/instance/name -H "Metadata-Flavor: Google"

或者我们可以使用NPM包 gcp-metadata 轻松获取数据使用node.js.在内部,Stackdriver node.js客户端也使用相同的程序包.这是我使用gcp-metadata

Or we can use the NPM package gcp-metadata to get the data easily using node.js. Internally, the Stackdriver node.js client also uses the same package. Here's how I got the instance name using gcp-metadata

const gcpMetadata = require('gcp-metadata');
const res = await gcpMetadata.instance('name');
const instance_name = res.data;

实例的可用属性为-

attributes/
cpu-platform
description
disks/
hostname
id
image
licenses/
machine-type
maintenance-event
name
network-interfaces/
preempted
remaining-cpu-time
scheduling/
service-accounts/
tags
virtual-clock/
zone

请查看文档以获取所有可用元数据的解释属性.

Please check the documentation for explanation of all the metadata available properties.

请记住,curl方法和gcp-metadata包在计算引擎实例中运行时都可以使用.因此它不会在本地计算机上运行.

Just to keep in mind that both the curl method and the gcp-metadata package will work when running in a compute engine instance. So it won't run in the local machine.

在所有即将发布的版本中解决Github问题后,我都会更新答案.

I will update the answer, as soon as the Github issue is resolved in any of the upcoming versions.

这篇关于在Stackdriver记录中标记正确的GCE实例名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆