Skip to content

Commit

Permalink
Add node.id label
Browse files Browse the repository at this point in the history
  • Loading branch information
jjti committed Feb 1, 2024
1 parent 685aaa9 commit 2d3743e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
7 changes: 6 additions & 1 deletion pkg/consuldp/consul_dataplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@ func (cdp *ConsulDataplane) Run(ctx context.Context) error {
return err
}

cdp.hcpTelemetry = telemetry.New(cdp.resourceClient, cdp.logger.Named("hcp_telemetry"), fmt.Sprintf("%s:%d", cdp.cfg.Envoy.AdminBindAddress, cdp.cfg.Envoy.AdminBindPort))
cdp.hcpTelemetry = telemetry.NewHCPExporter(
cdp.resourceClient,
cdp.logger.Named("hcp_telemetry"),
fmt.Sprintf("%s:%d", cdp.cfg.Envoy.AdminBindAddress, cdp.cfg.Envoy.AdminBindPort),
cdp.cfg.Proxy.ProxyID,
)
go cdp.hcpTelemetry.Run(ctx)

doneCh := make(chan error)
Expand Down
7 changes: 5 additions & 2 deletions pkg/hcp/telemetry/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ const exporterDefaultScrapeInterval = time.Minute
// Exporter is a telemetry exporter that is specific to HCP. If enabled via the hcp.v2.TelemetryState resource,
// this exporter periodically scrapes Envoy metrics from its admin endpoint and pushes the metrics up to HCP.
type Exporter struct {
envoyProxyID string
logger hclog.Logger
scrapeInterval time.Duration
scraper scraper
stateTracker stateTracker
}

// New creates a new HCP telemetry exporter.
func New(resourceClient pbresource.ResourceServiceClient, logger hclog.Logger, envoyAdminHostPort string) *Exporter {
// NewHCPExporter creates a new HCP telemetry exporter.
func NewHCPExporter(resourceClient pbresource.ResourceServiceClient, logger hclog.Logger, envoyAdminHostPort, envoyProxyID string) *Exporter {
return &Exporter{
envoyProxyID: envoyProxyID,
logger: logger,
scrapeInterval: exporterDefaultScrapeInterval,
scraper: newScraper(envoyAdminHostPort, logger.Named("scraper")),
Expand All @@ -48,6 +50,7 @@ func (w *Exporter) Run(ctx context.Context) {
for k, v := range state.labels {
labels.PutStr(k, v)
}
labels.PutStr("node.id", w.envoyProxyID)

metrics, err := w.scraper.scrape(ctx, state.includeList, labels)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion pkg/hcp/telemetry/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-uuid"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
Expand Down Expand Up @@ -50,6 +51,7 @@ func (s *fakeStateTracker) GetState() (*state, bool) {

type fakeClient struct {
exportCalled atomic.Bool
exportLabels atomic.Value
exportErr error
}

Expand Down Expand Up @@ -103,7 +105,9 @@ func Test_Worker(t *testing.T) {
r := require.New(t)

// Create a worker. We don't use the client or envoy admin addr for anything.
worker := New(NewMockResourceServiceClient(t), hclog.NewNullLogger(), "localhost:1234")
proxyID, err := uuid.GenerateUUID()
r.NoError(err)
worker := NewHCPExporter(NewMockResourceServiceClient(t), hclog.NewNullLogger(), "localhost:1234", proxyID)
worker.scrapeInterval = time.Millisecond * 10

// Create fake exporter and state.
Expand Down

0 comments on commit 2d3743e

Please sign in to comment.