-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[HCP Observability] Metrics filtering and Labels in Go Metrics sink #17184
Merged
Achooo
merged 104 commits into
feature/hcp-telemetry
from
cc-4361-cc-4742/labels-filters
May 24, 2023
Merged
Changes from all commits
Commits
Show all changes
104 commits
Select commit
Hold shift + click to select a range
b6381b7
Move hcp client to subpackage hcpclient (#16800)
Achooo 5b82711
[HCP Observability] New MetricsClient (#17100)
Achooo 8a16311
[HCP Observability] New MetricsClient (#17100)
Achooo c7945b3
[HCP Observability] New MetricsClient (#17100)
Achooo e4f0761
Client configured with TLS using HCP config and retry/throttle
Achooo 8bbc289
run go mod tidy
Achooo f6182b4
Remove one abstraction to use the config from deps
Achooo 009f08e
Address PR feedback
Achooo 41ba7ee
Client configured with TLS using HCP config and retry/throttle
Achooo 8fd34e4
run go mod tidy
Achooo 894cef4
Create new OTELExporter which uses the MetricsClient
Achooo b50057c
Fix lint error
Achooo 0c01542
early return when there are no metrics
Achooo da20fe3
Add NewOTELExporter() function
Achooo 749b1c8
Downgrade to metrics SDK version: v1.15.0-rc.1
Achooo 383b366
Fix imports
Achooo 1d222b1
fix small nits with comments and url.URL
Achooo 5564bce
Fix tests by asserting actual error for context cancellation, fix par…
Achooo 424a065
Cleanup error handling and clarify empty metrics case
Achooo 470a11d
Fix input/expected naming in otel_transform_test.go
Achooo be0b01b
add comment for metric tracking
Achooo 325bb4d
Add a general isEmpty method
Achooo a0352ac
Add clear error types
Achooo 2d356f6
update to latest version 1.15.0 of OTEL
Achooo 22bb2ee
Client configured with TLS using HCP config and retry/throttle
Achooo 22be78f
run go mod tidy
Achooo 478181a
Remove one abstraction to use the config from deps
Achooo c2ffaab
Address PR feedback
Achooo 864e6d7
Initialize OTELSink with sync.Map for all the instrument stores.
Achooo 05c418b
Moved PeriodicReader init to NewOtelReader function. This allows us t…
Achooo 72ae205
Switch to mutex instead of sync.Map to avoid type assertion
Achooo 83fba0a
Add gauge store
Achooo 520ba9f
Clarify comments
Achooo 190ef2a
return concrete sink type
Achooo 659a7dd
Fix lint errors
Achooo 9659a87
Move gauge store to be within sink
Achooo 80e01c7
Use context.TODO,rebase and clenaup opts handling
Achooo 7cbed58
Rebase onto otl exporter to downgrade metrics API to v1.15.0-rc.1
Achooo 91fcfc7
Fix imports
Achooo 48d69e3
Update to latest stable version by rebasing on cc-4933, fix import, r…
Achooo 563330e
Add lots of documentation to the OTELSink
Achooo b98481d
Fix gauge store comment and check ok
Achooo 0162bb6
Add select and ctx.Done() check to gauge callback
Achooo 899dbba
use require.Equal for attributes
Achooo 542d23a
Fixed import naming
Achooo 2d8a18a
Remove float64 calls and add a NewGaugeStore method
Achooo 5defe6a
Change name Store to Set in gaugeStore, add concurrency tests in both…
Achooo a893c32
Generate 100 gauge operations
Achooo 9d5f5ef
Seperate the labels into goroutines in sink test
Achooo 80a534b
Generate kv store for the test case keys to avoid using uuid
Achooo aa2a971
Added a race test with 300 samples for OTELSink
Achooo 91f9a89
[HCP Observability] OTELExporter (#17128)
Achooo e3e8d3f
Merge and fix conflicts
Achooo d13849b
Do not pass in waitgroup and use error channel instead.
Achooo 713c5fa
Using SHA 7dea2225a218872e86d2f580e82c089b321617b0 to avoid build fai…
Achooo c6f2f9c
Rebase onto otl exporter to downgrade metrics API to v1.15.0-rc.1
Achooo f2c0d7a
Initialize OTELSink with sync.Map for all the instrument stores.
Achooo d884daf
Added telemetry agent to client and init sink in deps
Achooo bf496f3
Fixed client
Achooo 4a047b0
Initalize sink in deps
Achooo 49dfd64
init sink in telemetry library
Achooo e58ba1e
Init deps before telemetry
Achooo 5cd9e2e
Use concrete telemetry.OtelSink type
Achooo 26bfd94
add /v1/metrics
Achooo 09b498c
Avoid returning err for telemetry init
Achooo 4604fd9
move sink init within the IsCloudEnabled()
Achooo 5f8c33e
Use HCPSinkOpts in deps instead
Achooo 675a494
update golden test for configuration file
Achooo f9b8e59
Switch to using extra sinks in the telemetry library
Achooo b07c731
keep name MetricsConfig
Achooo e6f367f
fix log in verifyCCMRegistration
Achooo e918544
Set logger in context
Achooo d357cb5
pass around MetricSink in deps
Achooo 3e79438
Fix imports
Achooo 3d57ff5
Rebased onto otel sink pr
Achooo dc48f65
Fix URL in test
Achooo 218a4f6
[HCP Observability] OTELSink (#17159)
Achooo 4f7daed
Merge branch feature/hcp-telemetry
Achooo 19b1d0c
pass extraSinks as function param instead
Achooo 8ae6af6
Add default interval as package export
Achooo f2ef9c7
remove verifyCCM func
Achooo ca41a33
Add clusterID
Achooo 52a1ee4
Fix import and add t.Parallel() for missing tests
Achooo e7f6d8b
merge feature/hcp-telemetry
Achooo fc86337
Kick Vercel CI
Achooo d0dbecb
Remove scheme from endpoint path, and fix error logging
Achooo de0deb9
return metrics.MetricSink for sink method
Achooo 58e838a
Update SDK
Achooo 60d450d
Added telemetry agent to client and init sink in deps
Achooo 6ac7c5e
Add node_id and __replica__ default labels
Achooo 8189fae
add function for default labels and set x-hcp-resource-id
Achooo a6f8e69
Fix labels tests
Achooo e014313
Commit suggestion for getDefaultLabels
Achooo 2726472
Fixed server.id, and t.Parallel()
Achooo eb7bef6
Make defaultLabels a method on the TelemetryConfig object
Achooo a66ded2
Rename FilterList to lowercase filterList
Achooo 6f70376
Cleanup filter implemetation by combining regex into a single one, an…
Achooo dec07b2
Fix append
Achooo 7facba6
use regex directly for filters
Achooo 5692d32
Fix x-resource-id test to use mocked value
Achooo c1575e5
Fix log.Error formats
Achooo 31a0aed
Forgot the len(opts.Label) optimization)
Achooo 24f1ebc
Use cfg.NodeID instead
Achooo 1dd5e2b
merge feature/hcp-telemetry
Achooo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import ( | |
"github.com/hashicorp/consul/agent/hcp/config" | ||
"github.com/hashicorp/consul/agent/hcp/scada" | ||
"github.com/hashicorp/consul/agent/hcp/telemetry" | ||
"github.com/hashicorp/consul/types" | ||
"github.com/hashicorp/go-hclog" | ||
) | ||
|
||
|
@@ -23,7 +24,7 @@ type Deps struct { | |
Sink metrics.MetricSink | ||
} | ||
|
||
func NewDeps(cfg config.CloudConfig, logger hclog.Logger) (d Deps, err error) { | ||
func NewDeps(cfg config.CloudConfig, logger hclog.Logger, nodeID types.NodeID) (d Deps, err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to pass in the NodeID here to set the labels |
||
d.Client, err = hcpclient.NewClient(cfg) | ||
if err != nil { | ||
return | ||
|
@@ -34,15 +35,15 @@ func NewDeps(cfg config.CloudConfig, logger hclog.Logger) (d Deps, err error) { | |
return | ||
} | ||
|
||
d.Sink = sink(d.Client, &cfg, logger) | ||
d.Sink = sink(d.Client, &cfg, logger, nodeID) | ||
|
||
return | ||
} | ||
|
||
// sink provides initializes an OTELSink which forwards Consul metrics to HCP. | ||
// The sink is only initialized if the server is registered with the management plane (CCM). | ||
// This step should not block server initialization, so errors are logged, but not returned. | ||
func sink(hcpClient hcpclient.Client, cfg hcpclient.CloudConfig, logger hclog.Logger) metrics.MetricSink { | ||
func sink(hcpClient hcpclient.Client, cfg hcpclient.CloudConfig, logger hclog.Logger, nodeID types.NodeID) metrics.MetricSink { | ||
ctx := context.Background() | ||
ctx = hclog.WithContext(ctx, logger) | ||
|
||
|
@@ -73,8 +74,10 @@ func sink(hcpClient hcpclient.Client, cfg hcpclient.CloudConfig, logger hclog.Lo | |
} | ||
|
||
sinkOpts := &telemetry.OTELSinkOpts{ | ||
Ctx: ctx, | ||
Reader: telemetry.NewOTELReader(metricsClient, u, telemetry.DefaultExportInterval), | ||
Ctx: ctx, | ||
Reader: telemetry.NewOTELReader(metricsClient, u, telemetry.DefaultExportInterval), | ||
Labels: telemetryCfg.DefaultLabels(string(nodeID)), | ||
Filters: telemetryCfg.MetricsConfig.Filters, | ||
} | ||
|
||
sink, err := telemetry.NewOTELSink(sinkOpts) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package telemetry | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
) | ||
|
||
// newFilterRegex returns a valid regex used to filter metrics. | ||
// It will fail if there are 0 valid regex filters given. | ||
func newFilterRegex(filters []string) (*regexp.Regexp, error) { | ||
var mErr error | ||
validFilters := make([]string, 0, len(filters)) | ||
for _, filter := range filters { | ||
_, err := regexp.Compile(filter) | ||
if err != nil { | ||
mErr = multierror.Append(mErr, fmt.Errorf("compilation of filter %q failed: %w", filter, err)) | ||
continue | ||
} | ||
validFilters = append(validFilters, filter) | ||
} | ||
|
||
if len(validFilters) == 0 { | ||
return nil, multierror.Append(mErr, fmt.Errorf("no valid filters")) | ||
} | ||
|
||
// Combine the valid regex strings with an OR. | ||
finalRegex := strings.Join(validFilters, "|") | ||
composedRegex, err := regexp.Compile(finalRegex) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to compile regex: %w", err) | ||
} | ||
|
||
return composedRegex, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package telemetry | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFilter(t *testing.T) { | ||
t.Parallel() | ||
for name, tc := range map[string]struct { | ||
filters []string | ||
expectedRegexString string | ||
matches []string | ||
wantErr string | ||
wantMatch bool | ||
}{ | ||
"badFilterRegex": { | ||
filters: []string{"(*LF)"}, | ||
wantErr: "no valid filters", | ||
}, | ||
"failsWithNoRegex": { | ||
filters: []string{}, | ||
wantErr: "no valid filters", | ||
}, | ||
"matchFound": { | ||
filters: []string{"raft.*", "mem.*"}, | ||
expectedRegexString: "raft.*|mem.*", | ||
matches: []string{"consul.raft.peers", "consul.mem.heap_size"}, | ||
wantMatch: true, | ||
}, | ||
"matchNotFound": { | ||
filters: []string{"mem.*"}, | ||
matches: []string{"consul.raft.peers", "consul.txn.apply"}, | ||
expectedRegexString: "mem.*", | ||
wantMatch: false, | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
f, err := newFilterRegex(tc.filters) | ||
|
||
if tc.wantErr != "" { | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), tc.wantErr) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, tc.expectedRegexString, f.String()) | ||
for _, metric := range tc.matches { | ||
m := f.MatchString(metric) | ||
require.Equal(t, tc.wantMatch, m) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set default
x-hcp-resource-id
which should be generated using the Go HCP SDK.