Skip to content

Commit

Permalink
added unit test and ran go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
okankoAMZ committed Oct 14, 2024
1 parent b67d022 commit 79e030f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 45 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ replace github.com/influxdata/telegraf => github.com/aws/telegraf v0.10.2-0.2024

// Replace with https://github.com/amazon-contributing/opentelemetry-collector-contrib, there are no requirements for all receivers/processors/exporters
// to be all replaced since there are some changes that will always be from upstream

replace (
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awscloudwatchlogsexporter => github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awscloudwatchlogsexporter v0.0.0-20240903195955-5944792b593a
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsemfexporter => github.com/amazon-contributing/opentelemetry-collector-contrib/exporter/awsemfexporter v0.0.0-20240903195955-5944792b593a
Expand Down
50 changes: 9 additions & 41 deletions plugins/inputs/prometheus/metrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
package prometheus

import (
"os"
"path/filepath"
"testing"

kitlog "github.com/go-kit/log"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/storage"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
)

func Test_metricAppender_Add_BadMetricName(t *testing.T) {
Expand Down Expand Up @@ -122,44 +123,11 @@ func Test_loadConfigFromFile(t *testing.T) {
logger.Log("reloaded")
return nil
}
err := reloadConfig(configFile, logger, true, reloader)
taManager := createTargetAllocatorManager(configFile, logger, nil, nil)
err := reloadConfig(configFile, logger, taManager, reloader)
assert.NoError(t, err)
}

func Test_TA_Labels(t *testing.T) {
mbCh := make(chan PrometheusMetricBatch, 3)
mr := metricsReceiver{pmbCh: mbCh}
ma := mr.Appender(nil)
var ts int64 = 10
var v = 10.0
ls := []labels.Label{
{Name: "__address__", Value: "192.168.20.37:8080"},
{Name: "__meta_kubernetes_namespace", Value: "default"},
{Name: "__meta_kubernetes_pod_container_id", Value: "containerd://b3a3dd8e4630a0c99fd65f4cffb8f2524394ec3e86dce2e94dced7dce082ea94"},
{Name: "__meta_kubernetes_pod_container_image", Value: "registry.k8s.io/kube-state-metrics/kube-state-metrics:v2.12.0"},
{Name: "__meta_kubernetes_pod_container_init", Value: "false"},
{Name: "__meta_kubernetes_pod_container_name", Value: "kube-state-metrics"},
{Name: "__meta_kubernetes_pod_container_port_name", Value: "http"},
{Name: "__meta_kubernetes_pod_container_port_number", Value: "8080"},
{Name: "__meta_kubernetes_pod_container_port_protocol", Value: "TCP"},
{Name: "__meta_kubernetes_pod_controller_kind", Value: "ReplicaSet"},
{Name: "__meta_kubernetes_pod_controller_name", Value: "example-kube-state-metrics-7446cc6b96"},
{Name: "__meta_kubernetes_pod_host_ip", Value: "192.168.0.41"},
{Name: "__meta_kubernetes_pod_ip", Value: "192.168.20.37"},
}

ref, err := ma.Append(0, ls, ts, v)
assert.Equal(t, ref, storage.SeriesRef(0))
assert.Nil(t, err)
mac, _ := ma.(*metricAppender)
assert.Equal(t, 1, len(mac.batch))
assert.True(t, taManager.enabled)
assert.Equal(t, taManager.config.TargetAllocator.CollectorID, "collector-1")
assert.Equal(t, taManager.config.TargetAllocator.TLSSetting.CAFile, DEFAULT_TLS_CA_FILE_PATH)

expected := PrometheusMetric{
metricName: "metric_name",
metricValue: v,
metricType: "",
timeInMS: ts,
tags: map[string]string{"tag_a": "a"},
}
assert.Equal(t, expected, *mac.batch[0])
}
5 changes: 4 additions & 1 deletion plugins/inputs/prometheus/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/prometheus/prometheus/discovery/targetgroup"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"

"github.com/prometheus/prometheus/discovery/targetgroup"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/oklog/run"
Expand Down Expand Up @@ -197,9 +198,11 @@ func Start(configFilePath string, receiver storage.Appendable, shutDownChan chan
close(reloadReady.C)
})
}
// @TODO: REMOVE BEFORE RELEASE
//scrapeIn := make(chan map[string][]*targetgroup.Group)
//go debugChannelWrapper(logger, discoveryManagerScrape.SyncCh(), scrapeIn)
//go debugScrapeManager(logger, scrapeManager)
//----
var g run.Group
{
// Termination handler.
Expand Down
10 changes: 8 additions & 2 deletions plugins/inputs/prometheus/target_allocator.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT

package prometheus

import (
"context"
"fmt"
"strings"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
otelpromreceiver "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver"
Expand All @@ -17,11 +22,12 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v3"
"strings"

"os"
)

const DEFAULT_TLS_CA_FILE_PATH = "/etc/amazon-cloudwatch-observability-agent-cert/tls-ca.crt"

type TargetAllocatorManager struct {
enabled bool
manager *tamanager.Manager
Expand Down Expand Up @@ -129,7 +135,7 @@ func (tam *TargetAllocatorManager) loadConfig(filename string) error {
return nil // no target allocator return
}
//has target allocator
tam.config.TargetAllocator.TLSSetting.CAFile = "/etc/amazon-cloudwatch-observability-agent-cert/tls-ca.crt"
tam.config.TargetAllocator.TLSSetting.CAFile = DEFAULT_TLS_CA_FILE_PATH
return nil
}
func (tam *TargetAllocatorManager) Start() error {
Expand Down

0 comments on commit 79e030f

Please sign in to comment.