From e7ef911bfad43d4c1997fa71358f220f0edf7388 Mon Sep 17 00:00:00 2001 From: Daria Kharlan Date: Tue, 10 Aug 2021 22:03:27 +0300 Subject: [PATCH] Send pipeline info to BC page (#25) Send pipeline info to BC page Co-authored-by: dariakharlan --- e2e/docker-compose.yaml | 1 + go.mod | 4 +- go.sum | 38 +- main.go | 18 + pkg/prometheus/bc.go | 45 ++ .../anodot/anodot-common/pkg/metrics3/bc.go | 27 + .../anodot-common/pkg/metrics3/client.go | 490 ++++++++++++++++++ .../anodot-common/pkg/metrics3/metrics.go | 97 ++++ .../anodot-common/pkg/metrics3/schema.go | 49 ++ vendor/modules.txt | 3 +- 10 files changed, 733 insertions(+), 39 deletions(-) create mode 100644 pkg/prometheus/bc.go create mode 100644 vendor/github.com/anodot/anodot-common/pkg/metrics3/bc.go create mode 100644 vendor/github.com/anodot/anodot-common/pkg/metrics3/client.go create mode 100644 vendor/github.com/anodot/anodot-common/pkg/metrics3/metrics.go create mode 100644 vendor/github.com/anodot/anodot-common/pkg/metrics3/schema.go diff --git a/e2e/docker-compose.yaml b/e2e/docker-compose.yaml index 5e5b567..5a9cd6d 100644 --- a/e2e/docker-compose.yaml +++ b/e2e/docker-compose.yaml @@ -36,6 +36,7 @@ services: ANODOT_METRICS_PER_REQUEST_SIZE: 4 ANODOT_LOG_LEVEL: "5" ANODOT_API_TOKEN: "123456" + ANODOT_ACCESS_KEY: "123456" ANODOT_RELABEL_CONFIG_PATH: "/mnt/relabel.yml" ANODOT_HTTP_DEBUG_ENABLED: "true" ANODOT_PUSH_METRICS_ENABLED: "true" diff --git a/go.mod b/go.mod index 87bd2c2..79bc8c3 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,10 @@ module github.com/anodot/anodot-remote-write -go 1.12 +go 1.13 require ( github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 // indirect - github.com/anodot/anodot-common v0.0.8 + github.com/anodot/anodot-common v0.0.10 github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.2 github.com/golang/snappy v0.0.3 diff --git a/go.sum b/go.sum index e9a02a3..eb30c4e 100644 --- a/go.sum +++ b/go.sum @@ -10,13 +10,12 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= -github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 h1:Hs82Z41s6SdL1CELW+XaDYmOH4hkBN4/N9og/AsOv7E= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 h1:AUNCr9CiJuwrRYS3XieqF+Z9B9gNxo/eANAJCF2eiN4= github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE= -github.com/anodot/anodot-common v0.0.8 h1:F42jnYLVOqoTXy5tHfcljqTWuP9tCjaFs6NQ01WPOPA= -github.com/anodot/anodot-common v0.0.8/go.mod h1:za8DDPMVdFhrwPdR8y1N4prNnKjEWddJe/CM136iUuU= +github.com/anodot/anodot-common v0.0.10 h1:VbNOCrgevBH360LHRKbmbcish/mwpOqNo8YDvJkGEgM= +github.com/anodot/anodot-common v0.0.10/go.mod h1:za8DDPMVdFhrwPdR8y1N4prNnKjEWddJe/CM136iUuU= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= @@ -28,7 +27,6 @@ github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQ github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= -github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= @@ -69,20 +67,16 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4= github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2rbfLwlschooIH4+wKKDR4Pdxhh+TRoA20= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= -github.com/go-kit/kit v0.9.0 h1:wDJmvq38kDhkVxi50ni9ykkdUr1PKgqKOoi01fa0Mdk= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.10.0/go.mod h1:xUsJbQ/Fp4kEt7AFgCuvyX4a71u8h9jB8tj/ORgOZ7o= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= -github.com/go-logfmt/logfmt v0.4.0 h1:MP4Eh7ZCb31lleYCFuwm0oe4/YGak+5l1vA2NOE80nA= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc= github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= -github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -90,14 +84,12 @@ github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= @@ -118,14 +110,12 @@ github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Z github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg= 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/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -143,7 +133,6 @@ github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFb github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoPndWW5VkKPlCE= github.com/hashicorp/consul/sdk v0.3.0/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= -github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM= github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= @@ -186,10 +175,8 @@ github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj2vyii2bbUNDw3kt9VxK2EY= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= @@ -214,10 +201,8 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= @@ -250,7 +235,6 @@ github.com/performancecopilot/speed v3.0.0+incompatible/go.mod h1:/CLtqpZ5gBg1M9 github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0jef7pBehbT1qWhCMrIgbYNnFAZCqQ5LRc= github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= 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= @@ -260,18 +244,14 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs= -github.com/prometheus/client_golang v1.0.0 h1:vrDKnkGzuGvhNAL56c7DBz29ZL+KxnoR0x7enabFceM= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= -github.com/prometheus/client_golang v1.3.0 h1:miYCvYqFXtl/J9FIy8eNpBfYthAEFg+Ys0XyUVEcDsc= github.com/prometheus/client_golang v1.3.0/go.mod h1:hJaj2vgQTGQmVCsAACORcieXFeDPbaTKGT+JTgUa3og= github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= github.com/prometheus/client_golang v1.10.0 h1:/o0BDeWzLWXNZ+4q5gXltUvaMpJqckTa+jTNoB+z4cg= github.com/prometheus/client_golang v1.10.0/go.mod h1:WJM3cc3yu7XKBKa/I8WeZm+V3eltZnBwfENSU7mdogU= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= -github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.1.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M= @@ -285,9 +265,7 @@ github.com/prometheus/common v0.24.0 h1:aIycr3wRFxPUq8XlLQlGQ9aNXV3dFi5y62pe/SB2 github.com/prometheus/common v0.24.0/go.mod h1:H6QK/N6XVT42whUeIdI3dp36w49c+/iMDk7UAI2qm7Q= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= -github.com/prometheus/procfs v0.0.2 h1:6LJUbpNm42llc4HRCuvApCSWB/WfhuNo9K98Q9sNGfs= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= -github.com/prometheus/procfs v0.0.8 h1:+fpWZdT24pJBiqJdAwYBjPSk+5YmQzYNPYzQsdzLkt8= github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A= github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4= @@ -304,7 +282,6 @@ github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0 github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= @@ -321,7 +298,6 @@ github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5J github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= 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 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= @@ -375,7 +351,6 @@ golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= -golang.org/x/net v0.0.0-20190613194153-d28f0bde5980 h1:dfGZHvZk057jK2MCeWus/TowKpJ8y4AmooUzdBSR9GU= golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -394,7 +369,6 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -406,10 +380,8 @@ golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/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-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -431,9 +403,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 h1:yhBbb4IRs2HS9PPlAg6DMC6mUOKexJBNsLf4Z+6En1Q= golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -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/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -483,11 +453,9 @@ google.golang.org/grpc v1.20.0/go.mod h1:chYK+tFQF0nDUGJgXMSgLCQk3phJEuONr2DCgLD google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= google.golang.org/grpc v1.22.1/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= -google.golang.org/grpc v1.23.0 h1:AzbTB6ux+okLTzP8Ru1Xs41C303zdcfEht7MQnYJt5A= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.23.1/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= -google.golang.org/grpc v1.26.0 h1:2dTRdpdFEEhJYQD8EMLB61nnrzSCTbG38PhqdhvOltg= google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= @@ -508,7 +476,6 @@ google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/l google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 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/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= @@ -524,7 +491,6 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 4077f46..52b8e8b 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "github.com/anodot/anodot-common/pkg/metrics3" "net/http" _ "net/http/pprof" "net/url" @@ -195,6 +196,23 @@ func main() { reporter.Report() } + ifSendToBC := defaultIfBlank(os.Getenv("ANODOT_SEND_TO_BC"), "true") + sendToBCPeriod, err := strconv.Atoi(defaultIfBlank(os.Getenv("ANODOT_SEND_TO_BC_PERIOD_SEC"), "60")) + if err != nil { + log.Fatalf("Could not parse ANODOT_SEND_TO_BC_PERIOD_SEC: %v", err) + } + if ifSendToBC != "false" { + accessKey := os.Getenv("ANODOT_ACCESS_KEY") + if len(strings.TrimSpace(accessKey)) == 0 { + log.Fatalf("ANODOT_ACCESS_KEY is not specified") + } + client, err := metrics3.NewAnodot30Client(*primaryUrl, &accessKey, &token, nil) + if err != nil { + log.Fatalf("failed to create anodot30 client: %v", err) + } + anodotPrometheus.SendAgentStatusToBC(client, sendToBCPeriod) + } + s.InitHttp(allWorkers) } diff --git a/pkg/prometheus/bc.go b/pkg/prometheus/bc.go new file mode 100644 index 0000000..51b172f --- /dev/null +++ b/pkg/prometheus/bc.go @@ -0,0 +1,45 @@ +package prometheus + +import ( + "github.com/anodot/anodot-common/pkg/metrics3" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" + log "k8s.io/klog/v2" + "time" +) + +func NewPipeline(startTime metrics3.AnodotTimestamp) metrics3.Pipeline { + pipeline := metrics3.Pipeline{} + pipeline.Id = "prometheus" + pipeline.Created = startTime + pipeline.Updated = startTime + pipeline.Status = "RUNNING" + + s := metrics3.Source{} + s.Name = "prometheus" + s.Type = "prometheus" + pipeline.Source = s + + return pipeline +} + +var sendStatusToBCErrors = promauto.NewCounter(prometheus.CounterOpts{ + Name: "anodot_send_status_to_bc_errors_total", + Help: "Total number of errors occurred while sending status to BC", +}) + +func SendAgentStatusToBC(client *metrics3.Anodot30Client, sendToBCPeriod int) { + startTime := metrics3.AnodotTimestamp{Time: time.Now()} + go func() { + ticker := time.NewTicker(time.Duration(sendToBCPeriod) * time.Second) + defer ticker.Stop() + + for range ticker.C { + _, err := client.SendToBC(NewPipeline(startTime)) + if err != nil { + sendStatusToBCErrors.Inc() + log.Errorf("Failed to send status to BC %v", err) + } + } + }() +} diff --git a/vendor/github.com/anodot/anodot-common/pkg/metrics3/bc.go b/vendor/github.com/anodot/anodot-common/pkg/metrics3/bc.go new file mode 100644 index 0000000..bf6877a --- /dev/null +++ b/vendor/github.com/anodot/anodot-common/pkg/metrics3/bc.go @@ -0,0 +1,27 @@ +package metrics3 + +type Source struct { + Name string `json:"name"` + Type string `json:"type"` +} + +type Scheduling struct { + Interval string `json:"interval"` + Delay string `json:"delay"` +} + +type Progress struct { + LastOffset string `json:"last_offset"` +} + +type Pipeline struct { + Id string `json:"pipeline_id"` + Created AnodotTimestamp `json:"created"` + Updated AnodotTimestamp `json:"updated"` + Status string `json:"status"` + SchemaId string `json:"schemaId"` + Source `json:"source"` + Scheduling `json:"scheduling"` + Progress `json:"progress"` + AnodotMetricsSchema `json:"schema"` +} diff --git a/vendor/github.com/anodot/anodot-common/pkg/metrics3/client.go b/vendor/github.com/anodot/anodot-common/pkg/metrics3/client.go new file mode 100644 index 0000000..eda880a --- /dev/null +++ b/vendor/github.com/anodot/anodot-common/pkg/metrics3/client.go @@ -0,0 +1,490 @@ +package metrics3 + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "net/http/httputil" + "net/url" + "os" + "strconv" + "time" +) + +type AnodotResponse interface { + HasErrors() bool + ErrorMessage() string + RawResponse() *http.Response +} + +type Api30Response struct { + Error *struct { + Status int `json:"status"` + Name string `json:"name"` + Message string `json:"message"` + AndtErrorCode int `json:"andtErrorCode"` + Path string `json:"path"` + } + HttpResponse *http.Response `json:"-"` +} + +func (r *Api30Response) HasErrors() bool { + return r.Error != nil +} + +func (r *Api30Response) ErrorMessage() string { + return fmt.Sprintf("%+v\n", r.Error) +} + +func (r *Api30Response) RawResponse() *http.Response { + return r.HttpResponse +} + +type refreshBearerResponse struct { + refreshTime time.Time + bearer string + Api30Response +} + +type Anodot30Client struct { + ServerURL *url.URL + AccessKey *string + DataCollectionToken *string + client *http.Client + bearerToken *struct { + timestemp time.Time + token string + } +} + +func NewAnodot30Client(anodotURL url.URL, accessKey *string, dataToken *string, httpClient *http.Client) (*Anodot30Client, error) { + if accessKey == nil && dataToken == nil { + return nil, fmt.Errorf("anodot token can't be nil") + } + + submitter := Anodot30Client{AccessKey: accessKey, DataCollectionToken: dataToken, ServerURL: &anodotURL, client: httpClient, bearerToken: nil} + if httpClient == nil { + client := http.Client{Timeout: 30 * time.Second} + + debugHTTP, _ := strconv.ParseBool(os.Getenv("ANODOT_HTTP_DEBUG_ENABLED")) + if debugHTTP { + client.Transport = &debugHTTPTransport{r: http.DefaultTransport} + } + submitter.client = &client + } + + return &submitter, nil +} + +func (c *Anodot30Client) GetBearerToken() (*string, error) { + // Token valid 24 hours, so if BearerToken field is null or token expired + // needs to refresh it, otherwise, returns existed token + + if c.bearerToken == nil || time.Since(c.bearerToken.timestemp) > 24*time.Hour { + resp, err := c.refreshBearerToken() + if err != nil { + return nil, err + } + + if resp.HasErrors() { + return nil, fmt.Errorf("failed to refresh toke: %v", resp.ErrorMessage()) + } + + c.bearerToken = &struct { + timestemp time.Time + token string + }{resp.refreshTime, resp.bearer} + + } + return &c.bearerToken.token, nil +} + +func (c *Anodot30Client) refreshBearerToken() (*refreshBearerResponse, error) { + + if c.AccessKey == nil { + return nil, fmt.Errorf("please provide AccesKey for obtain bearer token") + } + sUrl := *c.ServerURL + sUrl.Path = "api/v2/access-token" + + q := sUrl.Query() + q.Set("responseformat", "JSON") + + sUrl.RawQuery = q.Encode() + + b, _ := json.Marshal( + struct { + RefreshToken string `json:"refreshToken"` + }{ + *c.AccessKey, + }, + ) + + r, _ := http.NewRequest(http.MethodPost, sUrl.String(), bytes.NewBuffer(b)) + r.Header.Add("Content-Type", "application/json") + + resp, err := c.client.Do(r) + if err != nil { + return nil, err + } + bodyBytes, _ := ioutil.ReadAll(resp.Body) + + refreshResponse := refreshBearerResponse{} + refreshResponse.HttpResponse = resp + + if resp.StatusCode/100 != 2 { + err = json.Unmarshal(bodyBytes, &refreshResponse.Error) + if err != nil { + return &refreshResponse, + fmt.Errorf("failed to parse reponse body: %v \n%s", err, string(bodyBytes)) + } + return &refreshResponse, nil + } + + responseJson := struct{ Token string }{} + + err = json.Unmarshal(bodyBytes, &responseJson) + if err != nil { + return &refreshResponse, + fmt.Errorf("failed to parse reponse body: %v \n%s", err, string(bodyBytes)) + } + + refreshResponse.bearer = responseJson.Token + refreshResponse.refreshTime = time.Now() + return &refreshResponse, nil +} + +func (c *Anodot30Client) SubmitMetrics(metrics []AnodotMetrics30) (*SubmitMetricsResponse, error) { + if c.DataCollectionToken == nil { + return nil, + fmt.Errorf("DataCollectionToken should be provided for metrics submit ") + } + + sUrl := *c.ServerURL + sUrl.Path = "api/v1/metrics" + + q := sUrl.Query() + q.Set("token", *c.DataCollectionToken) + q.Set("protocol", "anodot30") + sUrl.RawQuery = q.Encode() + + b, err := json.Marshal(metrics) + if err != nil { + return nil, fmt.Errorf("Failed to parse schema:" + err.Error()) + } + r, _ := http.NewRequest(http.MethodPost, sUrl.String(), bytes.NewBuffer(b)) + r.Header.Add("Content-Type", "application/json") + + resp, err := c.client.Do(r) + if err != nil { + return nil, err + } + anodotResponse := &SubmitMetricsResponse{} + anodotResponse.HttpResponse = resp + + if resp.Body == nil { + return anodotResponse, fmt.Errorf("empty response body") + } + + bodyBytes, _ := ioutil.ReadAll(resp.Body) + if resp.StatusCode/100 != 2 { + err = json.Unmarshal(bodyBytes, &anodotResponse) + if err != nil { + return anodotResponse, fmt.Errorf("http response is differ from 2xx\nfalied to parse response body: %v \n%s", err, string(bodyBytes)) + } + } + + err = json.Unmarshal(bodyBytes, anodotResponse) + if err != nil { + return anodotResponse, + fmt.Errorf("failed to parse reponse body: %v \n%s", err, string(bodyBytes)) + } + return anodotResponse, nil +} + +func (c *Anodot30Client) CreateSchema(schema AnodotMetricsSchema) (*CreateSchemaResponse, error) { + token, err := c.GetBearerToken() + if err != nil { + return nil, err + } + + var bearer = "Bearer " + *token + sUrl := c.ServerURL + sUrl.Path = "/api/v2/stream-schemas" + + b, e := json.Marshal(schema) + if e != nil { + return nil, + fmt.Errorf("Failed to parse schema:" + e.Error()) + } + + r, _ := http.NewRequest(http.MethodPost, sUrl.String(), bytes.NewBuffer(b)) + + r.Header.Set("Authorization", bearer) + r.Header.Add("Content-Type", "application/json") + + resp, err := c.client.Do(r) + if err != nil { + return nil, err + } + + anodotResponse := &CreateSchemaResponse{} + anodotResponse.HttpResponse = resp + + if resp.Body == nil { + return anodotResponse, fmt.Errorf("empty response body") + } + + bodyBytes, _ := ioutil.ReadAll(resp.Body) + + if resp.StatusCode/100 != 2 { + err = json.Unmarshal(bodyBytes, &anodotResponse.Error) + if err != nil { + return anodotResponse, + fmt.Errorf("failed to parse reponse body: %v \n%s", err, string(bodyBytes)) + } + return anodotResponse, nil + } + + schemaCreated := struct { + Schema AnodotMetricsSchema `json:"schema"` + }{ + Schema: AnodotMetricsSchema{}, + } + + err = json.Unmarshal(bodyBytes, &schemaCreated) + if err != nil { + return anodotResponse, err + } + + anodotResponse.SchemaId = &schemaCreated.Schema.Id + return anodotResponse, nil +} + +func (c *Anodot30Client) DeleteSchema(schemaId string) (*DeleteSchemaResponse, error) { + token, err := c.GetBearerToken() + if err != nil { + return nil, err + } + + var bearer = "Bearer " + *token + sUrl := c.ServerURL + sUrl.Path = "api/v2/stream-schemas/" + schemaId + + r, _ := http.NewRequest(http.MethodDelete, sUrl.String(), nil) + + r.Header.Set("Authorization", bearer) + r.Header.Add("Content-Type", "application/json") + + resp, err := c.client.Do(r) + if err != nil { + return nil, err + } + + anodotResponse := &DeleteSchemaResponse{} + anodotResponse.HttpResponse = resp + + if resp.Body == nil { + return anodotResponse, fmt.Errorf("empty response body") + } + + bodyBytes, _ := ioutil.ReadAll(resp.Body) + + if resp.StatusCode/100 != 2 { + err = json.Unmarshal(bodyBytes, &anodotResponse.Error) + fmt.Println(anodotResponse.Error) + if err != nil { + return anodotResponse, + fmt.Errorf("failed to parse reponse body: %v \n%s", err, string(bodyBytes)) + } + return anodotResponse, nil + } + + schemaDeleted := struct { + Deleted string `json:"deleted"` + }{} + + err = json.Unmarshal(bodyBytes, &schemaDeleted) + if err != nil { + return anodotResponse, err + } + + anodotResponse.SchemaId = &schemaDeleted.Deleted + return anodotResponse, nil +} + +func (c *Anodot30Client) GetSchemas() (*GetSchemaResponse, error) { + + token, err := c.GetBearerToken() + if err != nil { + return nil, err + } + + var bearer = "Bearer " + *token + + sUrl := c.ServerURL + sUrl.Path = "/api/v2/stream-schemas/schemas" + + r, _ := http.NewRequest(http.MethodGet, sUrl.String(), nil) + + r.Header.Set("Authorization", bearer) + + resp, err := c.client.Do(r) + if err != nil { + return nil, err + } + + anodotResponse := &GetSchemaResponse{} + anodotResponse.HttpResponse = resp + + if resp.Body == nil { + return anodotResponse, fmt.Errorf("empty response body") + } + + bodyBytes, _ := ioutil.ReadAll(resp.Body) + + if resp.Body == nil { + return nil, fmt.Errorf("empty response body") + } + + if resp.StatusCode/100 != 2 { + err = json.Unmarshal(bodyBytes, &anodotResponse.Error) + if err != nil { + return nil, fmt.Errorf("failed to parse reponse body: %v \n%s", err, string(bodyBytes)) + } + return anodotResponse, nil + } + + schemasTmp := make([]StreamSchemaWrapper, 0) + schemas := make([]AnodotMetricsSchema, 0) + + err = json.Unmarshal(bodyBytes, &schemasTmp) + if err != nil { + return anodotResponse, err + } + + for _, s := range schemasTmp { + schemas = append(schemas, s.Wrapper.Schema) + } + + anodotResponse.Schemas = schemas + + return anodotResponse, nil +} + +func (c *Anodot30Client) SubmitWatermark(schemaId string, watermark AnodotTimestamp) (*SubmitWatermarkResponse, error) { + if c.DataCollectionToken == nil { + return nil, + fmt.Errorf("DataCollectionToken should be provided for watermark submit ") + } + + sUrl := *c.ServerURL + sUrl.Path = "api/v1/metrics/watermark" + + q := sUrl.Query() + q.Set("token", *c.DataCollectionToken) + q.Set("protocol", "anodot30") + sUrl.RawQuery = q.Encode() + + b, _ := json.Marshal( + struct { + SchemaId string `json:"schemaId"` + Watermark AnodotTimestamp `json:"watermark"` + }{schemaId, watermark}, + ) + r, _ := http.NewRequest(http.MethodPost, sUrl.String(), bytes.NewBuffer(b)) + r.Header.Add("Content-Type", "application/json") + + resp, err := c.client.Do(r) + if err != nil { + return nil, err + } + anodotResponse := SubmitWatermarkResponse{} + anodotResponse.HttpResponse = resp + + if resp.Body == nil { + return &anodotResponse, fmt.Errorf("empty response body") + } + + bodyBytes, _ := ioutil.ReadAll(resp.Body) + + if resp.StatusCode/100 != 2 { + err = json.Unmarshal(bodyBytes, &anodotResponse) + if err != nil { + return &anodotResponse, fmt.Errorf("http response is differ from 2xx\nfalied to parse response body: %v \n%s", err, string(bodyBytes)) + } + } + + err = json.Unmarshal(bodyBytes, &anodotResponse) + if err != nil { + return &anodotResponse, + fmt.Errorf("failed to parse reponse body: %v \n%s", err, string(bodyBytes)) + } + + return &anodotResponse, nil +} + +func (c *Anodot30Client) SendToBC(bcData Pipeline) (*Api30Response, error) { + token, err := c.GetBearerToken() + if err != nil { + return nil, err + } + + var bearer = "Bearer " + *token + sUrl := *c.ServerURL + sUrl.Path = "api/v2/bc/agents" + + b, e := json.Marshal(bcData) + if e != nil { + return nil, fmt.Errorf("Failed to parse bc data:" + e.Error()) + } + + r, _ := http.NewRequest(http.MethodPost, sUrl.String(), bytes.NewBuffer(b)) + + r.Header.Set("Authorization", bearer) + r.Header.Add("Content-Type", "application/json") + + resp, err := c.client.Do(r) + if err != nil { + return nil, err + } + + anodotResponse := &Api30Response{} + anodotResponse.HttpResponse = resp + + if resp.Body == nil { + return anodotResponse, fmt.Errorf("empty response body") + } + + bodyBytes, _ := ioutil.ReadAll(resp.Body) + + if resp.StatusCode/100 != 2 { + err = json.Unmarshal(bodyBytes, &anodotResponse.Error) + if err != nil { + return anodotResponse, fmt.Errorf("failed to parse reponse body: %v \n%s", err, string(bodyBytes)) + } + return anodotResponse, nil + } + + return anodotResponse, nil +} + +type debugHTTPTransport struct { + r http.RoundTripper +} + +func (d *debugHTTPTransport) RoundTrip(h *http.Request) (*http.Response, error) { + dump, _ := httputil.DumpRequestOut(h, true) + fmt.Printf("----------------------------------REQUEST----------------------------------\n%s\n", string(dump)) + resp, err := d.r.RoundTrip(h) + if err != nil { + fmt.Println("failed to obtain response: ", err.Error()) + return resp, err + } + + dump, _ = httputil.DumpResponse(resp, true) + fmt.Printf("----------------------------------RESPONSE----------------------------------\n%s\n----------------------------------\n\n", string(dump)) + return resp, err +} diff --git a/vendor/github.com/anodot/anodot-common/pkg/metrics3/metrics.go b/vendor/github.com/anodot/anodot-common/pkg/metrics3/metrics.go new file mode 100644 index 0000000..6554763 --- /dev/null +++ b/vendor/github.com/anodot/anodot-common/pkg/metrics3/metrics.go @@ -0,0 +1,97 @@ +package metrics3 + +import ( + "encoding/json" + "fmt" + "net/http" + "strings" + "time" +) + +type AnodotTimestamp struct { + time.Time +} + +func (t AnodotTimestamp) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprint(t.Unix())), nil +} + +type AnodotMetrics30 struct { + SchemaId string `json:"schemaId"` + Timestamp AnodotTimestamp `json:"timestamp"` + Dimensions map[string]string `json:"dimensions"` + Measurements map[string]float64 `json:"measurements"` + Tags map[string][]string `json:"tags"` +} + +func (m *AnodotMetrics30) MarshalJSON() ([]byte, error) { + type Alias AnodotMetrics30 + + dimesnions := make(map[string]string, len(m.Dimensions)) + measurements := make(map[string]float64, len(m.Measurements)) + + tags := make(map[string][]string, len(m.Tags)) + + for k, v := range m.Dimensions { + dimesnions[escape(strings.TrimSpace(k))] = escape(strings.TrimSpace(v)) + } + for k, v := range m.Measurements { + measurements[escape(strings.TrimSpace(k))] = v + } + + for k, v := range m.Tags { + tgs := make([]string, len(v)) + for i, tag := range v { + tgs[i] = escape(strings.TrimSpace(tag)) + } + tags[escape(strings.TrimSpace(k))] = tgs + } + + return json.Marshal(&struct { + Dimesnions map[string]string `json:"dimensions"` + Measurements map[string]float64 `json:"measurements"` + Tags map[string][]string `json:"tags"` + *Alias + }{ + Dimesnions: dimesnions, + Measurements: measurements, + Tags: tags, + Alias: (*Alias)(m), + }) +} + +type Anodot20Response struct { + Errors []struct { + Description string + Error int64 + Index string + } `json:"errors"` + HttpResponse *http.Response `json:"-"` +} + +func (r *Anodot20Response) HasErrors() bool { + return len(r.Errors) > 0 +} + +func (r *Anodot20Response) ErrorMessage() string { + return fmt.Sprintf("%+v\n", r.Errors) +} + +func (r *Anodot20Response) RawResponse() *http.Response { + return r.HttpResponse +} + +type SubmitMetricsResponse struct { + Anodot20Response +} + +type SubmitWatermarkResponse struct { + Anodot20Response +} + +func escape(s string) string { + result := strings.ReplaceAll(s, ".", "_") + result = strings.ReplaceAll(result, "=", "_") + + return strings.ReplaceAll(result, " ", "_") +} diff --git a/vendor/github.com/anodot/anodot-common/pkg/metrics3/schema.go b/vendor/github.com/anodot/anodot-common/pkg/metrics3/schema.go new file mode 100644 index 0000000..db0a944 --- /dev/null +++ b/vendor/github.com/anodot/anodot-common/pkg/metrics3/schema.go @@ -0,0 +1,49 @@ +package metrics3 + +type Measurment struct { + Base MeasurmentBase + Name string +} + +type MeasurmentBase struct { + Aggregation string `json:"aggregation"` + CountBy string `json:"countBy"` + Units string `json:"units,omitempty"` +} + +type DimensionPolicy struct { + Action string `json:"action"` + Fill string `json:"fill,omitempty"` +} + +type AnodotMetricsSchema struct { + Id string `json:"id,omitempty"` + Dimensions []string `json:"dimensions"` + Measurements map[string]MeasurmentBase `json:"measurements"` + MissingDimPolicy *DimensionPolicy `json:"missingDimPolicy,omitempty"` + Name string `json:"name"` + Version string `json:"version,omitempty"` +} + +type StreamSchemaWrapper struct { + Wrapper struct { + Schema AnodotMetricsSchema `json:"schema"` + } `json:"streamSchemaWrapper"` +} + +// Responses for api calls +// Inherits base methods and fields from ApiResponse structure using composition +type GetSchemaResponse struct { + Schemas []AnodotMetricsSchema + Api30Response +} + +type DeleteSchemaResponse struct { + SchemaId *string + Api30Response +} + +type CreateSchemaResponse struct { + SchemaId *string + Api30Response +} diff --git a/vendor/modules.txt b/vendor/modules.txt index d4a982e..375664f 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -3,8 +3,9 @@ github.com/alecthomas/template github.com/alecthomas/template/parse # github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 github.com/alecthomas/units -# github.com/anodot/anodot-common v0.0.8 +# github.com/anodot/anodot-common v0.0.10 github.com/anodot/anodot-common/pkg/metrics +github.com/anodot/anodot-common/pkg/metrics3 # github.com/beorn7/perks v1.0.1 github.com/beorn7/perks/quantile # github.com/cespare/xxhash/v2 v2.1.1