- Notifications
You must be signed in to change notification settings - Fork914
chore: route connection logs to new table#18340
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
ethanndickson wants to merge12 commits intomainChoose a base branch fromethan/reroute-connection-logs
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+2,012 −456
Draft
Changes fromall commits
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
54accd6
chore: route connection logs to new table
ethanndickson9d7efc1
gen, identation
ethanndickson4475ee4
comment
ethanndicksonf7c656a
comment
ethanndickson43bd52f
add back request id as connection id
ethanndicksonc2ae96e
lint
ethanndickson8bc1a6f
review
ethanndicksonaf70a4a
docs link changed again
ethanndickson02f36dd
migration numbers
ethanndicksoncf39fd7
use a single row for connections and disconnections
ethanndickson6bdf3be
dbmem
ethanndicksond42bdff
fix migrations
ethanndicksonFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
16 changes: 8 additions & 8 deletionscoderd/agentapi/api.go
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
105 changes: 0 additions & 105 deletionscoderd/agentapi/audit.go
This file was deleted.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
96 changes: 96 additions & 0 deletionscoderd/agentapi/connectionlog.go
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,96 @@ | ||
package agentapi | ||
import ( | ||
"context" | ||
"database/sql" | ||
"sync/atomic" | ||
"github.com/google/uuid" | ||
"golang.org/x/xerrors" | ||
"google.golang.org/protobuf/types/known/emptypb" | ||
"cdr.dev/slog" | ||
ethanndickson marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
agentproto "github.com/coder/coder/v2/agent/proto" | ||
"github.com/coder/coder/v2/coderd/connectionlog" | ||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/database/db2sdk" | ||
) | ||
type ConnLogAPI struct { | ||
AgentFn func(context.Context) (database.WorkspaceAgent, error) | ||
ConnectionLogger *atomic.Pointer[connectionlog.ConnectionLogger] | ||
Database database.Store | ||
Log slog.Logger | ||
} | ||
func (a *ConnLogAPI) ReportConnection(ctx context.Context, req *agentproto.ReportConnectionRequest) (*emptypb.Empty, error) { | ||
// We use the connection ID to identify which connection log event to mark | ||
// as closed, when we receive a close action for that ID. | ||
connectionID, err := uuid.FromBytes(req.GetConnection().GetId()) | ||
if err != nil { | ||
return nil, xerrors.Errorf("connection id from bytes: %w", err) | ||
} | ||
if connectionID == uuid.Nil { | ||
return nil, xerrors.New("connection ID cannot be nil") | ||
} | ||
action, err := db2sdk.ConnectionActionFromAgentProtoConnectionAction(req.GetConnection().GetAction()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
connectionType, err := db2sdk.ConnectionLogConnectionTypeFromAgentProtoConnectionType(req.GetConnection().GetType()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Fetch contextual data for this connection log event. | ||
workspaceAgent, err := a.AgentFn(ctx) | ||
if err != nil { | ||
return nil, xerrors.Errorf("get agent: %w", err) | ||
} | ||
workspace, err := a.Database.GetWorkspaceByAgentID(ctx, workspaceAgent.ID) | ||
if err != nil { | ||
return nil, xerrors.Errorf("get workspace by agent id: %w", err) | ||
} | ||
reason := req.GetConnection().GetReason() | ||
connLogger := *a.ConnectionLogger.Load() | ||
err = connLogger.Upsert(ctx, database.UpsertConnectionLogParams{ | ||
ID: uuid.New(), | ||
Time: req.GetConnection().GetTimestamp().AsTime(), | ||
OrganizationID: workspace.OrganizationID, | ||
WorkspaceOwnerID: workspace.OwnerID, | ||
WorkspaceID: workspace.ID, | ||
WorkspaceName: workspace.Name, | ||
AgentName: workspaceAgent.Name, | ||
Type: connectionType, | ||
Code: req.GetConnection().GetStatusCode(), | ||
Ip: database.ParseIP(req.GetConnection().GetIp()), | ||
ConnectionID: uuid.NullUUID{ | ||
UUID: connectionID, | ||
Valid: true, | ||
}, | ||
CloseReason: sql.NullString{ | ||
String: reason, | ||
Valid: reason != "", | ||
}, | ||
// Used to populate whether the connection was established or closed | ||
// outside of the DB (slog). | ||
ConnectionAction: action, | ||
// It's not possible to tell which user connected. Once we have | ||
// the capability, this may be reported by the agent. | ||
UserID: uuid.NullUUID{ | ||
Valid: false, | ||
}, | ||
// N/A | ||
UserAgent: sql.NullString{}, | ||
// N/A | ||
SlugOrPort: sql.NullString{}, | ||
}) | ||
if err != nil { | ||
return nil, xerrors.Errorf("export connection log: %w", err) | ||
} | ||
return &emptypb.Empty{}, nil | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.