Skip to content

Commit

Permalink
Rename worker to exporter, add more words to doc.go
Browse files Browse the repository at this point in the history
  • Loading branch information
jjti committed Dec 20, 2023
1 parent 31b85d6 commit 132d863
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/consuldp/consul_dataplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type ConsulDataplane struct {
aclToken string
metricsConfig *metricsConfig
lifecycleConfig *lifecycleConfig
hcpTelemetry *telemetry.Worker
hcpTelemetry *telemetry.Exporter
}

// NewConsulDP creates a new instance of ConsulDataplane
Expand Down
27 changes: 26 additions & 1 deletion pkg/hcp/telemetry/doc.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
// Package telemetry is for pushing Envoy metrics to HCP.
// Package telemetry is for scraping and sending Envoy metrics to HCP in OTLP format.
//
// This exists to let customers send mesh telemetry to HCP without having to deploy
// a dedicated telemetry collector. Configuration for the collection, filtering, and exporting
// of Envoy metrics is managed in the hcp.v1.TelemetryState resource in Consul.
//
// ┌───────────────────┐
// │ │
// │ HCP telemetry API │
// │ │
// └───────────▲────--─┘
// │POST /v1/metrics
// ┌──────────┐WatchList┌────--─────┴─----─┐
// │ │◄────────┤ │
// │ consul │ │ consul-dataplane │
// │ ├────────►│ │
// └──────────┘ └─────────────┬────┘
// hcp.v1.TelemetryState │GET /stats/prometheus
// ┌─────────────▼────┐
// │ │
// │ envoy admin │
// │ │
// └──────────────────┘
//
// A lot of the package is concerned with converting prometheus-format metrics into their
// OTLP equivalent.
package telemetry
14 changes: 9 additions & 5 deletions pkg/hcp/telemetry/worker.go → pkg/hcp/telemetry/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@ import (

const workerDefaultScrapeInterval = time.Minute

type Worker struct {
// Exporter is a telemetry exporter that is specific to HCP. If enabled via the hcp.v1.TelemetryState resource,
// this exporter periodically scrapes Envoy metrics from its admin endpoint and pushes the metrics up to HCP.
type Exporter struct {
logger hclog.Logger
scrapeInterval time.Duration
scraper scraper
stateTracker stateTracker
}

func New(resourceClient pbresource.ResourceServiceClient, logger hclog.Logger, envoyAdminHostPort string) *Worker {
return &Worker{
// New creates a new HCP telemetry exporter.
func New(resourceClient pbresource.ResourceServiceClient, logger hclog.Logger, envoyAdminHostPort string) *Exporter {
return &Exporter{
logger: logger,
scrapeInterval: workerDefaultScrapeInterval,
scraper: newScraper(envoyAdminHostPort, logger.Named("scraper")),
stateTracker: newStateTracker(resourceClient, logger.Named("state_tracker")),
}
}

func (w *Worker) Run(ctx context.Context) {
// Run starts the exporter's worker goroutine that periodically scrapes Envoy metrics and pushes them to HCP.
func (w *Exporter) Run(ctx context.Context) {
go w.stateTracker.Run(ctx) // start syncing state from consul.
ticker := time.NewTicker(w.scrapeInterval)

Expand All @@ -50,7 +54,7 @@ func (w *Worker) Run(ctx context.Context) {
continue
}

if err := state.exporter.ExportMetrics(ctx, metrics); err != nil {
if err := state.client.ExportMetrics(ctx, metrics); err != nil {
w.logger.Error("failed to export metrics", "error", err)
}
case <-ctx.Done():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ func (s *fakeStateTracker) GetState() (*state, bool) {
return nil, false
}

type fakeExporter struct {
type fakeClient struct {
exportCalled atomic.Bool
exportErr error
}

func (e *fakeExporter) ExportMetrics(ctx context.Context, metrics pmetric.Metrics) error {
func (e *fakeClient) ExportMetrics(ctx context.Context, metrics pmetric.Metrics) error {
e.exportCalled.Store(true)

if e.exportErr != nil {
Expand Down Expand Up @@ -107,11 +107,11 @@ func Test_Worker(t *testing.T) {
worker.scrapeInterval = time.Millisecond * 10

// Create fake exporter and state.
exporter := &fakeExporter{
exporter := &fakeClient{
exportErr: tc.exportErr,
}
state := &state{
exporter: exporter,
client: exporter,
disabled: false,
labels: map[string]string{
"foo": "bar",
Expand Down
4 changes: 2 additions & 2 deletions pkg/hcp/telemetry/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
//
// All of this is sourced from Consul that, in turn, gets it from HCP.
type state struct {
exporter otlphttp.Client
client otlphttp.Client
labels map[string]string
disabled bool
metricsIncludeList []string
Expand Down Expand Up @@ -168,7 +168,7 @@ func (st *stateTrackerImpl) resourceToState(res *pbresource.Resource) (*state, e

// TODO replace with actual proto conversion from data
return &state{
exporter: exporter,
client: exporter,
labels: map[string]string{},
disabled: false,
metricsIncludeList: []string{".+"},
Expand Down

0 comments on commit 132d863

Please sign in to comment.