-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: separate metrics code for reuse (#13514)
Signed-off-by: Alan Clucas <[email protected]>
- Loading branch information
Showing
41 changed files
with
781 additions
and
594 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package telemetry | ||
|
||
const ( | ||
AttribBuildVersion string = `version` | ||
AttribBuildPlatform string = `platform` | ||
AttribBuildGoVersion string = `go_version` | ||
AttribBuildDate string = `build_date` | ||
AttribBuildCompiler string = `compiler` | ||
AttribBuildGitCommit string = `git_commit` | ||
AttribBuildGitTreeState string = `git_treestate` | ||
AttribBuildGitTag string = `git_tag` | ||
|
||
AttribCronWFName string = `name` | ||
|
||
AttribErrorCause string = "cause" | ||
|
||
AttribLogLevel string = `level` | ||
|
||
AttribNodePhase string = `node_phase` | ||
|
||
AttribPodPhase string = `phase` | ||
AttribPodNamespace string = `namespace` | ||
AttribPodPendingReason string = `reason` | ||
|
||
AttribQueueName string = `queue_name` | ||
|
||
AttribRecentlyStarted string = `recently_started` | ||
|
||
AttribRequestKind = `kind` | ||
AttribRequestVerb = `verb` | ||
AttribRequestCode = `status_code` | ||
|
||
AttribTemplateName string = `name` | ||
AttribTemplateNamespace string = `namespace` | ||
AttribTemplateCluster string = `cluster_scope` | ||
|
||
AttribWorkerType string = `worker_type` | ||
|
||
AttribWorkflowNamespace string = `namespace` | ||
AttribWorkflowPhase string = `phase` | ||
AttribWorkflowStatus = `status` | ||
AttribWorkflowType = `type` | ||
) |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/otel/sdk/metric" | ||
) | ||
|
||
func createDefaultTestMetrics() (*Metrics, *TestMetricsExporter, error) { | ||
config := Config{ | ||
Enabled: true, | ||
} | ||
return createTestMetrics(&config) | ||
} | ||
|
||
func createTestMetrics(config *Config) (*Metrics, *TestMetricsExporter, error) { | ||
ctx /* with cancel*/ := context.Background() | ||
te := NewTestMetricsExporter() | ||
|
||
m, err := NewMetrics(ctx, TestScopeName, TestScopeName, config, metric.WithReader(te)) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
err = m.Populate(ctx, AddVersion, addTestingCounter, addTestingHistogram) | ||
return m, te, err | ||
} | ||
|
||
const ( | ||
nameTestingHistogram = `testing_histogram` | ||
nameTestingCounter = `testing_counter` | ||
errorCauseTestingA = "TestingA" | ||
errorCauseTestingB = "TestingB" | ||
) | ||
|
||
func addTestingHistogram(_ context.Context, m *Metrics) error { | ||
// The buckets here are only the 'defaults' and can be overridden with configmap defaults | ||
return m.CreateInstrument(Float64Histogram, | ||
nameTestingHistogram, | ||
"Testing Metric", | ||
"s", | ||
WithDefaultBuckets([]float64{0.0, 1.0, 5.0, 10.0}), | ||
WithAsBuiltIn(), | ||
) | ||
} | ||
|
||
func (m *Metrics) TestingHistogramRecord(ctx context.Context, value float64) { | ||
m.Record(ctx, nameTestingHistogram, value, InstAttribs{}) | ||
} | ||
|
||
func addTestingCounter(ctx context.Context, m *Metrics) error { | ||
return m.CreateInstrument(Int64Counter, | ||
nameTestingCounter, | ||
"Testing Error Counting Metric", | ||
"{errors}", | ||
WithAsBuiltIn(), | ||
) | ||
} | ||
|
||
func (m *Metrics) TestingErrorA(ctx context.Context) { | ||
m.AddInt(ctx, nameTestingCounter, 1, InstAttribs{{Name: AttribErrorCause, Value: errorCauseTestingB}}) | ||
} | ||
|
||
func (m *Metrics) TestingErrorB(ctx context.Context) { | ||
m.AddInt(ctx, nameTestingCounter, 1, InstAttribs{{Name: AttribErrorCause, Value: errorCauseTestingB}}) | ||
} |
Oops, something went wrong.