From b6c617deee5152de519cf6971e8b9660bc2fd0e1 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 29 Apr 2021 16:45:58 +0800 Subject: [PATCH] feat: code generator of time series data --- .gitignore | 1 + README.md | 67 ++- go.mod | 9 + go.sum | 104 ++++ protoc-gen-metric/main.go | 7 + protoc-gen-metric/metric.go | 75 +++ protoc-gen-metric/tmpl.go | 57 ++ testdata/metric/test.pb.go | 955 ++++++++++++++++++++++++++++++++++ testdata/metric/test.pb.mt.go | 115 ++++ testdata/proto/test.proto | 49 ++ 10 files changed, 1438 insertions(+), 1 deletion(-) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 protoc-gen-metric/main.go create mode 100644 protoc-gen-metric/metric.go create mode 100644 protoc-gen-metric/tmpl.go create mode 100644 testdata/metric/test.pb.go create mode 100644 testdata/metric/test.pb.mt.go create mode 100644 testdata/proto/test.proto diff --git a/.gitignore b/.gitignore index 66fd13c..0ca2a00 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.dll *.so *.dylib +.idea # Test binary, built with `go test -c` *.test diff --git a/README.md b/README.md index fb6a67e..fc0beb7 100644 --- a/README.md +++ b/README.md @@ -1 +1,66 @@ -# hunter \ No newline at end of file +# hunter + +> influx时序数据写客户端 + + 问题:直接使用influx client或API写数据时,没有统一定义格式规范 + 解决:用protobuf定义格式规范,protoc生成套路代码 + +#### 一、格式规范 + * 时序数据表名、标签名和字段名均为下划线风格; + * schema的定义三层嵌套,顶层为类别,中层为表,底层为标签或值 + 举例kubernetes数据: + message kubernetes { + message node { + message tag { + string node_name = 1; + } + message field { + int64 cpu_usage_core_nanoseconds = 1; + int64 memory_available_bytes = 2; + } + } + message pod_network { + message tag { + string namespace = 1; + string node_name = 2; + string pod_name = 3; + } + message field { + int64 rx_bytes = 1; + int64 rx_errors = 2; + } + } + message pod_volume { + message tag { + string namespace = 1; + string node_name = 2; + string pod_name = 3; + string volume_name = 4; + } + message field { + int64 available_bytes = 1; + int64 capacity_bytes = 2; + int64 used_bytes = 3; + } + } + message pod_container { + message tag { + string node_name = 1; + string container_name = 2; + } + message field { + int64 cpu_usage_core_nanoseconds = 1; + int64 memory_usage_bytes = 2; + } + } + } + + #### 二、代码生成 + + 安装: + * go install github.com/LilithGames/hunter/protoc-gen-metric + * go install github.com/golang/protobuf/protoc-gen-go + 使用: + 1. 编写指定时序数据的protobuf文件 + 2. 使用protoc-gen-go生成代码,如protoc -I . --go_out=. ./testdata/proto/* + 3. 使用protoc-gen-metric生成代码,如protoc -I . --metric_out=. ./testdata/proto/* \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d1b23eb --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/LilithGames/hunter + +go 1.15 + +require ( + github.com/influxdata/influxdb-client-go v1.4.0 // indirect + github.com/influxdata/influxdb-client-go/v2 v2.2.3 // indirect + github.com/lyft/protoc-gen-star v0.5.2 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..53d9035 --- /dev/null +++ b/go.sum @@ -0,0 +1,104 @@ +github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/deepmap/oapi-codegen v1.3.6 h1:Wj44p9A0V0PJ+AUg0BWdyGcsS1LY18U+0rCuPQgK0+o= +github.com/deepmap/oapi-codegen v1.3.6/go.mod h1:aBozjEveG+33xPiP55Iw/XbVkhtZHEGLq3nxlX0+hfU= +github.com/deepmap/oapi-codegen v1.3.13 h1:9HKGCsdJqE4dnrQ8VerFS0/1ZOJPmAhN+g8xgp8y3K4= +github.com/deepmap/oapi-codegen v1.3.13/go.mod h1:WAmG5dWY8/PYHt4vKxlt90NsbHMAOCiteYKZMiIRfOo= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/getkin/kin-openapi v0.2.0/go.mod h1:V1z9xl9oF5Wt7v32ne4FmiF1alpS4dM6mNzoywPOXlk= +github.com/getkin/kin-openapi v0.13.0/go.mod h1:WGRs2ZMM1Q8LR1QBEwUxC6RJEfaBcD0s+pcEVXFuAjw= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219/go.mod h1:/X8TswGSh1pIozq4ZwCfxS0WA5JGXguxk94ar/4c87Y= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/influxdata/influxdb-client-go v1.4.0 h1:+KavOkwhLClHFfYcJMHHnTL5CZQhXJzOm5IKHI9BqJk= +github.com/influxdata/influxdb-client-go v1.4.0/go.mod h1:S+oZsPivqbcP1S9ur+T+QqXvrYS3NCZeMQtBoH4D1dw= +github.com/influxdata/influxdb-client-go/v2 v2.2.3 h1:082jdJ5t1CFeo0rpGQvKAK1mONVSbFhL4finWA5bRM8= +github.com/influxdata/influxdb-client-go/v2 v2.2.3/go.mod h1:fa/d1lAdUHxuc1jedx30ZfNG573oQTQmUni3N6pcW+0= +github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 h1:W9WBk7wlPfJLvMCdtV4zPulc4uCPrlywQOmbFOhgQNU= +github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo= +github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/labstack/echo/v4 v4.1.11 h1:z0BZoArY4FqdpUEl+wlHp4hnr/oSR6MTmQmv8OHSoww= +github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g= +github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0= +github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= +github.com/lyft/protoc-gen-star v0.5.2 h1:ICQPpOr4uO46eme1Y5Jj0fnJkc9/upQ9xxt0+2AmUDQ= +github.com/lyft/protoc-gen-star v0.5.2/go.mod h1:9toiA3cC7z5uVbODF7kEQ91Xn7XNFkVUl+SrEe+ZORU= +github.com/matryer/moq v0.0.0-20190312154309-6cfb0558e1bd/go.mod h1:9ELz6aaclSIGnZBoaSLZ3NAl1VTufbOrXBPvtcy6WiQ= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= +github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= +github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10= +github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/spf13/afero v1.3.3 h1:p5gZEKLYoL7wh8VrJesMaYeNxdEd1v3cb4irOk9zB54= +github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= +github.com/valyala/fasttemplate v1.1.0 h1:RZqt0yGBsps8NGvLSGW804QQqCUYYLsaOjTVHy1Ocw4= +github.com/valyala/fasttemplate v1.1.0/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708 h1:pXVtWnwHkrWD9ru3sDxY/qFK/bfc0egRovX91EjWjf4= +golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20191112182307-2180aed22343 h1:00ohfJ4K98s3m6BGUoBd8nyfp4Yl0GoIKvw5abItTjI= +golang.org/x/net v0.0.0-20191112182307-2180aed22343/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191115151921-52ab43148777 h1:wejkGHRTr38uaKRqECZlsCsJ1/TGxIyFbH32x5zUdu4= +golang.org/x/sys v0.0.0-20191115151921-52ab43148777/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.5 h1:ymVxjfMaHvXD8RqPRmzHHsB3VvucivSkIAvJFDI5O3c= +gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/protoc-gen-metric/main.go b/protoc-gen-metric/main.go new file mode 100644 index 0000000..d717e15 --- /dev/null +++ b/protoc-gen-metric/main.go @@ -0,0 +1,7 @@ +package main + +import pgs "github.com/lyft/protoc-gen-star" + +func main() { + pgs.Init(pgs.DebugEnv("DEBUG")).RegisterModule(NewMetric()).Render() +} diff --git a/protoc-gen-metric/metric.go b/protoc-gen-metric/metric.go new file mode 100644 index 0000000..187782b --- /dev/null +++ b/protoc-gen-metric/metric.go @@ -0,0 +1,75 @@ +package main + +import ( + "errors" + pgs "github.com/lyft/protoc-gen-star" + pgsgo "github.com/lyft/protoc-gen-star/lang/go" + "text/template" +) + +type metricModule struct { + *pgs.ModuleBase + ctx pgsgo.Context + tpl *template.Template +} + +func NewMetric() pgs.Module { + return &metricModule{ModuleBase: &pgs.ModuleBase{}} +} + +func (m *metricModule) Name() string { + return "metric" +} + +func (m *metricModule) InitContext(c pgs.BuildContext) { + m.ModuleBase.InitContext(c) + m.ctx = pgsgo.InitContext(c.Parameters()) + tpl := template.New("metric").Funcs(map[string]interface{}{ + "package": m.ctx.PackageName, + "name": m.ctx.Name, + "selectSubMsg": m.selectSubMsg, + "lowerCamelCase": m.lowerCamelCase, + "lowerSnakeCase": m.lowerSnakeCase, + "upperCamelCase": m.upperCamelCase, + "upperSnakeCase": m.upperSnakeCase, + }) + m.tpl = template.Must(tpl.Parse(metricTemplate)) +} + +func (m *metricModule) Execute(targets map[string]pgs.File, packages map[string]pgs.Package) []pgs.Artifact { + for _, f := range targets { + if len(f.Messages()) == 0 { + continue + } + name := m.ctx.OutputPath(f).SetExt(".mt.go") + m.AddGeneratorTemplateFile(name.String(), m.tpl, f) + } + return m.Artifacts() +} + +func (m *metricModule) lowerCamelCase(entity pgs.Entity) string { + return entity.Name().LowerCamelCase().String() +} + +func (m *metricModule) lowerSnakeCase(entity pgs.Entity) string { + return entity.Name().LowerSnakeCase().String() +} + +func (m *metricModule) upperCamelCase(entity pgs.Entity) string { + return entity.Name().UpperCamelCase().String() +} + +func (m *metricModule) upperSnakeCase(entity pgs.Entity) string { + return entity.Name().UpperSnakeCase().String() +} + +func (m *metricModule) selectSubMsg(msg pgs.Message, name string) ([]pgs.Field, error) { + for _, m := range msg.Messages() { + if m.Name().LowerCamelCase().String() == name { + return m.Fields(), nil + } + } + return nil, errors.New("not found message") +} + + diff --git a/protoc-gen-metric/tmpl.go b/protoc-gen-metric/tmpl.go new file mode 100644 index 0000000..e1d17ac --- /dev/null +++ b/protoc-gen-metric/tmpl.go @@ -0,0 +1,57 @@ +package main + +const metricTemplate = `// Code generated by protoc-gen-metric. DO NOT EDIT. + +package {{ package . }} + +import ( + "errors" + influxdb "github.com/influxdata/influxdb-client-go/v2" + "github.com/influxdata/influxdb-client-go/v2/api" + "time" +) +{{ range .Messages }}{{- $project := name . }}{{ $lowerCamelProject := lowerCamelCase . }}{{ $lowerSnakeProject := lowerSnakeCase . }} +type {{ $project }}Writer interface { + Flush() + Errors() <- chan error + {{- range .Messages -}}{{ $table := upperCamelCase . }} + Write{{ $table }}Point(tag *{{ $project }}{{ $table }}Tag, field *{{ $project }}{{ $table }}Field) error + Write{{ $table }}PointWithTime(tag *{{ $project }}{{ $table }}Tag, field *{{ $project }}{{ $table }}Field, timestamp time.Time) error + {{- end }} +} + +type {{ lowerCamelCase . }}Writer struct { + api.WriteAPI +} + +func New{{ $project }}Writer(endpoint string) {{ $project }}Writer { + return &{{ lowerCamelCase . }}Writer{WriteAPI: influxdb.NewClient(endpoint, "").WriteAPI("", "")} +} + +func (w *{{ lowerCamelCase . }}Writer) Flush() { + w.WriteAPI.Flush() +} + +func (w *{{ lowerCamelCase . }}Writer) Errors() <-chan error { + return w.WriteAPI.Errors() +} +{{ range .Messages }}{{ $table := upperCamelCase . }}{{ $lowerSnakeTable := lowerSnakeCase . }} +func (w *{{ $lowerCamelProject }}Writer) Write{{ $table }}Point(tag *{{ $project }}{{ $table }}Tag, field *{{ $project }}{{ $table }}Field) error { + return w.Write{{ $table }}PointWithTime(tag, field, time.Now()) +} + +func (w *{{ $lowerCamelProject }}Writer) Write{{ $table }}PointWithTime(tag *{{ $project }}{{ $table }}Tag, field *{{ $project }}{{ $table }}Field, timestamp time.Time) error { + if tag == nil || field == nil { + return errors.New("nil point error") + } + point := influxdb.NewPointWithMeasurement("{{ $lowerSnakeProject }}_{{ $lowerSnakeTable }}") + {{ range ( selectSubMsg . "tag" )}} + point.AddTag("{{ lowerSnakeCase . }}", tag.{{ upperCamelCase . }}){{ end }} + {{ range ( selectSubMsg . "field")}} + point.AddField("{{ lowerSnakeCase . }}", field.{{ upperCamelCase . }}){{ end }} + w.WriteAPI.WritePoint(point) + return nil +} +{{- end -}} +{{ end }} +` \ No newline at end of file diff --git a/testdata/metric/test.pb.go b/testdata/metric/test.pb.go new file mode 100644 index 0000000..3f7750c --- /dev/null +++ b/testdata/metric/test.pb.go @@ -0,0 +1,955 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.13.0 +// source: testdata/proto/test.proto + +package metric + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Kubernetes struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *Kubernetes) Reset() { + *x = Kubernetes{} + if protoimpl.UnsafeEnabled { + mi := &file_testdata_proto_test_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Kubernetes) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Kubernetes) ProtoMessage() {} + +func (x *Kubernetes) ProtoReflect() protoreflect.Message { + mi := &file_testdata_proto_test_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Kubernetes.ProtoReflect.Descriptor instead. +func (*Kubernetes) Descriptor() ([]byte, []int) { + return file_testdata_proto_test_proto_rawDescGZIP(), []int{0} +} + +type KubernetesNode struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *KubernetesNode) Reset() { + *x = KubernetesNode{} + if protoimpl.UnsafeEnabled { + mi := &file_testdata_proto_test_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KubernetesNode) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KubernetesNode) ProtoMessage() {} + +func (x *KubernetesNode) ProtoReflect() protoreflect.Message { + mi := &file_testdata_proto_test_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use KubernetesNode.ProtoReflect.Descriptor instead. +func (*KubernetesNode) Descriptor() ([]byte, []int) { + return file_testdata_proto_test_proto_rawDescGZIP(), []int{0, 0} +} + +type KubernetesPodNetwork struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *KubernetesPodNetwork) Reset() { + *x = KubernetesPodNetwork{} + if protoimpl.UnsafeEnabled { + mi := &file_testdata_proto_test_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KubernetesPodNetwork) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KubernetesPodNetwork) ProtoMessage() {} + +func (x *KubernetesPodNetwork) ProtoReflect() protoreflect.Message { + mi := &file_testdata_proto_test_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use KubernetesPodNetwork.ProtoReflect.Descriptor instead. +func (*KubernetesPodNetwork) Descriptor() ([]byte, []int) { + return file_testdata_proto_test_proto_rawDescGZIP(), []int{0, 1} +} + +type KubernetesPodVolume struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *KubernetesPodVolume) Reset() { + *x = KubernetesPodVolume{} + if protoimpl.UnsafeEnabled { + mi := &file_testdata_proto_test_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KubernetesPodVolume) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KubernetesPodVolume) ProtoMessage() {} + +func (x *KubernetesPodVolume) ProtoReflect() protoreflect.Message { + mi := &file_testdata_proto_test_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use KubernetesPodVolume.ProtoReflect.Descriptor instead. +func (*KubernetesPodVolume) Descriptor() ([]byte, []int) { + return file_testdata_proto_test_proto_rawDescGZIP(), []int{0, 2} +} + +type KubernetesPodContainer struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields +} + +func (x *KubernetesPodContainer) Reset() { + *x = KubernetesPodContainer{} + if protoimpl.UnsafeEnabled { + mi := &file_testdata_proto_test_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KubernetesPodContainer) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KubernetesPodContainer) ProtoMessage() {} + +func (x *KubernetesPodContainer) ProtoReflect() protoreflect.Message { + mi := &file_testdata_proto_test_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use KubernetesPodContainer.ProtoReflect.Descriptor instead. +func (*KubernetesPodContainer) Descriptor() ([]byte, []int) { + return file_testdata_proto_test_proto_rawDescGZIP(), []int{0, 3} +} + +type KubernetesNodeTag struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + NodeName string `protobuf:"bytes,1,opt,name=node_name,json=nodeName,proto3" json:"node_name,omitempty"` +} + +func (x *KubernetesNodeTag) Reset() { + *x = KubernetesNodeTag{} + if protoimpl.UnsafeEnabled { + mi := &file_testdata_proto_test_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KubernetesNodeTag) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KubernetesNodeTag) ProtoMessage() {} + +func (x *KubernetesNodeTag) ProtoReflect() protoreflect.Message { + mi := &file_testdata_proto_test_proto_msgTypes[5] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use KubernetesNodeTag.ProtoReflect.Descriptor instead. +func (*KubernetesNodeTag) Descriptor() ([]byte, []int) { + return file_testdata_proto_test_proto_rawDescGZIP(), []int{0, 0, 0} +} + +func (x *KubernetesNodeTag) GetNodeName() string { + if x != nil { + return x.NodeName + } + return "" +} + +type KubernetesNodeField struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + CpuUsageCoreNanoseconds int64 `protobuf:"varint,1,opt,name=cpu_usage_core_nanoseconds,json=cpuUsageCoreNanoseconds,proto3" json:"cpu_usage_core_nanoseconds,omitempty"` + MemoryAvailableBytes int64 `protobuf:"varint,2,opt,name=memory_available_bytes,json=memoryAvailableBytes,proto3" json:"memory_available_bytes,omitempty"` +} + +func (x *KubernetesNodeField) Reset() { + *x = KubernetesNodeField{} + if protoimpl.UnsafeEnabled { + mi := &file_testdata_proto_test_proto_msgTypes[6] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KubernetesNodeField) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KubernetesNodeField) ProtoMessage() {} + +func (x *KubernetesNodeField) ProtoReflect() protoreflect.Message { + mi := &file_testdata_proto_test_proto_msgTypes[6] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use KubernetesNodeField.ProtoReflect.Descriptor instead. +func (*KubernetesNodeField) Descriptor() ([]byte, []int) { + return file_testdata_proto_test_proto_rawDescGZIP(), []int{0, 0, 1} +} + +func (x *KubernetesNodeField) GetCpuUsageCoreNanoseconds() int64 { + if x != nil { + return x.CpuUsageCoreNanoseconds + } + return 0 +} + +func (x *KubernetesNodeField) GetMemoryAvailableBytes() int64 { + if x != nil { + return x.MemoryAvailableBytes + } + return 0 +} + +type KubernetesPodNetworkTag struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Namespace string `protobuf:"bytes,1,opt,name=namespace,proto3" json:"namespace,omitempty"` + NodeName string `protobuf:"bytes,2,opt,name=node_name,json=nodeName,proto3" json:"node_name,omitempty"` + PodName string `protobuf:"bytes,3,opt,name=pod_name,json=podName,proto3" json:"pod_name,omitempty"` +} + +func (x *KubernetesPodNetworkTag) Reset() { + *x = KubernetesPodNetworkTag{} + if protoimpl.UnsafeEnabled { + mi := &file_testdata_proto_test_proto_msgTypes[7] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KubernetesPodNetworkTag) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KubernetesPodNetworkTag) ProtoMessage() {} + +func (x *KubernetesPodNetworkTag) ProtoReflect() protoreflect.Message { + mi := &file_testdata_proto_test_proto_msgTypes[7] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use KubernetesPodNetworkTag.ProtoReflect.Descriptor instead. +func (*KubernetesPodNetworkTag) Descriptor() ([]byte, []int) { + return file_testdata_proto_test_proto_rawDescGZIP(), []int{0, 1, 0} +} + +func (x *KubernetesPodNetworkTag) GetNamespace() string { + if x != nil { + return x.Namespace + } + return "" +} + +func (x *KubernetesPodNetworkTag) GetNodeName() string { + if x != nil { + return x.NodeName + } + return "" +} + +func (x *KubernetesPodNetworkTag) GetPodName() string { + if x != nil { + return x.PodName + } + return "" +} + +type KubernetesPodNetworkField struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + RxBytes int64 `protobuf:"varint,1,opt,name=rx_bytes,json=rxBytes,proto3" json:"rx_bytes,omitempty"` + RxErrors int64 `protobuf:"varint,2,opt,name=rx_errors,json=rxErrors,proto3" json:"rx_errors,omitempty"` +} + +func (x *KubernetesPodNetworkField) Reset() { + *x = KubernetesPodNetworkField{} + if protoimpl.UnsafeEnabled { + mi := &file_testdata_proto_test_proto_msgTypes[8] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KubernetesPodNetworkField) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KubernetesPodNetworkField) ProtoMessage() {} + +func (x *KubernetesPodNetworkField) ProtoReflect() protoreflect.Message { + mi := &file_testdata_proto_test_proto_msgTypes[8] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use KubernetesPodNetworkField.ProtoReflect.Descriptor instead. +func (*KubernetesPodNetworkField) Descriptor() ([]byte, []int) { + return file_testdata_proto_test_proto_rawDescGZIP(), []int{0, 1, 1} +} + +func (x *KubernetesPodNetworkField) GetRxBytes() int64 { + if x != nil { + return x.RxBytes + } + return 0 +} + +func (x *KubernetesPodNetworkField) GetRxErrors() int64 { + if x != nil { + return x.RxErrors + } + return 0 +} + +type KubernetesPodVolumeTag struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Namespace string `protobuf:"bytes,1,opt,name=namespace,proto3" json:"namespace,omitempty"` + NodeName string `protobuf:"bytes,2,opt,name=node_name,json=nodeName,proto3" json:"node_name,omitempty"` + PodName string `protobuf:"bytes,3,opt,name=pod_name,json=podName,proto3" json:"pod_name,omitempty"` + VolumeName string `protobuf:"bytes,4,opt,name=volume_name,json=volumeName,proto3" json:"volume_name,omitempty"` +} + +func (x *KubernetesPodVolumeTag) Reset() { + *x = KubernetesPodVolumeTag{} + if protoimpl.UnsafeEnabled { + mi := &file_testdata_proto_test_proto_msgTypes[9] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KubernetesPodVolumeTag) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KubernetesPodVolumeTag) ProtoMessage() {} + +func (x *KubernetesPodVolumeTag) ProtoReflect() protoreflect.Message { + mi := &file_testdata_proto_test_proto_msgTypes[9] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use KubernetesPodVolumeTag.ProtoReflect.Descriptor instead. +func (*KubernetesPodVolumeTag) Descriptor() ([]byte, []int) { + return file_testdata_proto_test_proto_rawDescGZIP(), []int{0, 2, 0} +} + +func (x *KubernetesPodVolumeTag) GetNamespace() string { + if x != nil { + return x.Namespace + } + return "" +} + +func (x *KubernetesPodVolumeTag) GetNodeName() string { + if x != nil { + return x.NodeName + } + return "" +} + +func (x *KubernetesPodVolumeTag) GetPodName() string { + if x != nil { + return x.PodName + } + return "" +} + +func (x *KubernetesPodVolumeTag) GetVolumeName() string { + if x != nil { + return x.VolumeName + } + return "" +} + +type KubernetesPodVolumeField struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + AvailableBytes int64 `protobuf:"varint,1,opt,name=available_bytes,json=availableBytes,proto3" json:"available_bytes,omitempty"` + CapacityBytes int64 `protobuf:"varint,2,opt,name=capacity_bytes,json=capacityBytes,proto3" json:"capacity_bytes,omitempty"` + UsedBytes int64 `protobuf:"varint,3,opt,name=used_bytes,json=usedBytes,proto3" json:"used_bytes,omitempty"` +} + +func (x *KubernetesPodVolumeField) Reset() { + *x = KubernetesPodVolumeField{} + if protoimpl.UnsafeEnabled { + mi := &file_testdata_proto_test_proto_msgTypes[10] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KubernetesPodVolumeField) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KubernetesPodVolumeField) ProtoMessage() {} + +func (x *KubernetesPodVolumeField) ProtoReflect() protoreflect.Message { + mi := &file_testdata_proto_test_proto_msgTypes[10] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use KubernetesPodVolumeField.ProtoReflect.Descriptor instead. +func (*KubernetesPodVolumeField) Descriptor() ([]byte, []int) { + return file_testdata_proto_test_proto_rawDescGZIP(), []int{0, 2, 1} +} + +func (x *KubernetesPodVolumeField) GetAvailableBytes() int64 { + if x != nil { + return x.AvailableBytes + } + return 0 +} + +func (x *KubernetesPodVolumeField) GetCapacityBytes() int64 { + if x != nil { + return x.CapacityBytes + } + return 0 +} + +func (x *KubernetesPodVolumeField) GetUsedBytes() int64 { + if x != nil { + return x.UsedBytes + } + return 0 +} + +type KubernetesPodContainerTag struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + NodeName string `protobuf:"bytes,1,opt,name=node_name,json=nodeName,proto3" json:"node_name,omitempty"` + ContainerName string `protobuf:"bytes,2,opt,name=container_name,json=containerName,proto3" json:"container_name,omitempty"` +} + +func (x *KubernetesPodContainerTag) Reset() { + *x = KubernetesPodContainerTag{} + if protoimpl.UnsafeEnabled { + mi := &file_testdata_proto_test_proto_msgTypes[11] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KubernetesPodContainerTag) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KubernetesPodContainerTag) ProtoMessage() {} + +func (x *KubernetesPodContainerTag) ProtoReflect() protoreflect.Message { + mi := &file_testdata_proto_test_proto_msgTypes[11] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use KubernetesPodContainerTag.ProtoReflect.Descriptor instead. +func (*KubernetesPodContainerTag) Descriptor() ([]byte, []int) { + return file_testdata_proto_test_proto_rawDescGZIP(), []int{0, 3, 0} +} + +func (x *KubernetesPodContainerTag) GetNodeName() string { + if x != nil { + return x.NodeName + } + return "" +} + +func (x *KubernetesPodContainerTag) GetContainerName() string { + if x != nil { + return x.ContainerName + } + return "" +} + +type KubernetesPodContainerField struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + CpuUsageCoreNanoseconds int64 `protobuf:"varint,1,opt,name=cpu_usage_core_nanoseconds,json=cpuUsageCoreNanoseconds,proto3" json:"cpu_usage_core_nanoseconds,omitempty"` + MemoryUsageBytes int64 `protobuf:"varint,2,opt,name=memory_usage_bytes,json=memoryUsageBytes,proto3" json:"memory_usage_bytes,omitempty"` +} + +func (x *KubernetesPodContainerField) Reset() { + *x = KubernetesPodContainerField{} + if protoimpl.UnsafeEnabled { + mi := &file_testdata_proto_test_proto_msgTypes[12] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *KubernetesPodContainerField) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*KubernetesPodContainerField) ProtoMessage() {} + +func (x *KubernetesPodContainerField) ProtoReflect() protoreflect.Message { + mi := &file_testdata_proto_test_proto_msgTypes[12] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use KubernetesPodContainerField.ProtoReflect.Descriptor instead. +func (*KubernetesPodContainerField) Descriptor() ([]byte, []int) { + return file_testdata_proto_test_proto_rawDescGZIP(), []int{0, 3, 1} +} + +func (x *KubernetesPodContainerField) GetCpuUsageCoreNanoseconds() int64 { + if x != nil { + return x.CpuUsageCoreNanoseconds + } + return 0 +} + +func (x *KubernetesPodContainerField) GetMemoryUsageBytes() int64 { + if x != nil { + return x.MemoryUsageBytes + } + return 0 +} + +var File_testdata_proto_test_proto protoreflect.FileDescriptor + +var file_testdata_proto_test_proto_rawDesc = []byte{ + 0x0a, 0x19, 0x74, 0x65, 0x73, 0x74, 0x64, 0x61, 0x74, 0x61, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xb9, 0x06, 0x0a, 0x0a, + 0x6b, 0x75, 0x62, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x65, 0x73, 0x1a, 0xa6, 0x01, 0x0a, 0x04, 0x6e, + 0x6f, 0x64, 0x65, 0x1a, 0x22, 0x0a, 0x03, 0x74, 0x61, 0x67, 0x12, 0x1b, 0x0a, 0x09, 0x6e, 0x6f, + 0x64, 0x65, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6e, + 0x6f, 0x64, 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x1a, 0x7a, 0x0a, 0x05, 0x66, 0x69, 0x65, 0x6c, 0x64, + 0x12, 0x3b, 0x0a, 0x1a, 0x63, 0x70, 0x75, 0x5f, 0x75, 0x73, 0x61, 0x67, 0x65, 0x5f, 0x63, 0x6f, + 0x72, 0x65, 0x5f, 0x6e, 0x61, 0x6e, 0x6f, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x73, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x03, 0x52, 0x17, 0x63, 0x70, 0x75, 0x55, 0x73, 0x61, 0x67, 0x65, 0x43, 0x6f, + 0x72, 0x65, 0x4e, 0x61, 0x6e, 0x6f, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x73, 0x12, 0x34, 0x0a, + 0x16, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x5f, 0x61, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, + 0x65, 0x5f, 0x62, 0x79, 0x74, 0x65, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x14, 0x6d, + 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x41, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x42, 0x79, + 0x74, 0x65, 0x73, 0x1a, 0xab, 0x01, 0x0a, 0x0b, 0x70, 0x6f, 0x64, 0x5f, 0x6e, 0x65, 0x74, 0x77, + 0x6f, 0x72, 0x6b, 0x1a, 0x5b, 0x0a, 0x03, 0x74, 0x61, 0x67, 0x12, 0x1c, 0x0a, 0x09, 0x6e, 0x61, + 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6e, + 0x61, 0x6d, 0x65, 0x73, 0x70, 0x61, 0x63, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x6e, 0x6f, 0x64, 0x65, + 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6e, 0x6f, 0x64, + 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x70, 0x6f, 0x64, 0x5f, 0x6e, 0x61, 0x6d, + 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x6f, 0x64, 0x4e, 0x61, 0x6d, 0x65, + 0x1a, 0x3f, 0x0a, 0x05, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x12, 0x19, 0x0a, 0x08, 0x72, 0x78, 0x5f, + 0x62, 0x79, 0x74, 0x65, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x07, 0x72, 0x78, 0x42, + 0x79, 0x74, 0x65, 0x73, 0x12, 0x1b, 0x0a, 0x09, 0x72, 0x78, 0x5f, 0x65, 0x72, 0x72, 0x6f, 0x72, + 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x72, 0x78, 0x45, 0x72, 0x72, 0x6f, 0x72, + 0x73, 0x1a, 0x82, 0x02, 0x0a, 0x0a, 0x70, 0x6f, 0x64, 0x5f, 0x76, 0x6f, 0x6c, 0x75, 0x6d, 0x65, + 0x1a, 0x7c, 0x0a, 0x03, 0x74, 0x61, 0x67, 0x12, 0x1c, 0x0a, 0x09, 0x6e, 0x61, 0x6d, 0x65, 0x73, + 0x70, 0x61, 0x63, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6e, 0x61, 0x6d, 0x65, + 0x73, 0x70, 0x61, 0x63, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x6e, 0x6f, 0x64, 0x65, 0x5f, 0x6e, 0x61, + 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6e, 0x6f, 0x64, 0x65, 0x4e, 0x61, + 0x6d, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x70, 0x6f, 0x64, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x6f, 0x64, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x1f, 0x0a, + 0x0b, 0x76, 0x6f, 0x6c, 0x75, 0x6d, 0x65, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x04, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x0a, 0x76, 0x6f, 0x6c, 0x75, 0x6d, 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x1a, 0x76, + 0x0a, 0x05, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x12, 0x27, 0x0a, 0x0f, 0x61, 0x76, 0x61, 0x69, 0x6c, + 0x61, 0x62, 0x6c, 0x65, 0x5f, 0x62, 0x79, 0x74, 0x65, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, + 0x52, 0x0e, 0x61, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x42, 0x79, 0x74, 0x65, 0x73, + 0x12, 0x25, 0x0a, 0x0e, 0x63, 0x61, 0x70, 0x61, 0x63, 0x69, 0x74, 0x79, 0x5f, 0x62, 0x79, 0x74, + 0x65, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0d, 0x63, 0x61, 0x70, 0x61, 0x63, 0x69, + 0x74, 0x79, 0x42, 0x79, 0x74, 0x65, 0x73, 0x12, 0x1d, 0x0a, 0x0a, 0x75, 0x73, 0x65, 0x64, 0x5f, + 0x62, 0x79, 0x74, 0x65, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x75, 0x73, 0x65, + 0x64, 0x42, 0x79, 0x74, 0x65, 0x73, 0x1a, 0xce, 0x01, 0x0a, 0x0d, 0x70, 0x6f, 0x64, 0x5f, 0x63, + 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x1a, 0x49, 0x0a, 0x03, 0x74, 0x61, 0x67, 0x12, + 0x1b, 0x0a, 0x09, 0x6e, 0x6f, 0x64, 0x65, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x08, 0x6e, 0x6f, 0x64, 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x25, 0x0a, 0x0e, + 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x4e, + 0x61, 0x6d, 0x65, 0x1a, 0x72, 0x0a, 0x05, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x12, 0x3b, 0x0a, 0x1a, + 0x63, 0x70, 0x75, 0x5f, 0x75, 0x73, 0x61, 0x67, 0x65, 0x5f, 0x63, 0x6f, 0x72, 0x65, 0x5f, 0x6e, + 0x61, 0x6e, 0x6f, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, + 0x52, 0x17, 0x63, 0x70, 0x75, 0x55, 0x73, 0x61, 0x67, 0x65, 0x43, 0x6f, 0x72, 0x65, 0x4e, 0x61, + 0x6e, 0x6f, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x73, 0x12, 0x2c, 0x0a, 0x12, 0x6d, 0x65, 0x6d, + 0x6f, 0x72, 0x79, 0x5f, 0x75, 0x73, 0x61, 0x67, 0x65, 0x5f, 0x62, 0x79, 0x74, 0x65, 0x73, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x10, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x55, 0x73, 0x61, + 0x67, 0x65, 0x42, 0x79, 0x74, 0x65, 0x73, 0x42, 0x12, 0x5a, 0x10, 0x2f, 0x74, 0x65, 0x73, 0x74, + 0x64, 0x61, 0x74, 0x61, 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, +} + +var ( + file_testdata_proto_test_proto_rawDescOnce sync.Once + file_testdata_proto_test_proto_rawDescData = file_testdata_proto_test_proto_rawDesc +) + +func file_testdata_proto_test_proto_rawDescGZIP() []byte { + file_testdata_proto_test_proto_rawDescOnce.Do(func() { + file_testdata_proto_test_proto_rawDescData = protoimpl.X.CompressGZIP(file_testdata_proto_test_proto_rawDescData) + }) + return file_testdata_proto_test_proto_rawDescData +} + +var file_testdata_proto_test_proto_msgTypes = make([]protoimpl.MessageInfo, 13) +var file_testdata_proto_test_proto_goTypes = []interface{}{ + (*Kubernetes)(nil), // 0: kubernetes + (*KubernetesNode)(nil), // 1: kubernetes.node + (*KubernetesPodNetwork)(nil), // 2: kubernetes.pod_network + (*KubernetesPodVolume)(nil), // 3: kubernetes.pod_volume + (*KubernetesPodContainer)(nil), // 4: kubernetes.pod_container + (*KubernetesNodeTag)(nil), // 5: kubernetes.node.tag + (*KubernetesNodeField)(nil), // 6: kubernetes.node.field + (*KubernetesPodNetworkTag)(nil), // 7: kubernetes.pod_network.tag + (*KubernetesPodNetworkField)(nil), // 8: kubernetes.pod_network.field + (*KubernetesPodVolumeTag)(nil), // 9: kubernetes.pod_volume.tag + (*KubernetesPodVolumeField)(nil), // 10: kubernetes.pod_volume.field + (*KubernetesPodContainerTag)(nil), // 11: kubernetes.pod_container.tag + (*KubernetesPodContainerField)(nil), // 12: kubernetes.pod_container.field +} +var file_testdata_proto_test_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_testdata_proto_test_proto_init() } +func file_testdata_proto_test_proto_init() { + if File_testdata_proto_test_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_testdata_proto_test_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Kubernetes); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_testdata_proto_test_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KubernetesNode); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_testdata_proto_test_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KubernetesPodNetwork); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_testdata_proto_test_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KubernetesPodVolume); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_testdata_proto_test_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KubernetesPodContainer); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_testdata_proto_test_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KubernetesNodeTag); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_testdata_proto_test_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KubernetesNodeField); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_testdata_proto_test_proto_msgTypes[7].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KubernetesPodNetworkTag); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_testdata_proto_test_proto_msgTypes[8].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KubernetesPodNetworkField); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_testdata_proto_test_proto_msgTypes[9].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KubernetesPodVolumeTag); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_testdata_proto_test_proto_msgTypes[10].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KubernetesPodVolumeField); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_testdata_proto_test_proto_msgTypes[11].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KubernetesPodContainerTag); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_testdata_proto_test_proto_msgTypes[12].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*KubernetesPodContainerField); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_testdata_proto_test_proto_rawDesc, + NumEnums: 0, + NumMessages: 13, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_testdata_proto_test_proto_goTypes, + DependencyIndexes: file_testdata_proto_test_proto_depIdxs, + MessageInfos: file_testdata_proto_test_proto_msgTypes, + }.Build() + File_testdata_proto_test_proto = out.File + file_testdata_proto_test_proto_rawDesc = nil + file_testdata_proto_test_proto_goTypes = nil + file_testdata_proto_test_proto_depIdxs = nil +} diff --git a/testdata/metric/test.pb.mt.go b/testdata/metric/test.pb.mt.go new file mode 100644 index 0000000..aa6836f --- /dev/null +++ b/testdata/metric/test.pb.mt.go @@ -0,0 +1,115 @@ +// Code generated by protoc-gen-metric. DO NOT EDIT. + +package metric + +import ( + "errors" + influxdb "github.com/influxdata/influxdb-client-go/v2" + "github.com/influxdata/influxdb-client-go/v2/api" + "time" +) + +type KubernetesWriter interface { + Flush() + Errors() <- chan error + WriteNodePoint(tag *KubernetesNodeTag, field *KubernetesNodeField) error + WriteNodePointWithTime(tag *KubernetesNodeTag, field *KubernetesNodeField, timestamp time.Time) error + WritePodNetworkPoint(tag *KubernetesPodNetworkTag, field *KubernetesPodNetworkField) error + WritePodNetworkPointWithTime(tag *KubernetesPodNetworkTag, field *KubernetesPodNetworkField, timestamp time.Time) error + WritePodVolumePoint(tag *KubernetesPodVolumeTag, field *KubernetesPodVolumeField) error + WritePodVolumePointWithTime(tag *KubernetesPodVolumeTag, field *KubernetesPodVolumeField, timestamp time.Time) error + WritePodContainerPoint(tag *KubernetesPodContainerTag, field *KubernetesPodContainerField) error + WritePodContainerPointWithTime(tag *KubernetesPodContainerTag, field *KubernetesPodContainerField, timestamp time.Time) error +} + +type kubernetesWriter struct { + api.WriteAPI +} + +func NewKubernetesWriter(endpoint string) KubernetesWriter { + return &kubernetesWriter{WriteAPI: influxdb.NewClient(endpoint, "").WriteAPI("", "")} +} + +func (w *kubernetesWriter) Flush() { + w.WriteAPI.Flush() +} + +func (w *kubernetesWriter) Errors() <-chan error { + return w.WriteAPI.Errors() +} + +func (w *kubernetesWriter) WriteNodePoint(tag *KubernetesNodeTag, field *KubernetesNodeField) error { + return w.WriteNodePointWithTime(tag, field, time.Now()) +} + +func (w *kubernetesWriter) WriteNodePointWithTime(tag *KubernetesNodeTag, field *KubernetesNodeField, timestamp time.Time) error { + if tag == nil || field == nil { + return errors.New("nil point error") + } + point := influxdb.NewPointWithMeasurement("kubernetes_node") + + point.AddTag("node_name", tag.NodeName) + + point.AddField("cpu_usage_core_nanoseconds", field.CpuUsageCoreNanoseconds) + point.AddField("memory_available_bytes", field.MemoryAvailableBytes) + w.WriteAPI.WritePoint(point) + return nil +} +func (w *kubernetesWriter) WritePodNetworkPoint(tag *KubernetesPodNetworkTag, field *KubernetesPodNetworkField) error { + return w.WritePodNetworkPointWithTime(tag, field, time.Now()) +} + +func (w *kubernetesWriter) WritePodNetworkPointWithTime(tag *KubernetesPodNetworkTag, field *KubernetesPodNetworkField, timestamp time.Time) error { + if tag == nil || field == nil { + return errors.New("nil point error") + } + point := influxdb.NewPointWithMeasurement("kubernetes_pod_network") + + point.AddTag("namespace", tag.Namespace) + point.AddTag("node_name", tag.NodeName) + point.AddTag("pod_name", tag.PodName) + + point.AddField("rx_bytes", field.RxBytes) + point.AddField("rx_errors", field.RxErrors) + w.WriteAPI.WritePoint(point) + return nil +} +func (w *kubernetesWriter) WritePodVolumePoint(tag *KubernetesPodVolumeTag, field *KubernetesPodVolumeField) error { + return w.WritePodVolumePointWithTime(tag, field, time.Now()) +} + +func (w *kubernetesWriter) WritePodVolumePointWithTime(tag *KubernetesPodVolumeTag, field *KubernetesPodVolumeField, timestamp time.Time) error { + if tag == nil || field == nil { + return errors.New("nil point error") + } + point := influxdb.NewPointWithMeasurement("kubernetes_pod_volume") + + point.AddTag("namespace", tag.Namespace) + point.AddTag("node_name", tag.NodeName) + point.AddTag("pod_name", tag.PodName) + point.AddTag("volume_name", tag.VolumeName) + + point.AddField("available_bytes", field.AvailableBytes) + point.AddField("capacity_bytes", field.CapacityBytes) + point.AddField("used_bytes", field.UsedBytes) + w.WriteAPI.WritePoint(point) + return nil +} +func (w *kubernetesWriter) WritePodContainerPoint(tag *KubernetesPodContainerTag, field *KubernetesPodContainerField) error { + return w.WritePodContainerPointWithTime(tag, field, time.Now()) +} + +func (w *kubernetesWriter) WritePodContainerPointWithTime(tag *KubernetesPodContainerTag, field *KubernetesPodContainerField, timestamp time.Time) error { + if tag == nil || field == nil { + return errors.New("nil point error") + } + point := influxdb.NewPointWithMeasurement("kubernetes_pod_container") + + point.AddTag("node_name", tag.NodeName) + point.AddTag("container_name", tag.ContainerName) + + point.AddField("cpu_usage_core_nanoseconds", field.CpuUsageCoreNanoseconds) + point.AddField("memory_usage_bytes", field.MemoryUsageBytes) + w.WriteAPI.WritePoint(point) + return nil +} diff --git a/testdata/proto/test.proto b/testdata/proto/test.proto new file mode 100644 index 0000000..d969d5b --- /dev/null +++ b/testdata/proto/test.proto @@ -0,0 +1,49 @@ +syntax = "proto3"; + +option go_package = "/testdata/metric"; + +message kubernetes { + message node { + message tag { + string node_name = 1; + } + message field { + int64 cpu_usage_core_nanoseconds = 1; + int64 memory_available_bytes = 2; + } + } + message pod_network { + message tag { + string namespace = 1; + string node_name = 2; + string pod_name = 3; + } + message field { + int64 rx_bytes = 1; + int64 rx_errors = 2; + } + } + message pod_volume { + message tag { + string namespace = 1; + string node_name = 2; + string pod_name = 3; + string volume_name = 4; + } + message field { + int64 available_bytes = 1; + int64 capacity_bytes = 2; + int64 used_bytes = 3; + } + } + message pod_container { + message tag { + string node_name = 1; + string container_name = 2; + } + message field { + int64 cpu_usage_core_nanoseconds = 1; + int64 memory_usage_bytes = 2; + } + } +} \ No newline at end of file