Skip to content

Proposal: built-in tracing and metrics via ActivitySource/Meter #1854

Open

Description

@YoussefElshemi

Hi, I'd like to propose adding built-in tracing and metrics to SSH.NET using System.Diagnostics.ActivitySource and Meter, the same approach Npgsql and the MongoDB driver use.

I first opened an OpenTelemetry contrib package that wraps ISftpClient/ISshClient (open-telemetry/opentelemetry-dotnet-contrib#5781). An OTel maintainer suggested building it into the library instead, so users only need AddSource("Renci.SshNet") and AddMeter("Renci.SshNet"). That also works with other backends (Datadog, App Insights), and covers cases a wrapper can't reach: CreateCommand + Execute, Begin*/End*, non-seekable streams.

I have a working prototype: https://github.com/YoussefElshemi/SSH.NET/tree/native-telemetry

  • Spans:
    • Connect (ssh/sftp/scp/netconf)
    • SFTP UploadFile, DownloadFile, ListDirectory
    • SshCommand execution
  • Metric: ssh.client.operation.duration histogram (seconds)
  • Where it hooks in: the existing Internal* methods, so every overload is covered
  • When nobody is listening: no allocation or extra work
  • Tests: existing unit tests pass. New integration tests pass on net48/net8/net9/net10.

Questions before I go further:

  1. It adds System.Diagnostics.DiagnosticSource (8.0.1) as a dependency on net462/netstandard2.0 only. Is that acceptable?
  2. Attribute names like ssh.operation.name, sftp.transfer.bytes and ssh.command.exit_code aren't official OpenTelemetry names yet. Any preference?
  3. Command text isn't recorded, since it may contain secrets. Should there be an opt-in?
  4. Should a first PR stay this small, or also cover Exists/Delete/Rename/SCP?

Would you be open to a PR along these lines?

Activity

  1. Rob-Hague commented on Oct 27, 2026

    @Rob-Hague
    Collaborator

    I would be open to it, but I have only surface-level familiarity with Activity/Metrics/OpenTelemetry so I would not be the best judge of an implementation (@mus65?). I also can't guarantee review timing from my side. There is already a list of things I would like to address if ever I am motivated.


    1. It adds System.Diagnostics.DiagnosticSource (8.0.1) as a dependency on net462/netstandard2.0 only. Is that acceptable?

    Yes, next month the Microsoft.Extensions.Logging.Abstractions dependency would be bumped to v10 which brings it in already.

    1. Attribute names like ssh.operation.name, sftp.transfer.bytes and ssh.command.exit_code aren't official OpenTelemetry names yet. Any preference?

    I would like to know what precedent there is in existing clients or servers. I assume this is not the first implementation.

    1. Command text isn't recorded, since it may contain secrets. Should there be an opt-in?

    Probably not until there is explicit demand in the future

    1. Should a first PR stay this small, or also cover Exists/Delete/Rename/SCP?>

    Without having studied the code, covering other SFTP operations sounds OK. SCP should not be touched as it is a legacy protocol whose use should be discouraged.

  2. YoussefElshemi commented on Oct 27, 2026

    @YoussefElshemi
    Author

    Thanks for the quick reply!

    On attribute names: as far as I could find, this would be the first. OpenTelemetry's semantic conventions have no SSH or SFTP namespace, and I couldn't find built-in tracing in other SSH clients (Paramiko, JSch, Apache MINA SSHD, Go's x/crypto/ssh). The only SSH-related thing I found in the OTel ecosystem is the Collector's sshcheck receiver, which is a connectivity probe. It uses ssh.endpoint and sshcheck.* metrics.

    So the plan would be:

    • Use the official attributes where they exist: server.address, server.port, network.protocol.name, file.path, error.type
    • Keep the custom ones minimal and under an ssh./sftp. prefix (ssh.operation.name, sftp.transfer.bytes, ssh.command.exit_code)
    • Treat the names as experimental until there's a convention for them, which I'm happy to propose upstream

    I'll drop the SCP changes, add the remaining SFTP operations (Exists, Delete, CreateDirectory, Rename, Get/SetAttributes, ChangeDirectory, etc.), and update the branch. No rush on review.

  3. mus65 commented on Oct 27, 2026

    @mus65
    Contributor

    I would be open to it, but I have only surface-level familiarity with Activity/Metrics/OpenTelemetry so I would not be the best judge of an implementation (@mus65?)

    I'm not too familiar with these APIs either, but I could look into it and help out with review. The plan sounds good to me in general.

  4. YoussefElshemi commented on Oct 27, 2026

    @YoussefElshemi
    Author

    Opened #1857 with the changes discussed above: SCP is left untouched and the remaining SFTP operations are now covered. @mus65 thanks for offering to help review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions