Skip to content

Commit

Permalink
Merge pull request #9 from opencost/atm/pass-by-ptr
Browse files Browse the repository at this point in the history
pass by ptr
  • Loading branch information
ameijer authored Mar 7, 2024
2 parents 0c240d0 + 807c632 commit 3694d01
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 1,294 deletions.
22 changes: 11 additions & 11 deletions datadog/cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ type DatadogCostSource struct {
rateLimiter *rate.Limiter
}

func (d *DatadogCostSource) GetCustomCosts(req pb.CustomCostRequest) []pb.CustomCostResponse {
results := []pb.CustomCostResponse{}
func (d *DatadogCostSource) GetCustomCosts(req *pb.CustomCostRequest) []*pb.CustomCostResponse {
results := []*pb.CustomCostResponse{}

targets, err := opencost.GetWindows(req.Start.AsTime(), req.End.AsTime(), req.Resolution.AsDuration())
if err != nil {
log.Errorf("error getting windows: %v", err)
errResp := pb.CustomCostResponse{
Errors: []string{fmt.Sprintf("error getting windows: %v", err)},
}
results = append(results, errResp)
results = append(results, &errResp)
return results
}

Expand All @@ -64,7 +64,7 @@ func (d *DatadogCostSource) GetCustomCosts(req pb.CustomCostRequest) []pb.Custom
errResp := pb.CustomCostResponse{
Errors: []string{fmt.Sprintf("error getting dd pricing: %v", err)},
}
results = append(results, errResp)
results = append(results, &errResp)
return results
} else {
log.Debugf("got list pricing: %v", listPricing.Details)
Expand Down Expand Up @@ -123,7 +123,7 @@ func boilerplateDDCustomCost(win opencost.Window) pb.CustomCostResponse {
Costs: []*pb.CustomCost{},
}
}
func (d *DatadogCostSource) getDDCostsForWindow(window opencost.Window, listPricing *datadogplugin.PricingInformation) pb.CustomCostResponse {
func (d *DatadogCostSource) getDDCostsForWindow(window opencost.Window, listPricing *datadogplugin.PricingInformation) *pb.CustomCostResponse {
ccResp := boilerplateDDCustomCost(window)
params := datadogV2.NewGetHourlyUsageOptionalParameters()
params.FilterTimestampEnd = window.End()
Expand All @@ -141,7 +141,7 @@ func (d *DatadogCostSource) getDDCostsForWindow(window opencost.Window, listPric
if err != nil {
log.Errorf("error waiting on rate limiter`: %v\n", err)
ccResp.Errors = append(ccResp.Errors, err.Error())
return ccResp
return &ccResp
}

resp, r, err := d.usageApi.GetHourlyUsage(d.ddCtx, *window.Start(), "all", *params)
Expand Down Expand Up @@ -192,7 +192,7 @@ func (d *DatadogCostSource) getDDCostsForWindow(window opencost.Window, listPric
}
}

return ccResp
return &ccResp
}

// the public pricing used in the pricing list doesn't always match the usage reports
Expand Down Expand Up @@ -323,17 +323,17 @@ func scrapeDatadogPrices(url string) (*datadogplugin.PricingInformation, error)
// Send a GET request to the URL
response, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("Failed to fetch the page: %v", err)
return nil, fmt.Errorf("failed to fetch the page: %v", err)
}
defer response.Body.Close()

// Check if the request was successful
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Failed to retrieve pricing page. Status code: %d", response.StatusCode)
return nil, fmt.Errorf("failed to retrieve pricing page. Status code: %d", response.StatusCode)
}
b, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("Failed to read pricing page body: %v", err)
return nil, fmt.Errorf("failed to read pricing page body: %v", err)
}
res := datadogplugin.DatadogProJSON{}
r := regexp.MustCompile(`var productDetailData = \s*(.*?)\s*;`)
Expand All @@ -346,7 +346,7 @@ func scrapeDatadogPrices(url string) (*datadogplugin.PricingInformation, error)
log.Debugf("matches[0][1]:" + matches[0][1])
err = json.Unmarshal([]byte(matches[0][1]), &res)
if err != nil {
return nil, fmt.Errorf("Failed to read pricing page body: %v", err)
return nil, fmt.Errorf("failed to read pricing page body: %v", err)
}

return &res.OfferData.PricingInformation, nil
Expand Down
30 changes: 14 additions & 16 deletions datadog/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@ module github.com/opencost/opencost-plugins/datadog
go 1.21.6

require (
github.com/DataDog/datadog-api-client-go v1.16.0 // indirect
github.com/DataDog/datadog-api-client-go/v2 v2.23.0 // indirect
github.com/DataDog/datadog-api-client-go/v2 v2.23.0
github.com/hashicorp/go-plugin v1.6.0
github.com/opencost/opencost-plugins/test v0.0.0-20240307142929-df4df8ee69fa
github.com/opencost/opencost/core v0.0.0-20240307141548-816f98c9051a
golang.org/x/time v0.5.0
google.golang.org/protobuf v1.33.0
)

require (
github.com/DataDog/zstd v1.5.5 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-hclog v1.6.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -29,34 +35,26 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/opencost/opencost-plugins v0.0.0-20240301164750-359b361ecae1 // indirect
github.com/opencost/opencost-plugins/test v0.0.0-20240301164750-359b361ecae1 // indirect
github.com/opencost/opencost/core v0.0.0-20240301064956-9c1f94b12d5e // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/rs/zerolog v1.32.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/oauth2 v0.17.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240228224816-df926f6c8641 // indirect
google.golang.org/grpc v1.62.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240304212257-790db918fca8 // indirect
google.golang.org/grpc v1.62.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit 3694d01

Please sign in to comment.