-
Notifications
You must be signed in to change notification settings - Fork 926
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ethanndickson
wants to merge
12
commits into
main
Choose a base branch
from
ethan/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 from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
54accd6
chore: route connection logs to new table
ethanndickson 9d7efc1
gen, identation
ethanndickson 4475ee4
comment
ethanndickson f7c656a
comment
ethanndickson 43bd52f
add back request id as connection id
ethanndickson c2ae96e
lint
ethanndickson 8bc1a6f
review
ethanndickson af70a4a
docs link changed again
ethanndickson 02f36dd
migration numbers
ethanndickson cf39fd7
use a single row for connections and disconnections
ethanndickson 6bdf3be
dbmem
ethanndickson d42bdff
fix migrations
ethanndickson 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 was deleted.
Oops, something went wrong.
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,79 @@ | ||
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" | ||
|
||
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" | ||
"github.com/coder/coder/v2/codersdk/agentsdk" | ||
) | ||
|
||
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) { | ||
action, err := db2sdk.ConnectionLogActionFromAgentProtoConnectionAction(req.GetConnection().GetAction()) | ||
ethanndickson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return nil, err | ||
} | ||
connectionType, err := agentsdk.ConnectionTypeFromProto(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.Export(ctx, database.ConnectionLog{ | ||
ID: uuid.New(), | ||
Time: req.GetConnection().GetTimestamp().AsTime(), | ||
OrganizationID: workspace.OrganizationID, | ||
WorkspaceOwnerID: workspace.OwnerID, | ||
WorkspaceID: workspace.ID, | ||
WorkspaceName: workspace.Name, | ||
AgentName: workspaceAgent.Name, | ||
Action: action, | ||
Code: req.GetConnection().GetStatusCode(), | ||
Ip: database.ParseIP(req.GetConnection().GetIp()), | ||
ConnectionType: sql.NullString{ | ||
String: string(connectionType), | ||
Valid: true, | ||
}, | ||
Reason: sql.NullString{ | ||
String: reason, | ||
Valid: reason != "", | ||
}, | ||
|
||
// It's not possible to tell which user connected. Once we have | ||
// the capability, this may be reported by the agent. | ||
UserID: uuid.Nil, | ||
}) | ||
if err != nil { | ||
return nil, xerrors.Errorf("export connection log: %w", err) | ||
} | ||
|
||
return &emptypb.Empty{}, nil | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.