-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
251e1a0
commit 498c7a8
Showing
16 changed files
with
460 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
FROM golang:1.5-alpine | ||
FROM golang:1.15.5 | ||
|
||
ADD ./load-watcher /usr/local/bin/load-watcher | ||
WORKDIR /go/src/github.com/paypal/load-watcher | ||
COPY . . | ||
RUN make build | ||
|
||
CMD ["/usr/local/bin/load-watcher"] | ||
FROM alpine:3.12 | ||
|
||
COPY --from=0 /go/src/github.com/paypal/load-watcher/bin/load-watcher /bin/load-watcher | ||
|
||
CMD ["/bin/load-watcher"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Copyright 2020 PayPal | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
COMMONENVVAR=GOOS=$(shell uname -s | tr A-Z a-z) GOARCH=$(subst x86_64,amd64,$(patsubst i%86,386,$(shell uname -m))) | ||
BUILDENVVAR=CGO_ENABLED=0 | ||
|
||
.PHONY: all | ||
all: build | ||
|
||
.PHONY: build | ||
build: | ||
$(COMMONENVVAR) $(BUILDENVVAR) go build -o bin/load-watcher main.go | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -rf ./bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/paypal/load-watcher/pkg/utils" | ||
"github.com/paypal/load-watcher/pkg/watcher" | ||
) | ||
|
||
func main() { | ||
metricProviderType := "load-watcher" | ||
metricEndpoint := "http://localhost:2020" | ||
|
||
client, err := utils.CreateClient(metricProviderType, metricEndpoint, "") | ||
if err != nil { | ||
log.Fatalf("unable to create new client: %v", err) | ||
} | ||
|
||
curWindow := watcher.CurrentFifteenMinuteWindow() | ||
watchermetrics, err := client.FetchAllHostsMetrics(curWindow) | ||
|
||
if err != nil { | ||
log.Fatalf("unable to fetch data from client: %v", err) | ||
} | ||
|
||
log.Printf("Timestamp: %v", watchermetrics.Timestamp) | ||
log.Printf("Source: %v", watchermetrics.Source) | ||
log.Printf("Window, start: %v", watchermetrics.Window.Start) | ||
log.Printf("Window, end: %v", watchermetrics.Window.End) | ||
log.Printf("Window, duration: %v", watchermetrics.Window.Duration) | ||
|
||
for k, v := range watchermetrics.Data.NodeMetricsMap { | ||
log.Printf("Host: %v", k) | ||
for _, metric := range v.Metrics { | ||
log.Printf("%+v\n", metric) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 | ||
kind: Deployment | ||
metadata: | ||
name: load-watcher | ||
namespace: monitoring | ||
spec: | ||
strategy: | ||
type: Recreate | ||
selector: | ||
matchLabels: | ||
app: load-watcher | ||
replicas: 1 # tells deployment to run 1 pods matching the template | ||
template: # create pods using pod definition in this template | ||
metadata: | ||
labels: | ||
app: load-watcher | ||
spec: | ||
containers: | ||
- name: watcher | ||
image: chenw/load-watcher:latest | ||
ports: | ||
- containerPort: 2020 | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: load-watcher | ||
namespace: monitoring | ||
labels: | ||
app: load-watcher | ||
spec: | ||
externalTrafficPolicy: Local | ||
ports: | ||
- name: http | ||
port: 2020 | ||
protocol: TCP | ||
targetPort: 2020 | ||
selector: | ||
app: load-watcher | ||
type: LoadBalancer | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Copyright 2020 | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package metricsprovider | ||
|
||
import ( | ||
"crypto/tls" | ||
"github.com/francoispqt/gojay" | ||
"github.com/paypal/load-watcher/pkg/watcher" | ||
"k8s.io/klog/v2" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
LoadWatcherClientName = "load-watcher" | ||
DefaultLoadWatcherClientEndpoint = "https://load-watcher.monitoring.svc.cluster.local:2020" | ||
watcherQuery = "/watcher" | ||
defaultRetries = 3 | ||
) | ||
|
||
var ( | ||
loadWatcherEndpoint string | ||
) | ||
|
||
type loadWatcherClient struct { | ||
client http.Client | ||
} | ||
|
||
func NewLoadWatcherClient(loadwatcherurl string) (watcher.FetcherClient, error) { | ||
tlsConfig := &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
} | ||
|
||
loadWatcherEndpoint = loadwatcherurl | ||
|
||
return loadWatcherClient{client: http.Client{ | ||
Timeout: httpClientTimeout, | ||
Transport: tlsConfig}}, nil | ||
} | ||
|
||
func (s loadWatcherClient) Name() string { | ||
return LoadWatcherClientName | ||
} | ||
|
||
func (s loadWatcherClient) FetchAllHostsMetrics(window *watcher.Window) (watcher.WatcherMetrics, error) { | ||
allhostQueryURL := loadWatcherEndpoint+watcherQuery | ||
return s.queryMetrics(allhostQueryURL) | ||
} | ||
|
||
func (s loadWatcherClient) FetchHostMetrics(host string, window *watcher.Window) (watcher.WatcherMetrics, error) { | ||
hostQueryURL := loadWatcherEndpoint+watcherQuery + "?host=" + host | ||
return s.queryMetrics(hostQueryURL) | ||
} | ||
|
||
func (s loadWatcherClient) queryMetrics(queryURL string) (watcher.WatcherMetrics, error) { | ||
metrics := watcher.WatcherMetrics{} | ||
req, err := http.NewRequest(http.MethodGet, queryURL, nil) | ||
if err != nil { | ||
return metrics, err | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
var retries int = defaultRetries | ||
var resp *http.Response | ||
for retries > 0 { | ||
resp, err = s.client.Do(req) | ||
|
||
if err != nil { | ||
retries -= 1 | ||
} else { | ||
break | ||
} | ||
} | ||
if err != nil { | ||
return metrics, err | ||
} | ||
|
||
defer resp.Body.Close() | ||
klog.V(6).Infof("received status code %v from watcher", resp.StatusCode) | ||
if resp.StatusCode == http.StatusOK { | ||
data := watcher.Data{NodeMetricsMap: make(map[string]watcher.NodeMetrics)} | ||
metrics = watcher.WatcherMetrics{Data: data} | ||
dec := gojay.BorrowDecoder(resp.Body) | ||
defer dec.Release() | ||
err = dec.Decode(&metrics) | ||
if err != nil { | ||
klog.Errorf("unable to decode watcher metrics: %v", err) | ||
return metrics, err | ||
} | ||
|
||
return metrics, nil | ||
} else { | ||
klog.Errorf("received status code %v from watcher", resp.StatusCode) | ||
} | ||
|
||
return metrics, nil | ||
} |
Oops, something went wrong.