Skip to content

Conversation

incertum
Copy link
Contributor

update: docs featuring latest improvements

#135 should land first as I reference those updates.

CC @ktoso @heckj

@incertum
Copy link
Contributor Author

Quick note: Some of the Documentation failures are due to the fact that I need to rebase this once #135 is merged. However, I already saw some suggestions re layout or heading levels, @heckj happy to follow your suggestions!

Copy link
Collaborator

@ktoso ktoso left a comment

Choose a reason for hiding this comment

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

Some notes, overall great docs! Just need to flesh out a few confusing bits; the rest about what's allowed etc is great 👍

For general advice on how to use `Prometheus` make sure to also read the [Prometheus documentation][prometheus-docs].
- *Standards Compliant*: Follows Prometheus naming conventions and exposition formats, enforces base guarantees.
- *Type Safe*: Prevents common configuration errors through Swift's type system.
- *Thread Safe*: Operations use internal locking and conform to `Sendable`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess we may stick to Swift buzzwords here, we usually say "concurrency safety", and I amended a bit the wording with sendable. On that note, We should enable Swift 6 mode if we want to claim concurrency safety, the compiler will do more checks then

Suggested change
- *Thread Safe*: Operations use internal locking and conform to `Sendable`.
- *Concurrency Safe*: Operations are thread safe, and safety is expressed using Swift's concurrency safety mechanisms such as `Sendable`.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simplified.


## Thread Safety

All operations are thread-safe, with the exception of the lower-level ``emit(into:)`` overload variant (compared to the thread-safe ``emitToBuffer()`` or ``emitToString()``). The client uses internal locking to ensure consistency across concurrent access and conforms to Swift's `Sendable` protocol, meaning it can be safely passed between actors and used in concurrent contexts without additional synchronization.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why wouldn't emit(into:) be thread safe, there's no reason for that 🤔

If you're concerned about someone concurrently calling with the same buffer -- swift won't allow that since that'd violate exclusive access with the inout enforces

I'd rephrase this section a bit, we don't need to explain Sendable to Swift developers here. Concurrency safety is kinda "expected" in Swift nowadays, however we should enable Swift 6 mode so we can prove we're safe :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will check, let's get back to it next week. Meanwhile quickly rebased. In general I suppose I still live too much in the C and C++ world ...

Copy link
Collaborator

Choose a reason for hiding this comment

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

I mean that Swift will literarily prevent such races:

func test() async { 
  var buffer: [UInt8] = []

  await Task { 
    emit(into: &buffer) // error: Sending value of non-Sendable type '() async -> ()' risks causing data races
  }.value
        
  await Task { 
    emit(into: &buffer)
  }.value
}

while the following is OK:

func test() async { 
  var buffer: [UInt8] = []

  await Task { 
    emit(into: &buffer)
  }.value
}

because the value is "sent" to that task and cannot be accessed from other contexts.

In Swift we lean heavily on compiler assured concurrency safety and it would be weird to call out a method is not safe like that, because it's context dependent and the compiler will prevent the racy versions of code :-)

This is an error in Swift 6 mode (or "complete concurrency checking), and a warning in 5 mode; there's no need to call out in our docs issues that the compiler will prevent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you! However, if it needs to be Sendable, you still need locking, just like we do with the internal buffer. I’ve rewritten this part of the documentation, so let me know!

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's the thing -- there's no locking required if sendable analysis can prove it's not strictly needed or ensured otherwise -- see the capturing in the Task, or you could have a buffer in an actor and the collecting tasks be on the same actor etc... So yes, it needs to be safe and swift will enforce that but it doesn't explicitly mean a lock per se :)

@incertum incertum force-pushed the update/docs-aug-2025 branch from 8003cde to da902b5 Compare August 30, 2025 14:25
incertum and others added 2 commits September 3, 2025 13:28
More precise thread-safety guidance / documentation.
Fix Swift Docc checks.

Co-authored-by: Konrad `ktoso` Malawski <[email protected]>
Signed-off-by: Melissa Kilby <[email protected]>
@incertum incertum force-pushed the update/docs-aug-2025 branch from da902b5 to 2c58e4f Compare September 3, 2025 20:29
@incertum incertum added the semver/none No version bump required. label Sep 3, 2025
@incertum incertum force-pushed the update/docs-aug-2025 branch from 2c58e4f to 9c7e444 Compare September 3, 2025 20:34
@ktoso
Copy link
Collaborator

ktoso commented Sep 3, 2025

Looks good, nice updates @incertum !

@ktoso ktoso merged commit cd8dbeb into swift-server:main Sep 3, 2025
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver/none No version bump required.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants