Skip to content

Commit

Permalink
Add PropertyTag Literals (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink authored Nov 10, 2021
1 parent 2e8d857 commit 2b998c1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ The `Unreleased` section name is replaced by the expected version of next releas
## [Unreleased]

### Added

- Stores: Expose `.Log.PropertyTag` Literals to enable log filtering [#298](https://github.com/jet/equinox/pull/298~~~~)

### Changed
### Removed
### Fixed
Expand Down
7 changes: 5 additions & 2 deletions src/Equinox.CosmosStore/CosmosStore.fs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ type IRetryPolicy = abstract member Execute: (int -> Async<'T>) -> Async<'T>

module Log =

/// <summary>Name of Property used for <c>Metric</c> in <c>LogEvent</c>s.</summary>
let [<Literal>] PropertyTag = "cosmosEvt"

[<NoEquality; NoComparison>]
type Measurement =
{ database: string; container: string; stream: string
Expand Down Expand Up @@ -265,7 +268,7 @@ module Log =
// Sidestep Log.ForContext converting to a string; see https://github.com/serilog/serilog/issues/1124
let internal event (value : Metric) (log : ILogger) =
let enrich (e : Serilog.Events.LogEvent) =
e.AddPropertyIfAbsent(Serilog.Events.LogEventProperty("cosmosEvt", Serilog.Events.ScalarValue(value)))
e.AddPropertyIfAbsent(Serilog.Events.LogEventProperty(PropertyTag, Serilog.Events.ScalarValue(value)))
log.ForContext({ new Serilog.Core.ILogEventEnricher with member __.Enrich(evt,_) = enrich evt })
let internal (|BlobLen|) = function null -> 0 | (x : byte[]) -> x.Length
let internal (|EventLen|) (x: #IEventData<_>) = let (BlobLen bytes), (BlobLen metaBytes) = x.Data, x.Meta in bytes + metaBytes + 80
Expand All @@ -274,7 +277,7 @@ module Log =
| (:? Serilog.Events.ScalarValue as x) -> Some x.Value
| _ -> None
let (|MetricEvent|_|) (logEvent : Serilog.Events.LogEvent) : Metric option =
match logEvent.Properties.TryGetValue("cosmosEvt") with
match logEvent.Properties.TryGetValue(PropertyTag) with
| true, SerilogScalar (:? Metric as e) -> Some e
| _ -> None
[<RequireQualifiedAccess>]
Expand Down
8 changes: 6 additions & 2 deletions src/Equinox.EventStore/EventStore.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type Direction = Forward | Backward with
override this.ToString() = match this with Forward -> "Forward" | Backward -> "Backward"

module Log =

/// <summary>Name of Property used for <c>Event</c> in <c>LogEvent</c>s.</summary>
let [<Literal>] PropertyTag = "esEvt"

[<NoEquality; NoComparison>]
type Measurement = { stream: string; interval: StopwatchInterval; bytes: int; count: int }

Expand Down Expand Up @@ -45,7 +49,7 @@ module Log =
/// Attach a property to the log context to hold the metrics
// Sidestep Log.ForContext converting to a string; see https://github.com/serilog/serilog/issues/1124
let event (value : Event) (log : ILogger) =
let enrich (e : LogEvent) = e.AddPropertyIfAbsent(LogEventProperty("esEvt", ScalarValue(value)))
let enrich (e : LogEvent) = e.AddPropertyIfAbsent(LogEventProperty(PropertyTag, ScalarValue(value)))
log.ForContext({ new Serilog.Core.ILogEventEnricher with member __.Enrich(evt,_) = enrich evt })

let withLoggedRetries<'t> retryPolicy (contextLabel : string) (f : ILogger -> Async<'t>) log : Async<'t> =
Expand Down Expand Up @@ -78,7 +82,7 @@ module Log =
| _ -> None

let (|EsMetric|_|) (logEvent : LogEvent) : Event option =
match logEvent.Properties.TryGetValue("esEvt") with
match logEvent.Properties.TryGetValue(PropertyTag) with
| true, SerilogScalar (:? Event as e) -> Some e
| _ -> None

Expand Down
8 changes: 6 additions & 2 deletions src/Equinox.SqlStreamStore/SqlStreamStore.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ type Direction = Forward | Backward with
override this.ToString() = match this with Forward -> "Forward" | Backward -> "Backward"

module Log =

/// <summary>Name of Property used for <c>Event</c> in <c>LogEvent</c>s.</summary>
let [<Literal>] PropertyTag = "esEvt"

[<NoEquality; NoComparison>]
type Measurement = { stream: string; interval: StopwatchInterval; bytes: int; count: int }
[<NoEquality; NoComparison>]
Expand All @@ -43,7 +47,7 @@ module Log =
/// Attach a property to the log context to hold the metrics
// Sidestep Log.ForContext converting to a string; see https://github.com/serilog/serilog/issues/1124
let event (value : Event) (log : ILogger) =
let enrich (e : LogEvent) = e.AddPropertyIfAbsent(LogEventProperty("esEvt", ScalarValue(value)))
let enrich (e : LogEvent) = e.AddPropertyIfAbsent(LogEventProperty(PropertyTag, ScalarValue(value)))
log.ForContext({ new Serilog.Core.ILogEventEnricher with member __.Enrich(evt,_) = enrich evt })
let withLoggedRetries<'t> retryPolicy (contextLabel : string) (f : ILogger -> Async<'t>) log: Async<'t> =
match retryPolicy with
Expand Down Expand Up @@ -73,7 +77,7 @@ module Log =
| (:? ScalarValue as x) -> Some x.Value
| _ -> None
let (|EsMetric|_|) (logEvent : LogEvent) : Event option =
match logEvent.Properties.TryGetValue("esEvt") with
match logEvent.Properties.TryGetValue(PropertyTag) with
| true, SerilogScalar (:? Event as e) -> Some e
| _ -> None
type Counter =
Expand Down

0 comments on commit 2b998c1

Please sign in to comment.