Skip to content

Commit

Permalink
style: format client file
Browse files Browse the repository at this point in the history
Signed-off-by: Rory Z <[email protected]>
  • Loading branch information
Rory-Z committed Nov 1, 2023
1 parent 0c8e712 commit 67b8786
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 52 deletions.
1 change: 0 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func (e edition) String() string {
}

type client interface {
getVersion() string
getLicense() (*collector.LicenseInfo, error)
getClusterStatus() (collector.ClusterStatus, error)
getBrokerMetrics() (*collector.Broker, error)
Expand Down
54 changes: 9 additions & 45 deletions client/client_4.x.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,38 @@ package client
import (
"emqx-exporter/collector"
"fmt"
"net/http"
"strconv"
"strings"
"time"

jsoniter "github.com/json-iterator/go"
"github.com/valyala/fasthttp"
)

var _ client = &cluster4x{}

type cluster4x struct {
version string
edition edition
client *fasthttp.Client
uri *fasthttp.URI
}

func (n *cluster4x) getVersion() string {
return n.version
}

func (n *cluster4x) getLicense() (lic *collector.LicenseInfo, err error) {
if n.edition == openSource {
return
}

resp := struct {
Data struct {
MaxConnections int64 `json:"max_connections"`
ExpiryAt string `json:"expiry_at"`
}
Code int
}{}
data, statusCode, err := callHTTPGet(n.client, n.uri, "/api/v4/license")
if statusCode == http.StatusNotFound {
// open source version doesn't support license api
err = nil
return
}
err = callHTTPGetWithResp(n.client, n.uri, "/api/v4/license", &resp)
if err != nil {
return
}

err = jsoniter.Unmarshal(data, &resp)
if err != nil {
err = fmt.Errorf("unmarshal license failed: /api/v4/license")
return
}

if resp.Code != 0 {
err = fmt.Errorf("get err from license api: %d", resp.Code)
return
}

expiryAt, err := time.Parse("2006-01-02 15:04:05", resp.Data.ExpiryAt)
if err != nil {
err = fmt.Errorf("parse expiry time failed: %s", resp.Data.ExpiryAt)
Expand Down Expand Up @@ -108,41 +90,23 @@ func (n *cluster4x) getClusterStatus() (cluster collector.ClusterStatus, err err
load.Load5, _ = strconv.ParseFloat(data.Load5, 64)
load.Load15, _ = strconv.ParseFloat(data.Load15, 64)
cluster.CPULoads[nodeName] = load

n.version = data.Version
}
return
}

func (n *cluster4x) getBrokerMetrics() (metrics *collector.Broker, err error) {
resp := struct {
Data struct {
Sent int64
Received int64
Sent int64 `json:"sent"`
Received int64 `json:"received"`
}
Code int
}{}
data, statusCode, err := callHTTPGet(n.client, n.uri, "/api/v4/monitor/current_metrics")
if statusCode == http.StatusNotFound {
// open source version doesn't support this api
err = nil
return
}
err = callHTTPGetWithResp(n.client, n.uri, "/api/v4/monitor/current_metrics", &resp)
if err != nil {
return
}

err = jsoniter.Unmarshal(data, &resp)
if err != nil {
err = fmt.Errorf("unmarshal license failed: /api/v4/monitor/current_metrics")
return
}

if resp.Code != 0 {
err = fmt.Errorf("get err from monitor api: %d", resp.Code)
return
}

metrics = &collector.Broker{
MsgInputPeriodSec: resp.Data.Received,
MsgOutputPeriodSec: resp.Data.Sent,
Expand Down
6 changes: 0 additions & 6 deletions client/client_5.x.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@ import (
var _ client = &cluster5x{}

type cluster5x struct {
version string
edition edition
client *fasthttp.Client
uri *fasthttp.URI
}

func (n *cluster5x) getVersion() string {
return n.version + "-" + n.edition.String()
}

func (n *cluster5x) getLicense() (lic *collector.LicenseInfo, err error) {
if n.edition == openSource {
return
Expand Down Expand Up @@ -98,7 +93,6 @@ func (n *cluster5x) getClusterStatus() (cluster collector.ClusterStatus, err err
} else {
n.edition = enterprise
}
n.version = data.Version
}
return
}
Expand Down

0 comments on commit 67b8786

Please sign in to comment.