Skip to content

Add metrics instrumentation#24

Merged
terryyylim merged 5 commits into
mainfrom
add-metrics-instrumentation
Jul 8, 2021
Merged

Add metrics instrumentation#24
terryyylim merged 5 commits into
mainfrom
add-metrics-instrumentation

Conversation

@terryyylim
Copy link
Copy Markdown
Contributor

This PR defines an interface for the metrics collector and provides concrete implementations for Nop and Prometheus.

@terryyylim terryyylim requested a review from krithika369 July 7, 2021 06:51
Comment thread api/internal/testutils/validation.go Outdated
import "testing"

// FailOnError logs the error and terminates the test immediately
func FailOnError(t *testing.T, err error) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The require package can be used in place of this. Some of the code in the original repo may need to be updated (out of the scope of this PR).


// InitMetricsCollector is used to select the appropriate metrics collector and
// set up the required values for instrumenting.
func InitMetricsCollector(enabled bool) error {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this InitMetricsCollector can be removed. We can have an InitNopMetricsCollector (effectively does nothing) and an InitPrometheusMetricsCollector (should take in the histogram map and whatever else we add in the future as an argument). Both of them should eventually do SetGlobMetricsCollector. But the decision on which one to call can be left to the caller.

In general, the code in pkg should not depend on anything outside. The dependency on api/log could be an issue if this module is being used elsewhere. But if we break this function down as suggested above, the logging can happen from the caller.

}
// Initialize
histogramMap := make(map[MetricName]*prometheus.HistogramVec)
globalMetricsCollector.InitMetrics(histogramMap)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be passed in from the caller.

Comment thread api/pkg/instrumentation/metrics/nop.go Outdated
}

// InitMetrics satisfies the Collector interface
func (NopMetricsCollector) InitMetrics(map[MetricName]*prometheus.HistogramVec) {}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Referring to the Prometheus library in this file doesn't quite make sense, right? In the future, for esample, there can be a Statsd Metrics collector. metrics.go and nop.go should not be concerned with individual collectors related functionality.

Copy link
Copy Markdown
Collaborator

@krithika369 krithika369 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left one minor comment for change. The rest looks good! Thanks for this MR.


func InitPrometheusMetricsCollector(histogramMap map[MetricName]*prometheus.HistogramVec) error {
SetGlobMetricsCollector(&PrometheusClient{histogramMap: histogramMap})
globalMetricsCollector.InitMetrics()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the InitMetrics method is now redundant - its functionality can simply be merged into InitPrometheusMetricsCollector and InitMetrics can simply be removed from the interface.

return &NopMetricsCollector{}
}

func InitNopMetricsCollector() error {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to add error to the signature here and in InitPrometheusMetricsCollector.

@terryyylim terryyylim merged commit 1d8bc50 into main Jul 8, 2021
@terryyylim terryyylim deleted the add-metrics-instrumentation branch July 8, 2021 01:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants