-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from castai/feat/send-collected-metrics
Send collected metrics to castai mothership
- Loading branch information
Showing
10 changed files
with
357 additions
and
18 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
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,121 @@ | ||
package castai | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/go-resty/resty/v2" | ||
"github.com/sirupsen/logrus" | ||
"google.golang.org/protobuf/proto" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
|
||
"github.com/castai/gpu-metrics-exporter/pb" | ||
) | ||
|
||
const ( | ||
tokenHeader = "X-API-Key" // #nosec G101 | ||
retryCount = 5 | ||
) | ||
|
||
var ( | ||
backoff = wait.Backoff{ | ||
Steps: retryCount, | ||
Duration: 50 * time.Millisecond, | ||
Factor: 8, | ||
Jitter: 0.15, | ||
} | ||
|
||
contentTypeHeader = http.CanonicalHeaderKey("Content-Type") | ||
contentType = "application/protobuf" | ||
|
||
contentEncodingHeader = http.CanonicalHeaderKey("Content-Encoding") | ||
contentEncoding = "gzip" | ||
|
||
userAgentHeader = http.CanonicalHeaderKey("User-Agent") | ||
userAgent = "castai-gpu-metrics-exporter/" | ||
) | ||
|
||
type Config struct { | ||
URL string | ||
APIKey string | ||
ClusterID string | ||
} | ||
|
||
type Client interface { | ||
UploadBatch(ctx context.Context, batch *pb.MetricsBatch) error | ||
} | ||
|
||
type client struct { | ||
restyClient *resty.Client | ||
cfg Config | ||
log logrus.FieldLogger | ||
} | ||
|
||
func NewClient(cfg Config, log logrus.FieldLogger, restyClient *resty.Client, version string) Client { | ||
restyClient.BaseURL = cfg.URL | ||
restyClient.SetHeaders(map[string]string{ | ||
tokenHeader: cfg.APIKey, | ||
contentTypeHeader: contentType, | ||
contentEncodingHeader: contentEncoding, | ||
userAgentHeader: fmt.Sprintf("%s%s", userAgent, version), | ||
}) | ||
|
||
return &client{ | ||
restyClient: restyClient, | ||
cfg: cfg, | ||
log: log, | ||
} | ||
} | ||
|
||
func (c client) UploadBatch(ctx context.Context, batch *pb.MetricsBatch) error { | ||
buffer, err := c.toBuffer(batch) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return wait.ExponentialBackoffWithContext(ctx, backoff, func(ctx context.Context) (done bool, err error) { | ||
resp, err := c.restyClient.R(). | ||
SetContext(ctx). | ||
SetBody(buffer). | ||
Post(fmt.Sprintf("/v1/kubernetes/clusters/%s/gpu-metrics", c.cfg.ClusterID)) | ||
|
||
if err != nil { | ||
c.log.Errorf("error making http request %v", err) | ||
return false, nil | ||
} | ||
|
||
statusCode := resp.StatusCode() | ||
switch { | ||
case statusCode >= 200 && statusCode < 300: | ||
return true, nil | ||
case statusCode >= 400 && statusCode < 500: | ||
return true, fmt.Errorf("client error: %d %s", statusCode, resp.Status()) | ||
default: | ||
c.log.Errorf("server error or unexpected response code: %d %s", statusCode, resp.Status()) | ||
return false, nil | ||
} | ||
}) | ||
} | ||
|
||
func (c client) toBuffer(batch *pb.MetricsBatch) (*bytes.Buffer, error) { | ||
payload := new(bytes.Buffer) | ||
|
||
protoBytes, err := proto.Marshal(batch) | ||
if err != nil { | ||
return nil, fmt.Errorf("error marshaling batch %w", err) | ||
} | ||
|
||
writer := gzip.NewWriter(payload) | ||
if _, err := writer.Write(protoBytes); err != nil { | ||
return nil, fmt.Errorf("error compressing payload %w", err) | ||
} | ||
if err := writer.Close(); err != nil { | ||
return nil, fmt.Errorf("error closing gzip writer %w", err) | ||
} | ||
|
||
return payload, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package castai_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/go-resty/resty/v2" | ||
"github.com/jarcoal/httpmock" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/castai/gpu-metrics-exporter/internal/castai" | ||
"github.com/castai/gpu-metrics-exporter/pb" | ||
) | ||
|
||
func Test_UploadBatch(t *testing.T) { | ||
log := logrus.New() | ||
restyClient := resty.New() | ||
client := castai.NewClient(castai.Config{ | ||
URL: "http://localhost", | ||
APIKey: "my-fake-token", | ||
ClusterID: "cluster-id-1", | ||
}, log, restyClient, "test") | ||
|
||
httpmock.ActivateNonDefault(restyClient.GetClient()) | ||
t.Run("calls gpu-metrics endpoint with proper headers", func(t *testing.T) { | ||
r := require.New(t) | ||
|
||
httpmock.RegisterResponder( | ||
"POST", | ||
"http://localhost/v1/kubernetes/clusters/cluster-id-1/gpu-metrics", | ||
func(req *http.Request) (*http.Response, error) { | ||
contentType := req.Header.Get("Content-type") | ||
r.Equal("application/protobuf", contentType) | ||
|
||
contentEncoding := req.Header.Get("Content-encoding") | ||
r.Equal("gzip", contentEncoding) | ||
|
||
apiKey := req.Header.Get("X-API-Key") | ||
r.Equal("my-fake-token", apiKey) | ||
|
||
userAgent := req.Header.Get("User-Agent") | ||
r.Equal("castai-gpu-metrics-exporter/test", userAgent) | ||
|
||
return &http.Response{StatusCode: 200}, nil | ||
}, | ||
) | ||
|
||
_ = client.UploadBatch(context.Background(), &pb.MetricsBatch{}) | ||
}) | ||
} |
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.