-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: change datadog api library to the official one
- Loading branch information
Showing
9 changed files
with
157 additions
and
112 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,72 @@ | ||
package testutil | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
_nethttp "net/http" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/zorkian/go-datadog-api" | ||
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1" | ||
) | ||
|
||
func MockQueryMetrics(from int64, to int64, query string) ([]datadog.Series, error) { | ||
type MockMetricsApi struct { | ||
} | ||
|
||
func NewMockMockMetricsApi() *MockMetricsApi { | ||
return &MockMetricsApi{} | ||
} | ||
|
||
func (m *MockMetricsApi) QueryMetrics(ctx context.Context, from int64, to int64, query string) (datadogV1.MetricsQueryResponse, *_nethttp.Response, error) { | ||
var ( | ||
resp datadogV1.MetricsQueryResponse | ||
httpResp *_nethttp.Response | ||
) | ||
_, filename, _, _ := runtime.Caller(0) | ||
currentFolderPath := filepath.Dir(filename) | ||
reader, err := ioutil.ReadFile(fmt.Sprintf("%s/mock/%s.json", currentFolderPath, query)) | ||
|
||
// Read directory contents | ||
mockQueryDir := filepath.Join(filepath.Dir(filename), "mock") | ||
files, err := os.ReadDir(mockQueryDir) | ||
if err != nil { | ||
return nil, err | ||
return resp, httpResp, err | ||
} | ||
var data []datadog.Series | ||
err = json.Unmarshal(reader, &data) | ||
if err != nil { | ||
return nil, err | ||
|
||
// Create a list of accessible queries | ||
mockQueryList := make(map[string]string, len(files)) | ||
for _, file := range files { | ||
if !file.IsDir() { | ||
mockQueryName := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name())) | ||
mockQueryFilePath := filepath.Join(mockQueryDir, file.Name()) | ||
mockQueryList[mockQueryName] = mockQueryFilePath | ||
} | ||
} | ||
return data, nil | ||
} | ||
|
||
type MockDatadog struct { | ||
} | ||
// Check if the query parameter is among the accessible queries | ||
mockQueryFilePath, found := mockQueryList[query] | ||
if !found { | ||
return resp, httpResp, fmt.Errorf("Mock file not found for query: %s", query) | ||
} | ||
|
||
func NewMockDatadog() *MockDatadog { | ||
return &MockDatadog{} | ||
} | ||
// Rest of the code... | ||
|
||
func (m *MockDatadog) QueryMetrics(from int64, to int64, query string) ([]datadog.Series, error) { | ||
return MockQueryMetrics(from, to, query) | ||
reader, err := os.ReadFile(mockQueryFilePath) | ||
if err != nil { | ||
return resp, httpResp, err | ||
} | ||
|
||
err = json.Unmarshal(reader, &resp) | ||
if err != nil { | ||
fmt.Printf("Error reading mock response:\n %s\n", err) | ||
return resp, httpResp, err | ||
} | ||
|
||
err = json.Unmarshal(reader, &resp) | ||
if err != nil { | ||
fmt.Printf("Error reading mock response:\n %s\n", err) | ||
return resp, httpResp, err | ||
} | ||
return resp, httpResp, 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 |
---|---|---|
@@ -1,38 +1,40 @@ | ||
[ | ||
{ | ||
"metric": "foo.metric.response.4xx", | ||
"display_name": "foo.metric.response.4xx", | ||
"pointlist": [ | ||
[ | ||
1557941880000, | ||
1 | ||
{ | ||
"series": [ | ||
{ | ||
"metric": "foo.metric.response.4xx", | ||
"display_name": "foo.metric.response.4xx", | ||
"pointlist": [ | ||
[ | ||
1557941880000, | ||
1 | ||
], | ||
[ | ||
1557941910000, | ||
2 | ||
], | ||
[ | ||
1557942010000, | ||
2 | ||
] | ||
], | ||
[ | ||
1557941910000, | ||
2 | ||
"scope": "service:foo-production", | ||
"expression": "sum:sw.foo.metric.response.4xx{service:foo-production}" | ||
}, | ||
{ | ||
"metric": "foo.metric.response.5xx", | ||
"display_name": "foo.metric.response.5xx", | ||
"pointlist": [ | ||
[ | ||
1557941880000, | ||
1 | ||
], | ||
[ | ||
1557941910000, | ||
2 | ||
] | ||
], | ||
[ | ||
1557942010000, | ||
2 | ||
] | ||
], | ||
"scope": "service:foo-production", | ||
"expression": "sum:sw.foo.metric.response.4xx{service:foo-production}" | ||
}, | ||
{ | ||
"metric": "foo.metric.response.5xx", | ||
"display_name": "foo.metric.response.5xx", | ||
"pointlist": [ | ||
[ | ||
1557941880000, | ||
1 | ||
], | ||
[ | ||
1557941910000, | ||
2 | ||
] | ||
], | ||
"scope": "service:foo-production", | ||
"expression": "sum:sw.foo.metric.response.5xx{service:foo-production}" | ||
} | ||
] | ||
"scope": "service:foo-production", | ||
"expression": "sum:sw.foo.metric.response.5xx{service:foo-production}" | ||
} | ||
] | ||
} |
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,18 +1,20 @@ | ||
[ | ||
{ | ||
"metric": "foo.metric.response.2xx", | ||
"display_name": "foo.metric.response.2xx", | ||
"pointlist": [ | ||
[ | ||
1557941880000, | ||
1 | ||
{ | ||
"series": [ | ||
{ | ||
"metric": "foo.metric.response.2xx", | ||
"display_name": "foo.metric.response.2xx", | ||
"pointlist": [ | ||
[ | ||
1557941880000, | ||
1 | ||
], | ||
[ | ||
1557941910000, | ||
2 | ||
] | ||
], | ||
[ | ||
1557941910000, | ||
2 | ||
] | ||
], | ||
"scope": "service:foo-production", | ||
"expression": "sum:sw.foo.metric.response.2xx{service:foo-production}" | ||
} | ||
] | ||
"scope": "service:foo-production", | ||
"expression": "sum:sw.foo.metric.response.2xx{service:foo-production}" | ||
} | ||
] | ||
} |
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
Oops, something went wrong.