如何从k6向Datadog Vector发送日志消息

huangapple go评论80阅读模式
英文:

How to send a log message to Datadog Vector from k6

问题

我们有一些带有矢量数据源的Datadog Vector应用程序。
我们想要使用k6进行断点和负载测试的性能测试。
然而,k6中没有现有的插件适用于这种用例。

数据源的实现是一个GRPC端点,因此应该可以使用k6的GRPC客户端来实现这一目的。

如果有任何已经这样做的人愿意分享他们的示例,我将不胜感激。

英文:

We have some Datadog Vector applications with vector datasources.
We would like to run some performance tests using k6 for breakpoint and load tests.
However there there is not existing plugin in k6 for this use case.

The datasource implementation is a GRPC endpoint, therefore it should be possible to use k6 GRPC client for this purpose.

I would really appreciate if anybody who did this would share their example.

答案1

得分: 0

我们需要从Datadog Vector的Git仓库中获取两个protobuf定义:

https://github.com/vectordotdev/vector/blob/master/lib/vector-core/proto/event.proto

https://github.com/vectordotdev/vector/blob/master/proto/vector.proto

将它们放入以下文件夹结构中:

./definitions/vector.proto
./definitions/vector.proto
./k6_test.js

k6测试的内容如下:

import {check, sleep} from 'k6';
import grpc from 'k6/net/grpc';
import encoding from 'k6/encoding';

const client = new grpc.Client();
client.load(['definitions'], 'event.proto', 'vector.proto');

export default () => {
  client.connect('vector-logs:6000', {
    plaintext: true
  });

  const data = {
    events: [
      {
        log: {
          fields: {
            field1: {integer: 111},
            field2: {boolean: true}
          },
          value: {
            map: {
              fields: {
                message: {raw_bytes: encoding.b64encode("message from k6")}
              }
            }
          },
          metadata: {
            map: {
              fields: {
                key111: {raw_bytes: encoding.b64encode("value1")},
                key2: {raw_bytes: encoding.b64encode("value2")}
              }
            }
          }
        }
      }
    ]
  };

  const response = client.invoke('vector.Vector/PushEvents', data);

  check(response, {
    'status is OK': (r) => r && r.status === grpc.StatusOK
  });

  console.log(JSON.stringify(response.message));

  client.close();
  sleep(1);
};

我创建了一个包含整数、布尔、字符串和映射内容类型的消息示例。如果您需要其他类型,请参考:https://protobuf.dev/programming-guides/proto3/#json

PS:我对Rust、k6和protobuf几乎没有经验,因此请将其视为示例。

英文:

We would need 2 proto buf definitions from Datadog vector git repo:
https://github.com/vectordotdev/vector/blob/master/lib/vector-core/proto/event.proto
https://github.com/vectordotdev/vector/blob/master/proto/vector.proto

Put them into following folder structure:

./definitions/vector.proto
./definitions/vector.proto
./k6_test.js

Contents for k6 test

import {check, sleep} from 'k6';
import grpc from 'k6/net/grpc';
import encoding from 'k6/encoding';

const client = new grpc.Client();
client.load(['definitions'], 'event.proto', 'vector.proto');

export default () => {
client.connect('vector-logs:6000', {
    plaintext: true
});

const data = {
    events: [
        {log: {
                fields: {
                    field1: {integer: 111},
                    field2: {boolean: true},
                },
                value: {
                    map: {
                        fields: {
                            message: {raw_bytes: encoding.b64encode("message from k6")}
                        }
                    }

                },
                metadata: {
                    map: {
                        fields: {
                            key111: {
                                raw_bytes: encoding.b64encode("value1")
                            },
                            key2: {
                                raw_bytes: encoding.b64encode("value2")
                            }
                        }
                    }
                }
            }
        }
    ]
}
const response = client.invoke('vector.Vector/PushEvents', data);

check(response, {
    'status is OK': (r) => r && r.status === grpc.StatusOK,
});

console.log(JSON.stringify(response.message));

client.close();
sleep(1);
};

I've create example for messages with int, boolean, string and map content types.
In case if you need any other types, you might need to consult with: https://protobuf.dev/programming-guides/proto3/#json

PS: I have almost zero experience with rust, k6 and protobuf therefore treat it just as an example.

huangapple
  • 本文由 发表于 2023年7月31日 18:37:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76802799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定