Skip to content
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

Trace ssh sessions #14966

Merged
merged 3 commits into from
Aug 4, 2022
Merged

Trace ssh sessions #14966

merged 3 commits into from
Aug 4, 2022

Conversation

rosstimothy
Copy link
Contributor

Adds a wrapper around ssh.Session which injects tracing context
in a similar manner to the ssh.Client wrapper.
All usages of ssh.Session have now been replaced and have the appropriate
context.Context passed along

Part of #12241

@rosstimothy rosstimothy force-pushed the tross/ssh_session_tracing branch from 56f6e01 to 642ab9b Compare July 27, 2022 20:54
@rosstimothy rosstimothy force-pushed the tross/ssh_session_tracing branch 5 times, most recently from ec4aa71 to 9531434 Compare July 28, 2022 13:48
Adds a wrapper around `ssh.Session` which injects tracing context
in a similar manner to the `ssh.Client` wrapper. All usages of
`ssh.Session` have now been replaced and have the appropriate
`context.Context` passed along

Part of #12241
@rosstimothy rosstimothy force-pushed the tross/ssh_session_tracing branch from 9531434 to f67c8f8 Compare July 28, 2022 15:18
@rosstimothy rosstimothy marked this pull request as ready for review July 28, 2022 15:56
@github-actions github-actions bot added the tsh tsh - Teleport's command line tool for logging into nodes running Teleport. label Jul 28, 2022
Comment on lines +363 to +375
ctx, span := tracer.Start(
c.ctx,
fmt.Sprintf("ssh.OpenChannel/%s", name),
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
oteltrace.WithAttributes(
append(
peerAttr(c.Conn.RemoteAddr()),
semconv.RPCServiceKey.String("ssh.Client"),
semconv.RPCMethodKey.String("OpenChannel"),
semconv.RPCSystemKey.String("ssh"),
)...,
),
)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is duplicated quite a few times, with the only things changing are what's in line #365 and #371, and some having extra attributes - whats your thoughts on abstracting it out?

Something like

func StartSSHTrace(ctx context.Context, kind, name string, tracer *tracing.Provider, attributes ...attribute.KeyValue) (*context.Context, trace.error) {
	return tracer.Start(
		ctx,
		fmt.Sprintf("ssh.%s/%s", kind, name),
		oteltrace.WithSpanKind(oteltrace.SpanKindClient),
		oteltrace.WithAttributes(
			append(
				semconv.RPCServiceKey.String("ssh.Client"),
				semconv.RPCMethodKey.String(kind),
				semconv.RPCSystemKey.String("ssh"),
				attributes...,
			)...,
		),
	)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did toy around with this, but I'm not sure that it adds much value or makes things any easier or less verbose.

I ended up with a slightly different function than the one your proposed:

func newSpan(ctx context.Context, name string, cfg *tracing.Config, attributes ...attribute.KeyValue) (context.Context, oteltrace.Span) {
	tracer := cfg.TracerProvider.Tracer(instrumentationName)

	return tracer.Start(
		ctx,
		name,
		oteltrace.WithSpanKind(oteltrace.SpanKindClient),
		oteltrace.WithAttributes(semconv.RPCSystemKey.String("ssh")),
		oteltrace.WithAttributes(
			attributes...,
		),
	)
}

Which turns the call site into:

	ctx, span := newSpan(
		ctx,
		fmt.Sprintf("ssh.SessionRequest/%s", name),
		config,
		[]attribute.KeyValue{
			attribute.Bool("want_reply", wantReply),
			semconv.RPCServiceKey.String("ssh.Session"),
			semconv.RPCMethodKey.String("SendRequest"),
		}...,
	)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I was hesitant to suggest it, as I realise it's still pretty verbose - I was just thinking of keeping similar attributes the same across the board. Happy either way!

opts []tracing.Option

// lock protects the context queue
lock sync.Mutex
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be a sync.RWMutex

Copy link
Contributor

@fspmarshall fspmarshall Aug 4, 2022

Choose a reason for hiding this comment

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

RWMutexes aren't more efficient unless you expect fairly high numbers of concurrent reads, both in absolute, and relative to the number of concurrent writes. Near as I can tell that isn't true for this type.

Comment on lines +341 to +342
c.lock.Lock()
defer c.lock.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

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

& then this becomes

Suggested change
c.lock.Lock()
defer c.lock.Unlock()
c.lock.RLock()
defer c.lock.RUnlock()

Copy link
Contributor

Choose a reason for hiding this comment

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

This function modifies c.contexts.

Copy link
Contributor

@fspmarshall fspmarshall left a comment

Choose a reason for hiding this comment

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

LGTM. I think if we add any more instrumentation to the ssh types we might want to consider creating interfaces to abstract over them. Feels fiddly to have to change the types all over to add a wrapper.

@rosstimothy rosstimothy enabled auto-merge (squash) August 4, 2022 21:33
@rosstimothy rosstimothy merged commit 0cb248d into master Aug 4, 2022
@github-actions
Copy link

github-actions bot commented Aug 4, 2022

@rosstimothy See the table below for backport results.

Branch Result
branch/v10 Create PR

This was referenced Aug 17, 2022
rosstimothy added a commit that referenced this pull request Aug 24, 2022
Trace ssh sessions (#14966)

Adds a wrapper around `ssh.Session` which injects tracing context
in a similar manner to the `ssh.Client` wrapper. All usages of
`ssh.Session` have now been replaced and have the appropriate
`context.Context` passed along

Part of #12241
rosstimothy added a commit that referenced this pull request Aug 24, 2022
Trace ssh sessions (#14966)

Adds a wrapper around `ssh.Session` which injects tracing context
in a similar manner to the `ssh.Client` wrapper. All usages of
`ssh.Session` have now been replaced and have the appropriate
`context.Context` passed along

Part of #12241
@zmb3 zmb3 deleted the tross/ssh_session_tracing branch September 9, 2022 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tsh tsh - Teleport's command line tool for logging into nodes running Teleport.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants