diff --git a/client.go b/client.go index 99c13ad..9df4a73 100644 --- a/client.go +++ b/client.go @@ -1,6 +1,7 @@ package diego_logging_client import ( + "cmp" "fmt" "time" @@ -11,6 +12,7 @@ import ( // Config is the shared configuration between v1 and v2 clients. type Config struct { UseV2API bool `json:"loggregator_use_v2_api"` + APIHost string `json:"loggregator_api_host"` APIPort int `json:"loggregator_api_port"` CACertPath string `json:"loggregator_ca_path"` CertPath string `json:"loggregator_cert_path"` @@ -29,6 +31,10 @@ type Config struct { AppMetricExclusionFilter []string `json:"loggregator_app_metric_exclusion_filter"` } +func (c Config) APIAddr() string { + return fmt.Sprintf("%s:%d", cmp.Or(c.APIHost, "127.0.0.1"), c.APIPort) +} + // A ContainerMetric records resource usage of an app in a container. type ContainerMetric struct { ApplicationId string //deprecated @@ -107,7 +113,7 @@ func newV2IngressClient(config Config) (IngressClient, error) { } if config.APIPort != 0 { - opts = append(opts, loggregator.WithAddr(fmt.Sprintf("127.0.0.1:%d", config.APIPort))) + opts = append(opts, loggregator.WithAddr(config.APIAddr())) } //lint:ignore SA1019 - we can't use grpc.WithContextDial until loggregator is updated for grpc.DialContext diff --git a/client_test.go b/client_test.go index a7db939..27d95a5 100644 --- a/client_test.go +++ b/client_test.go @@ -33,6 +33,22 @@ var _ = Describe("DiegoLoggingClient", func() { metronClientKeyFile = filepath.Join(fixturesPath, "metron", "client.key") }) + Context("Config", func() { + It("returns the configured API address", func() { + config := client.Config{ + APIHost: "api.example.com", + APIPort: 8080, + } + Expect(config.APIAddr()).To(Equal("api.example.com:8080")) + }) + It("returns the default API address when no host is specified", func() { + config := client.Config{ + APIPort: 8080, + } + Expect(config.APIAddr()).To(Equal("127.0.0.1:8080")) + }) + }) + Context("when the v2 api is used", func() { var ( testIngressServer *testhelpers.TestIngressServer