-
Notifications
You must be signed in to change notification settings - Fork 894
feat: expose template insights as Prometheus metrics #10325
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
24e4ff3
WIP
mtojek a6c9c1a
Use db
mtojek 86f45a0
debug
mtojek bfcd8f4
Merge branch 'main' into 9983-insights-metrics
mtojek a3c29a7
DB calls
mtojek 24ae332
WIP
mtojek 0cb1206
WIP dbfake
mtojek 3c31168
Merge branch 'main' into 9983-insights-metrics
mtojek a764cef
Collect metrics data
mtojek 32f5245
Only template
mtojek 0c81a7a
Fixes
mtojek 432af16
fmt
mtojek 5ebbfe9
fix: dbauthz
mtojek f4c1202
Unit test
mtojek 62ad6a4
golden file
mtojek 85dc078
Fix
mtojek 2d03fed
Fix?
mtojek e41bfbc
t.cleanup to defer
mtojek cd8a809
ticker.stop
mtojek 3dbb6eb
GROUP BY alias
mtojek c25c499
Preallocate
mtojek 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 hidden or 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 hidden or 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,72 @@ | ||
package insights | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"github.com/coder/coder/v2/coderd/database" | ||
) | ||
|
||
var ( | ||
activeUsersDesc = prometheus.NewDesc("coderd_insights_active_users", "The number of active users of the template.", []string{"template_name"}, nil) | ||
applicationsUsageSecondsDesc = prometheus.NewDesc("coderd_insights_applications_usage_seconds", "The application usage per template.", []string{"application_name", "template_name"}, nil) | ||
parametersDesc = prometheus.NewDesc("coderd_insights_parameters", "The parameter usage per template.", []string{"template_name", "name", "value"}, nil) | ||
) | ||
|
||
type MetricsCollector struct { | ||
database database.Store | ||
duration time.Duration | ||
} | ||
|
||
var _ prometheus.Collector = new(MetricsCollector) | ||
|
||
func NewMetricsCollector(db database.Store, duration time.Duration) *MetricsCollector { | ||
if duration == 0 { | ||
duration = 5 * time.Minute | ||
} | ||
|
||
return &MetricsCollector{ | ||
database: db, | ||
duration: duration, | ||
} | ||
} | ||
|
||
func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) { | ||
ctx, closeFunc := context.WithCancel(ctx) | ||
done := make(chan struct{}) | ||
|
||
// Use time.Nanosecond to force an initial tick. It will be reset to the | ||
// correct duration after executing once. | ||
ticker := time.NewTicker(time.Nanosecond) | ||
doTick := func() { | ||
defer ticker.Reset(mc.duration) | ||
} | ||
|
||
go func() { | ||
defer close(done) | ||
defer ticker.Stop() | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
doTick() | ||
} | ||
} | ||
}() | ||
return func() { | ||
closeFunc() | ||
<-done | ||
}, nil | ||
} | ||
|
||
func (*MetricsCollector) Describe(descCh chan<- *prometheus.Desc) { | ||
descCh <- activeUsersDesc | ||
descCh <- applicationsUsageSecondsDesc | ||
descCh <- parametersDesc | ||
} | ||
|
||
func (mc *MetricsCollector) Collect(metricsCh chan<- prometheus.Metric) { | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.