Skip to content

Commit

Permalink
Merge pull request #13 from chatwork/fix_some_file
Browse files Browse the repository at this point in the history
Fix some file
  • Loading branch information
cw-sakamoto authored Nov 30, 2023
2 parents 30b06c0 + 01e81e9 commit b911e43
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 47 deletions.
39 changes: 17 additions & 22 deletions cmd/datadog-agent/datadog-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (

type DatadogAgent struct {
*cmd.Checker
ApiKey string
AppKey string
ClusterName string
WaitTime time.Duration
ApiKey string
AppKey string
QueryMetrics string
WaitTime time.Duration
}

func NewDatadogAgent(debug bool, logger func() *logrus.Entry, chatwork *notify.Chatwork) (*DatadogAgent, error) {
Expand All @@ -36,16 +36,18 @@ func NewDatadogAgent(debug bool, logger func() *logrus.Entry, chatwork *notify.C

apiKey := ""
appKey := ""
clusterName := ""
queryMetrics := ""

if v := os.Getenv("DD_API_KEY"); v != "" {
apiKey = v
}
if v := os.Getenv("DD_APP_KEY"); v != "" {
appKey = v
}
if v := os.Getenv("CLUSTER_NAME"); v != "" {
clusterName = v

queryMetrics = "avg:kubernetes.cpu.user.total"
if v := os.Getenv("QUERY_METRICS"); v != "" {
queryMetrics = v
}

k8sclient, err := config.NewK8sClientset()
Expand All @@ -55,11 +57,11 @@ func NewDatadogAgent(debug bool, logger func() *logrus.Entry, chatwork *notify.C
}

return &DatadogAgent{
Checker: cmd.NewChecker(namespace, k8sclient, debug, logger, chatwork),
ApiKey: apiKey,
AppKey: appKey,
ClusterName: clusterName,
WaitTime: 3 * 60 * time.Second,
Checker: cmd.NewChecker(namespace, k8sclient, debug, logger, chatwork),
ApiKey: apiKey,
AppKey: appKey,
QueryMetrics: queryMetrics,
WaitTime: 3 * 60 * time.Second,
}, nil
}

Expand All @@ -71,12 +73,6 @@ func (d *DatadogAgent) Check() error {
d.Chatwork.AddMessage("DD_API_KEY or DD_APP_KEY is empty\n")
return errors.New("DD_API_KEY or DD_APP_KEY is empty")
}
if d.ClusterName == "" {
d.Logger().Error("CLUSTER_NAME is empty")
d.Chatwork.AddMessage("CLUSTER_NAME is empty\n")
return errors.New("CLUSTER_NAME is empty")
}

d.Chatwork.AddMessage("datadog-agent check start\n")

if err := d.checkMetrics(); err != nil {
Expand All @@ -101,17 +97,16 @@ func (d *DatadogAgent) checkMetrics() error {
configuration := datadog.NewConfiguration()
apiClient := datadog.NewAPIClient(configuration)
api := datadogV1.NewMetricsApi(apiClient)
query := fmt.Sprintf("avg:kubernetes.cpu.user.total{env:%s}", d.ClusterName)

d.Logger().Info("Waiting metrics...")
time.Sleep(d.WaitTime)

d.Logger().Infof("Querying metrics with query: %s", query)
d.Chatwork.AddMessage(fmt.Sprintf("Querying metrics with query: %s", query))
d.Logger().Infof("Querying metrics with query: %s", d.QueryMetrics)
d.Chatwork.AddMessage(fmt.Sprintf("Querying metrics with query: %s", d.QueryMetrics))
now := time.Now().Unix()
from := now - 60*2
err := wait.PollUntilContextTimeout(context.Background(), 30*time.Second, 10*time.Minute, true, func(ctx context.Context) (bool, error) {
resp, r, err := api.QueryMetrics(ddctx, from, now, query)
resp, r, err := api.QueryMetrics(ddctx, from, now, d.QueryMetrics)

if err != nil {
if r != nil && r.StatusCode == 403 {
Expand Down
35 changes: 10 additions & 25 deletions cmd/datadog-agent/datadog-agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package datadogagent

import (
"errors"
"os"
"testing"
"time"

Expand Down Expand Up @@ -41,11 +40,11 @@ func TestCheck(t *testing.T) {

chatwork := &notify.Chatwork{ApiToken: "token", RoomId: "test", Logger: logger}
datadogAgent := &DatadogAgent{
Checker: cmd.NewChecker("test", k8sclient, true, logger, chatwork),
ApiKey: "",
AppKey: "",
ClusterName: "",
WaitTime: 1 * time.Second,
Checker: cmd.NewChecker("test", k8sclient, true, logger, chatwork),
ApiKey: "",
AppKey: "",
QueryMetrics: "",
WaitTime: 1 * time.Second,
}

err = datadogAgent.Check()
Expand All @@ -58,26 +57,12 @@ func TestCheck(t *testing.T) {
t.Errorf("Expected '%s', got '%s'", expectedError.Error(), err.Error())
}

os.Setenv("DD_API_KEY", "test")
os.Setenv("DD_APP_KEY", "test")
datadogAgent, err = NewDatadogAgent(true, logger, chatwork)
if err != nil {
t.Fatalf("NewDatadogAgent: %s", err)
}
err = datadogAgent.Check()
expectedError = errors.New("CLUSTER_NAME is empty")
if err.Error() != expectedError.Error() {
t.Errorf("Expected '%s', got '%s'", expectedError.Error(), err.Error())
}

os.Setenv("CLUSTER_NAME", "test")

datadogAgent = &DatadogAgent{
Checker: cmd.NewChecker("test", k8sclient, true, logger, chatwork),
ApiKey: "test",
AppKey: "test",
ClusterName: "test",
WaitTime: 1 * time.Second,
Checker: cmd.NewChecker("test", k8sclient, true, logger, chatwork),
ApiKey: "test",
AppKey: "test",
QueryMetrics: "avg:kubernetes.cpu.user.total",
WaitTime: 1 * time.Second,
}

err = datadogAgent.Check()
Expand Down
1 change: 1 addition & 0 deletions cmd/fluent/fluent.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func (f *Fluent) checkS3Object() error {

if err != nil {
f.Logger().Error("Timed out waiting for output S3 Object:", err)
f.Chatwork.AddMessage(fmt.Sprintf("Timed out waiting for output S3 Object: %s\n", err))
return err
}

Expand Down

0 comments on commit b911e43

Please sign in to comment.