将 CloudWatch 日志发送到数据库

发布于 2025-01-16 19:06:30 字数 846 浏览 3 评论 0原文

我正在尝试将 Cloudwatch 日志保存在本地 Postgres 数据库中。我目前正在将日志导出到 S3 并保存在 DynamoDB 中。我现在的要求是使用节点和 AWS js-SDK 将其保留在我们的数据库中。我对 Node 和 js-SDK 不是很了解,所以我会非常感激任何想法。

我尝试了一个简单的实现。

const pools = require('../src/common/db'),
const AWS = require('aws-sdk');

// set the cwl
let cwl = new AWS.CloudWatchLogs({
  region: 'us-east-1',
  accessKeyId: 'ABCD1234',
  secretAccessKey: 'MNBV76543',
  Bucket: 'My_bucket'
});

// Get the events
cwl.getLogEvents({
  logGroupName: 'OurLogGroupname',
  logStreamName: 'specifiedLogstream'
}, (error, success ) =>{
  if(error){
    console.log(error)
  }
  console.log(success)
})

// Try saving to db
let sql = ''
pools.query_db('abc', 'INSERT INTO logging.aws_logs(request_id, duration, billed_duration) VALUES (?,?,?)', function(err, res){
  if(err) return callback(err);
  callback();
})

I am attempting to save our Cloudwatch logs in an on-premise Postgres database. I'm currently exporting logs to S3 and save in DynamoDB. My requirement now is to persist it in our DB, using node and AWS js-SDK. I'm not very strong on node and js-SDK, so I'll greatly appreciate any idea.

I tried a simple implementation.

const pools = require('../src/common/db'),
const AWS = require('aws-sdk');

// set the cwl
let cwl = new AWS.CloudWatchLogs({
  region: 'us-east-1',
  accessKeyId: 'ABCD1234',
  secretAccessKey: 'MNBV76543',
  Bucket: 'My_bucket'
});

// Get the events
cwl.getLogEvents({
  logGroupName: 'OurLogGroupname',
  logStreamName: 'specifiedLogstream'
}, (error, success ) =>{
  if(error){
    console.log(error)
  }
  console.log(success)
})

// Try saving to db
let sql = ''
pools.query_db('abc', 'INSERT INTO logging.aws_logs(request_id, duration, billed_duration) VALUES (?,?,?)', function(err, res){
  if(err) return callback(err);
  callback();
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灰色世界里的红玫瑰 2025-01-23 19:06:30

如果您确实想将 Cloudwatch 中的所有消息存储到数据库中,我更喜欢以下方式:

  • 向您的 Cloudwatch LogGroup 添加订阅

  • 此订阅可配置为触发 Lambda

  • Lambda 将具有以下逻辑:

    1. 从事件变量中提取消息
    2. 准备 SQL 语句
    3. 连接到数据库(如果无法连接,请重试)
    4. 执行SQL语句(如果不可能则重试)
    5. 完成

一个关于如何提取事件消息的好示例Cloudwatch 订阅调用将把这些日志发送到 Opensearch(搜索蓝图)

I would prefer the following way, if you really want to store all messages from Cloudwatch into a database:

  • Add a subscription to your Cloudwatch LogGroup

  • This subscription can be configured to trigger a Lambda

  • The Lambda will have the following logic:

    1. extract the message from the event variable
    2. prepare your SQL statement
    3. connect to database (retry if not possible)
    4. execute the SQL statement (retry if not possible)
    5. done

One good example on how to extract the message of a Cloudwatch Subscription invocation would the one for sending those logs to Opensearch (search the blueprints)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文