Skip to content

Commit

Permalink
Merge pull request #216 from dikhan/bugfix/telemetry_http_endpoint_ma…
Browse files Browse the repository at this point in the history
…rshall_issue

[BugFix: Issue #217] Fix marshal nil pointer caused when marshalling TelemetryProviderHTTPEndpoint
  • Loading branch information
dikhan committed Apr 3, 2020
2 parents a21d807 + 8de9eba commit 5c2c992
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
20 changes: 19 additions & 1 deletion openapi/plugin_config_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,32 @@ func TestPluginConfigSchemaV1Marshal(t *testing.T) {
},
},
}
pluginConfigSchema = NewPluginConfigSchemaV1(services, nil)
pluginConfigSchema = NewPluginConfigSchemaV1(services, &TelemetryConfig{
Graphite: &TelemetryProviderGraphite{
Host: "some-host.com",
Port: 8080,
Prefix: "some_prefix",
},
HTTPEndpoint: &TelemetryProviderHTTPEndpoint{
URL: "http://my-api.com/v1/metrics",
Prefix: "some_prefix",
},
})
Convey("When Marshal method is called", func() {
marshalConfig, err := pluginConfigSchema.Marshal()
Convey("Then the error returned should be nil as configuration is correct", func() {
So(err, ShouldBeNil)
})
Convey("And the marshalConfig should contain the right marshal configuration", func() {
expectedConfig := fmt.Sprintf(`version: "1"
telemetry:
graphite:
host: some-host.com
port: 8080
prefix: some_prefix
http_endpoint:
url: http://my-api.com/v1/metrics
prefix: some_prefix
services:
test:
swagger-url: %s
Expand Down
5 changes: 2 additions & 3 deletions openapi/plugin_config_telemetry_provider_httpendpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ type TelemetryProviderHTTPEndpoint struct {
URL string `yaml:"url"`
// Prefix enables to append a prefix to the metrics pushed to graphite
Prefix string `yaml:"prefix,omitempty"`
// HTTPClient holds the http client used to submit the metrics to the API
HTTPClient http.Client
}

type metricType string
Expand Down Expand Up @@ -83,7 +81,8 @@ func (g TelemetryProviderHTTPEndpoint) submitMetric(metric telemetryMetric) erro
if err != nil {
return err
}
resp, err := g.HTTPClient.Do(req)
c := http.Client{}
resp, err := c.Do(req)
if err != nil {
return fmt.Errorf("request POST %s failed. Response Error: '%s'", g.URL, err.Error())
}
Expand Down
9 changes: 3 additions & 6 deletions openapi/plugin_config_telemetry_provider_httpendpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ func TestTelemetryProviderHttpEndpointSubmitMetric(t *testing.T) {
defer api.Close()

tph := TelemetryProviderHTTPEndpoint{
URL: fmt.Sprintf("%s/v1/metrics", api.URL),
HTTPClient: *api.Client(),
URL: fmt.Sprintf("%s/v1/metrics", api.URL),
}
err := tph.submitMetric(expectedCounterMetric)
if tc.expectedErr == nil {
Expand Down Expand Up @@ -233,8 +232,7 @@ func TestTelemetryProviderHttpEndpointIncOpenAPIPluginVersionTotalRunsCounter(t
defer api.Close()

tph := TelemetryProviderHTTPEndpoint{
URL: fmt.Sprintf("%s/v1/metrics", api.URL),
HTTPClient: *api.Client(),
URL: fmt.Sprintf("%s/v1/metrics", api.URL),
}
err := tph.IncOpenAPIPluginVersionTotalRunsCounter("0.26.0")
if tc.expectedErr == nil {
Expand Down Expand Up @@ -280,8 +278,7 @@ func TestTelemetryProviderHttpEndpointIncServiceProviderTotalRunsCounter(t *test
defer api.Close()

tph := TelemetryProviderHTTPEndpoint{
URL: fmt.Sprintf("%s/v1/metrics", api.URL),
HTTPClient: *api.Client(),
URL: fmt.Sprintf("%s/v1/metrics", api.URL),
}
err := tph.IncServiceProviderTotalRunsCounter("cdn")
if tc.expectedErr == nil {
Expand Down

0 comments on commit 5c2c992

Please sign in to comment.