Skip to content

Add monitoring package #1446

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

Closed
wants to merge 10 commits into from
27 changes: 21 additions & 6 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/coder/coder/coderd/database/databasefake"
"github.com/coder/coder/coderd/devtunnel"
"github.com/coder/coder/coderd/gitsshkey"
"github.com/coder/coder/coderd/monitoring"
"github.com/coder/coder/coderd/tracing"
"github.com/coder/coder/coderd/turnconn"
"github.com/coder/coder/codersdk"
Expand Down Expand Up @@ -80,6 +81,7 @@ func server() *cobra.Command {
oauth2GithubClientSecret string
oauth2GithubAllowedOrganizations []string
oauth2GithubAllowSignups bool
telemetryRaw string
tlsCertFile string
tlsClientCAFile string
tlsClientAuth string
Expand Down Expand Up @@ -112,7 +114,7 @@ func server() *cobra.Command {
sqlDriver = "postgres"
)
if trace {
tracerProvider, err = tracing.TracerProvider(cmd.Context(), "coderd")
tracerProvider, err = tracing.Provider(cmd.Context(), "coderd")
if err != nil {
logger.Warn(cmd.Context(), "failed to start telemetry exporter", slog.Error(err))
} else {
Expand Down Expand Up @@ -243,6 +245,11 @@ func server() *cobra.Command {
return xerrors.Errorf("parse ssh keygen algorithm %s: %w", sshKeygenAlgorithmRaw, err)
}

telemetry, err := monitoring.ParseTelemetry(telemetryRaw)
if err != nil {
return xerrors.Errorf("parse telemetry %s: %w", telemetryRaw, err)
}

turnServer, err := turnconn.New(&turn.RelayAddressGeneratorStatic{
RelayAddress: net.ParseIP(turnRelayAddress),
Address: turnRelayAddress,
Expand Down Expand Up @@ -301,6 +308,12 @@ func server() *cobra.Command {
}
}

options.Monitor = monitoring.New(cmd.Context(), &monitoring.Options{
Database: options.Database,
Logger: options.Logger,
Telemetry: telemetry,
})

coderAPI := coderd.New(options)
client := codersdk.New(localURL)
if tlsEnable {
Expand Down Expand Up @@ -537,6 +550,8 @@ func server() *cobra.Command {
"Specifies organizations the user must be a member of to authenticate with GitHub.")
cliflag.BoolVarP(root.Flags(), &oauth2GithubAllowSignups, "oauth2-github-allow-signups", "", "CODER_OAUTH2_GITHUB_ALLOW_SIGNUPS", false,
"Specifies whether new users can sign up with GitHub.")
cliflag.StringVarP(root.Flags(), &telemetryRaw, "telemetry", "", "CODER_TELEMETRY", "all", "The level of telemetry to send. "+
`Accepted values are "all", "core", or "none"`)
cliflag.BoolVarP(root.Flags(), &tlsEnable, "tls-enable", "", "CODER_TLS_ENABLE", false, "Specifies if TLS will be enabled")
cliflag.StringVarP(root.Flags(), &tlsCertFile, "tls-cert-file", "", "CODER_TLS_CERT_FILE", "",
"Specifies the path to the certificate for TLS. It requires a PEM-encoded file. "+
Expand Down Expand Up @@ -698,16 +713,16 @@ func newProvisionerDaemon(ctx context.Context, coderAPI *coderd.API,
func printLogo(cmd *cobra.Command, spooky bool) {
if spooky {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), `
▄████▄ ▒█████ ▓█████▄ ▓█████ ██▀███
▄████▄ ▒█████ ▓█████▄ ▓█████ ██▀███
▒██▀ ▀█ ▒██▒ ██▒▒██▀ ██▌▓█ ▀ ▓██ ▒ ██▒
▒▓█ ▄ ▒██░ ██▒░██ █▌▒███ ▓██ ░▄█ ▒
▒▓▓▄ ▄██▒▒██ ██░░▓█▄ ▌▒▓█ ▄ ▒██▀▀█▄
▒▓▓▄ ▄██▒▒██ ██░░▓█▄ ▌▒▓█ ▄ ▒██▀▀█▄
▒ ▓███▀ ░░ ████▓▒░░▒████▓ ░▒████▒░██▓ ▒██▒
░ ░▒ ▒ ░░ ▒░▒░▒░ ▒▒▓ ▒ ░░ ▒░ ░░ ▒▓ ░▒▓░
░ ▒ ░ ▒ ▒░ ░ ▒ ▒ ░ ░ ░ ░▒ ░ ▒░
░ ░ ░ ░ ▒ ░ ░ ░ ░ ░░ ░
░ ░ ░ ░ ░ ░ ░ ░
░ ░
░ ░ ░ ░ ▒ ░ ░ ░ ░ ░░ ░
░ ░ ░ ░ ░ ░ ░ ░
░ ░

`)
return
Expand Down
4 changes: 3 additions & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/coder/coder/coderd/gitsshkey"
"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/coderd/httpmw"
"github.com/coder/coder/coderd/monitoring"
"github.com/coder/coder/coderd/rbac"
"github.com/coder/coder/coderd/tracing"
"github.com/coder/coder/coderd/turnconn"
Expand All @@ -50,6 +51,7 @@ type Options struct {
GoogleTokenValidator *idtoken.Validator
GithubOAuth2Config *GithubOAuth2Config
ICEServers []webrtc.ICEServer
Monitor *monitoring.Monitor
SecureAuthCookie bool
SSHKeygenAlgorithm gitsshkey.Algorithm
TURNServer *turnconn.Server
Expand Down Expand Up @@ -92,7 +94,7 @@ func New(options *Options) *API {
next.ServeHTTP(middleware.NewWrapResponseWriter(w, r.ProtoMajor), r)
})
},
httpmw.Prometheus,
httpmw.Prometheus(options.Monitor),
tracing.HTTPMW(api.TracerProvider, "coderd.http"),
)

Expand Down
14 changes: 10 additions & 4 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/coder/coder/coderd/database/databasefake"
"github.com/coder/coder/coderd/database/postgres"
"github.com/coder/coder/coderd/gitsshkey"
"github.com/coder/coder/coderd/monitoring"
"github.com/coder/coder/coderd/turnconn"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/cryptorand"
Expand Down Expand Up @@ -160,10 +161,15 @@ func NewWithAPI(t *testing.T, options *Options) (*codersdk.Client, *coderd.API)
AzureCertificates: options.AzureCertificates,
GithubOAuth2Config: options.GithubOAuth2Config,
GoogleTokenValidator: options.GoogleTokenValidator,
SSHKeygenAlgorithm: options.SSHKeygenAlgorithm,
TURNServer: turnServer,
APIRateLimit: options.APIRateLimit,
Authorizer: options.Authorizer,
Monitor: monitoring.New(ctx, &monitoring.Options{
Database: db,
Logger: slogtest.Make(t, nil),
Telemetry: monitoring.TelemetryNone,
}),
SSHKeygenAlgorithm: options.SSHKeygenAlgorithm,
TURNServer: turnServer,
APIRateLimit: options.APIRateLimit,
Authorizer: options.Authorizer,
})
srv.Config.Handler = coderAPI.Handler
if options.IncludeProvisionerD {
Expand Down
46 changes: 46 additions & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,35 @@ func (q *fakeQuerier) GetLatestWorkspaceBuildsByWorkspaceIDs(_ context.Context,
return returnBuilds, nil
}

func (q *fakeQuerier) GetLatestWorkspaceResources(ctx context.Context) ([]database.WorkspaceResource, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

// Get latest workspace builds.
builds := make(map[uuid.UUID]database.WorkspaceBuild)
buildNumbers := make(map[uuid.UUID]int32)
for _, workspaceBuild := range q.workspaceBuilds {
if workspaceBuild.BuildNumber > buildNumbers[workspaceBuild.WorkspaceID] {
builds[workspaceBuild.WorkspaceID] = workspaceBuild
buildNumbers[workspaceBuild.WorkspaceID] = workspaceBuild.BuildNumber
}
}

// Get resources for each latest build.
resources := make([]database.WorkspaceResource, 0)
for _, workspaceBuild := range q.workspaceBuilds {
rs, err := q.GetWorkspaceResourcesByJobID(ctx, workspaceBuild.JobID)
if err != nil {
return nil, err
}
resources = append(resources, rs...)
}
if len(resources) == 0 {
return nil, sql.ErrNoRows
}
return resources, nil
}

func (q *fakeQuerier) GetWorkspaceBuildByWorkspaceID(_ context.Context,
params database.GetWorkspaceBuildByWorkspaceIDParams) ([]database.WorkspaceBuild, error) {
q.mutex.RLock()
Expand Down Expand Up @@ -1180,6 +1209,23 @@ func (q *fakeQuerier) GetWorkspaceResourcesByJobID(_ context.Context, jobID uuid
return resources, nil
}

// revive:disable-next-line:flag-parameter
func (q *fakeQuerier) GetWorkspaces(_ context.Context, deleted bool) ([]database.Workspace, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

workspaces := make([]database.Workspace, 0)
for _, workspace := range q.workspaces {
if workspace.Deleted == deleted {
workspaces = append(workspaces, workspace)
}
}
if len(workspaces) == 0 {
return nil, sql.ErrNoRows
}
return workspaces, nil
}

func (q *fakeQuerier) GetProvisionerJobsByIDs(_ context.Context, ids []uuid.UUID) ([]database.ProvisionerJob, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
Expand Down
2 changes: 2 additions & 0 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions coderd/database/queries/workspaceresources.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,23 @@ INSERT INTO
workspace_resources (id, created_at, job_id, transition, type, name)
VALUES
($1, $2, $3, $4, $5, $6) RETURNING *;

-- name: GetLatestWorkspaceResources :many
SELECT workspace_resources.*
FROM (
SELECT
workspace_id, MAX(build_number) as max_build_number
FROM
workspace_builds
GROUP BY
workspace_id
) latest_workspace_builds
INNER JOIN
workspace_builds
ON
workspace_builds.workspace_id = latest_workspace_builds.workspace_id
AND workspace_builds.build_number = latest_workspace_builds.max_build_number
INNER JOIN
workspace_resources
ON
workspace_resources.job_id = workspace_builds.job_id;
3 changes: 3 additions & 0 deletions coderd/database/queries/workspaces.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
-- name: GetWorkspaces :many
SELECT * FROM workspaces WHERE deleted = $1;

-- name: GetWorkspaceByID :one
SELECT
*
Expand Down
Loading