-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* resolve issue #5 and #12 * remove redundant empty lines and fix some comments * Update main.go sort library imports * Address review comments Refactor code, move metricsprovider to internal Signed-off-by: Abdul Qadeer <[email protected]> * Update copyright in Makefile * Delete manifests directory Signed-off-by: Abdul Qadeer <[email protected]> Co-authored-by: Abdul Qadeer <[email protected]>
- Loading branch information
1 parent
d3ea39d
commit fd8dfd7
Showing
15 changed files
with
430 additions
and
155 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 2021 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
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 2021 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. | ||
*/ | ||
|
||
package api | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/francoispqt/gojay" | ||
"github.com/paypal/load-watcher/pkg/watcher" | ||
"github.com/paypal/load-watcher/pkg/watcher/internal/metricsprovider" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
const ( | ||
httpClientTimeoutSeconds = 55 * time.Second | ||
) | ||
|
||
// Client for Watcher APIs as a library | ||
type libraryClient struct { | ||
fetcherClient watcher.MetricsProviderClient | ||
watcher *watcher.Watcher | ||
} | ||
|
||
// Client for Watcher APIs as a service | ||
type serviceClient struct { | ||
httpClient http.Client | ||
watcherAddress string | ||
} | ||
|
||
// Creates a new watcher client when using watcher as a library | ||
func NewLibraryClient(opts watcher.MetricsProviderOpts) (Client, error) { | ||
var err error | ||
client := libraryClient{} | ||
switch opts.Name { | ||
case watcher.PromClientName: | ||
client.fetcherClient, err = metricsprovider.NewPromClient(opts) | ||
case watcher.SignalFxClientName: | ||
client.fetcherClient, err = metricsprovider.NewSignalFxClient(opts) | ||
default: | ||
client.fetcherClient, err = metricsprovider.NewMetricsServerClient() | ||
} | ||
if err != nil { | ||
return client, err | ||
} | ||
client.watcher = watcher.NewWatcher(client.fetcherClient) | ||
client.watcher.StartWatching() | ||
return client, nil | ||
} | ||
|
||
// Creates a new watcher client when using watcher as a service | ||
func NewServiceClient(watcherAddress string) (Client, error) { | ||
return serviceClient{ | ||
httpClient: http.Client{ | ||
Timeout: httpClientTimeoutSeconds, | ||
}, | ||
watcherAddress: watcherAddress, | ||
}, nil | ||
} | ||
|
||
func (c libraryClient) GetLatestWatcherMetrics() (*watcher.WatcherMetrics, error) { | ||
return c.watcher.GetLatestWatcherMetrics(watcher.FifteenMinutes) | ||
} | ||
|
||
func (c serviceClient) GetLatestWatcherMetrics() (*watcher.WatcherMetrics, error) { | ||
req, err := http.NewRequest(http.MethodGet, c.watcherAddress+watcher.BaseUrl, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
//TODO(aqadeer): Add a couple of retries for transient errors | ||
resp, err := c.httpClient.Do(req) | ||
if err != nil { | ||
return nil, 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 nil, err | ||
} else { | ||
return &metrics, nil | ||
} | ||
} else { | ||
klog.Errorf("received status code %v from watcher", resp.StatusCode) | ||
} | ||
return nil, nil | ||
} |
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
Oops, something went wrong.