Skip to content

Commit

Permalink
Merge pull request #8 from nouseforaname/add_scheme
Browse files Browse the repository at this point in the history
Add scheme
  • Loading branch information
DaspawnW authored Oct 9, 2020
2 parents 5e3ee5b + f79e90b commit c0a5e81
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 35 deletions.
16 changes: 12 additions & 4 deletions pkg/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,23 @@ func parseMetricEndpoint(key string, value string, prefix string) (*endpoints.In
parsedMetric := r.FindStringSubmatch(key)

if len(parsedMetric) == 3 {
parsedPort, err := strconv.ParseInt(parsedMetric[1], 10, 64)
//TO BE FAIR, NONE OF THIS IS A GOOD IDEA. THIS IS AT BEST A DIRTY AND FUGLY QUICK FIX. THIS SHOULD BE REFACTORED TO DO SOMETHING REASONABLE
var scheme = "http"
parsedString := strings.Split(parsedMetric[1], ":")
if len(parsedString) == 2 {
scheme = parsedString[1]
}

parsedPort, err := strconv.ParseInt(parsedString[0], 10, 64)
if err != nil {
return nil, err
}

return &endpoints.InstanceMetrics{
Name: value,
Path: parsedMetric[2],
Port: parsedPort,
Name: value,
Path: parsedMetric[2],
Port: parsedPort,
Scheme: scheme,
}, nil
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ func TestDiscoveryHasCorrectEndpoints(t *testing.T) {
if err != nil {
t.Error("Failed to discover instances", err)
}

expectedInstanceList := libtesting.InstanceList()

if !reflect.DeepEqual(expectedInstanceList, returnedInstanceList.Instances) {
t.Errorf("Expected instance list %v to equal returned instance list %v", expectedInstanceList, returnedInstanceList)
}
Expand Down
14 changes: 8 additions & 6 deletions pkg/endpoints/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ type Instance struct {
}

type InstanceMetrics struct {
Name string
Port int64
Path string
Name string
Port int64
Path string
Scheme string
}

type DiscoveredInstances struct {
Expand All @@ -28,13 +29,13 @@ type outputFormat struct {
Labels map[string]string `json:"labels"`
}

func ToJsonString(d DiscoveredInstances) ([]byte, error) {
func ToJSONString(d DiscoveredInstances) ([]byte, error) {
outputList := []outputFormat{}

for _, instance := range d.Instances {
for _, metric := range instance.Metrics {
targetHost := fmt.Sprintf("%s:%d", instance.PrivateIP, metric.Port)
l := labels(instance.Tags, targetHost, metric.Path, metric.Name)
l := labels(instance.Tags, targetHost, metric.Path, metric.Name, metric.Scheme)

output := outputFormat{Targets: []string{targetHost}, Labels: l}
outputList = append(outputList, output)
Expand All @@ -44,9 +45,10 @@ func ToJsonString(d DiscoveredInstances) ([]byte, error) {
return json.Marshal(outputList)
}

func labels(tags map[string]string, targetHost string, path string, metricName string) map[string]string {
func labels(tags map[string]string, targetHost string, path string, metricName string, scheme string) map[string]string {
labels := standardizeKeys(tags)
labels["__metrics_path__"] = path
labels["__scheme__"] = scheme
labels["__address__"] = targetHost
if val, ok := labels["name"]; ok {
labels["instancename"] = val
Expand Down
25 changes: 14 additions & 11 deletions pkg/endpoints/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ func TestToJsonStringHasCorrectContent(t *testing.T) {

instanceList := InstanceList()

returnedJSONContentBytes, _ := ToJsonString(DiscoveredInstances{Instances: instanceList})
expectedJSONContent := "[{\"targets\":[\"127.0.0.1:9100\"],\"labels\":{\"__address__\":\"127.0.0.1:9100\",\"__metrics_path__\":\"/metrics\",\"billingnumber\":\"1111\",\"instancename\":\"Testinstance1\",\"name\":\"node_exporter\",\"spotprice\":\"123\"}},{\"targets\":[\"127.0.0.1:8080\"],\"labels\":{\"__address__\":\"127.0.0.1:8080\",\"__metrics_path__\":\"/metrics\",\"billingnumber\":\"1111\",\"instancename\":\"Testinstance1\",\"name\":\"blackbox_exporter\",\"spotprice\":\"123\"}},{\"targets\":[\"127.0.0.2:9100\"],\"labels\":{\"__address__\":\"127.0.0.2:9100\",\"__metrics_path__\":\"/metrics\",\"billingnumber\":\"2222\",\"instancename\":\"Testinstance2\",\"name\":\"node_exporter\"}}]"
returnedJSONContentBytes, _ := ToJSONString(DiscoveredInstances{Instances: instanceList})
expectedJSONContent := "[{\"targets\":[\"127.0.0.1:9100\"],\"labels\":{\"__address__\":\"127.0.0.1:9100\",\"__metrics_path__\":\"/metrics\",\"__scheme__\":\"http\",\"billingnumber\":\"1111\",\"instancename\":\"Testinstance1\",\"name\":\"node_exporter\",\"spotprice\":\"123\"}},{\"targets\":[\"127.0.0.1:8080\"],\"labels\":{\"__address__\":\"127.0.0.1:8080\",\"__metrics_path__\":\"/metrics\",\"__scheme__\":\"https\",\"billingnumber\":\"1111\",\"instancename\":\"Testinstance1\",\"name\":\"blackbox_exporter\",\"spotprice\":\"123\"}},{\"targets\":[\"127.0.0.2:9100\"],\"labels\":{\"__address__\":\"127.0.0.2:9100\",\"__metrics_path__\":\"/metrics\",\"__scheme__\":\"http\",\"billingnumber\":\"2222\",\"instancename\":\"Testinstance2\",\"name\":\"node_exporter\"}}]"

if expectedJSONContent != string(returnedJSONContentBytes) {
t.Errorf("Expected json string with content %s, but got %s", expectedJSONContent, string(returnedJSONContentBytes))
Expand All @@ -27,14 +27,16 @@ func InstanceList() []Instance {
tagsI1["Spot price"] = "123"
metricsI1 := []InstanceMetrics{}
m1I1 := InstanceMetrics{
Name: "node_exporter",
Path: "/metrics",
Port: 9100,
Name: "node_exporter",
Path: "/metrics",
Port: 9100,
Scheme: "http",
}
m2I1 := InstanceMetrics{
Name: "blackbox_exporter",
Path: "/metrics",
Port: 8080,
Name: "blackbox_exporter",
Path: "/metrics",
Port: 8080,
Scheme: "https",
}
metricsI1 = append(metricsI1, m1I1, m2I1)
i1 := Instance{
Expand All @@ -52,9 +54,10 @@ func InstanceList() []Instance {
tagsI2[" "] = "empty"
metricsI2 := []InstanceMetrics{}
m1I2 := InstanceMetrics{
Name: "node_exporter",
Path: "/metrics",
Port: 9100,
Name: "node_exporter",
Path: "/metrics",
Port: 9100,
Scheme: "http",
}
metricsI2 = append(metricsI2, m1I2)
i2 := Instance{
Expand Down
23 changes: 13 additions & 10 deletions pkg/libtesting/libtesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ func InstanceList() []endpoints.Instance {
tagsI1["billingnumber"] = "1111"
metricsI1 := []endpoints.InstanceMetrics{}
m1I1 := endpoints.InstanceMetrics{
Name: "node_exporter",
Path: "/metrics",
Port: 9100,
Name: "node_exporter",
Path: "/metrics",
Port: 9100,
Scheme: "http",
}
m2I1 := endpoints.InstanceMetrics{
Name: "blackbox_exporter",
Path: "/metrics",
Port: 8080,
Name: "blackbox_exporter",
Path: "/metrics",
Port: 8080,
Scheme: "https",
}
metricsI1 = append(metricsI1, m1I1, m2I1)
i1 := endpoints.Instance{
Expand All @@ -39,9 +41,10 @@ func InstanceList() []endpoints.Instance {
tagsI2["billingnumber"] = "2222"
metricsI2 := []endpoints.InstanceMetrics{}
m1I2 := endpoints.InstanceMetrics{
Name: "node_exporter",
Path: "/metrics",
Port: 9100,
Name: "node_exporter",
Path: "/metrics",
Port: 9100,
Scheme: "http",
}
metricsI2 = append(metricsI2, m1I2)
i2 := endpoints.Instance{
Expand All @@ -64,7 +67,7 @@ func EC2InstanceList() []*ec2.Instance {
Value: aws.String("node_exporter"),
}
t12 := ec2.Tag{
Key: aws.String("prom/scrape:8080/metrics"),
Key: aws.String("prom/scrape:8080:https/metrics"),
Value: aws.String("blackbox_exporter"),
}
nameTag1 := ec2.Tag{
Expand Down
2 changes: 1 addition & 1 deletion pkg/outputfile/outputfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type OutputFile struct {
}

func (o OutputFile) Write(instances endpoints.DiscoveredInstances) error {
content, err := endpoints.ToJsonString(instances)
content, err := endpoints.ToJSONString(instances)
if err != nil {
log.Error("Failed to convert instances to json string with error", err)
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/outputkubernetes/outputkubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewOutputKubernetes(kubeconfig string, namespace string, cmName string, cmF
}

func (o OutputKubernetes) Write(instances endpoints.DiscoveredInstances) error {
output, err := endpoints.ToJsonString(instances)
output, err := endpoints.ToJSONString(instances)
if err != nil {
log.Error("Failed to convert instances to json string")
return err
Expand Down

0 comments on commit c0a5e81

Please sign in to comment.