Skip to content

Commit a6ee289

Browse files
committed
Fleshed out ActivitySource instrumentation
1 parent c6b355b commit a6ee289

File tree

7 files changed

+496
-372
lines changed

7 files changed

+496
-372
lines changed

paket.dependencies

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ nuget FSharp.Core >= 4.7.2 content: false, lowest_matching: true
1616

1717
nuget AWSSDK.DynamoDBv2 ~> 3.7.5
1818
nuget System.Diagnostics.DiagnosticSource >= 6.0.0
19+
nuget System.Text.Json
1920
github eiriktsarpalis/TypeShape:10.0.0 src/TypeShape/TypeShape.fs
2021

2122
group Test

paket.lock

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,41 @@ STORAGE: NONE
22
RESTRICTION: || (== net9.0) (== netstandard2.0) (== netstandard2.1)
33
NUGET
44
remote: https://api.nuget.org/v3/index.json
5-
AWSSDK.Core (3.7.400.64)
5+
AWSSDK.Core (3.7.400.70)
66
Microsoft.Bcl.AsyncInterfaces (>= 1.1) - restriction: || (&& (== net9.0) (< netcoreapp3.1)) (== netstandard2.0) (== netstandard2.1)
7-
AWSSDK.DynamoDBv2 (3.7.404.5)
8-
AWSSDK.Core (>= 3.7.400.64 < 4.0)
7+
AWSSDK.DynamoDBv2 (3.7.404.11)
8+
AWSSDK.Core (>= 3.7.400.70 < 4.0)
99
DotNet.ReproducibleBuilds (1.2.25) - copy_local: true
1010
FSharp.Core (4.7.2)
11-
Microsoft.Bcl.AsyncInterfaces (9.0) - restriction: || (&& (== net9.0) (< netcoreapp3.1)) (== netstandard2.0) (== netstandard2.1)
11+
Microsoft.Bcl.AsyncInterfaces (9.0) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
1212
System.Threading.Tasks.Extensions (>= 4.5.4) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< netstandard2.1)) (== netstandard2.0) (&& (== netstandard2.1) (>= net462))
1313
System.Buffers (4.6) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< netcoreapp2.1)) (== netstandard2.0) (== netstandard2.1)
1414
System.Diagnostics.DiagnosticSource (9.0)
1515
System.Memory (>= 4.5.5) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
1616
System.Runtime.CompilerServices.Unsafe (>= 6.0) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
17+
System.IO.Pipelines (9.0) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
18+
System.Buffers (>= 4.5.1) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
19+
System.Memory (>= 4.5.5) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
20+
System.Threading.Tasks.Extensions (>= 4.5.4) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
1721
System.Memory (4.6) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
1822
System.Buffers (>= 4.6) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< netcoreapp2.1)) (== netstandard2.0) (== netstandard2.1)
1923
System.Numerics.Vectors (>= 4.6) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< netcoreapp2.1)) (== netstandard2.0) (== netstandard2.1)
2024
System.Runtime.CompilerServices.Unsafe (>= 6.1) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< netcoreapp2.1)) (== netstandard2.0) (== netstandard2.1)
2125
System.Numerics.Vectors (4.6) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< netcoreapp2.1)) (== netstandard2.0) (== netstandard2.1)
2226
System.Runtime.CompilerServices.Unsafe (6.1) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (&& (== net9.0) (< netcoreapp2.1)) (== netstandard2.0) (== netstandard2.1)
23-
System.Threading.Tasks.Extensions (4.6) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< netstandard2.1)) (== netstandard2.0) (&& (== netstandard2.1) (>= net462))
27+
System.Text.Encodings.Web (9.0) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
28+
System.Buffers (>= 4.5.1) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
29+
System.Memory (>= 4.5.5) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
30+
System.Runtime.CompilerServices.Unsafe (>= 6.0) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
31+
System.Text.Json (9.0)
32+
Microsoft.Bcl.AsyncInterfaces (>= 9.0) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
33+
System.Buffers (>= 4.5.1) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
34+
System.IO.Pipelines (>= 9.0) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
35+
System.Memory (>= 4.5.5) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
36+
System.Runtime.CompilerServices.Unsafe (>= 6.0) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
37+
System.Text.Encodings.Web (>= 9.0) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
38+
System.Threading.Tasks.Extensions (>= 4.5.4) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (== netstandard2.0) (== netstandard2.1)
39+
System.Threading.Tasks.Extensions (4.6) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< net8.0)) (&& (== net9.0) (< netstandard2.1)) (== netstandard2.0) (== netstandard2.1)
2440
System.Runtime.CompilerServices.Unsafe (>= 6.1) - restriction: || (&& (== net9.0) (>= net462)) (&& (== net9.0) (< netcoreapp2.1)) (== netstandard2.0) (== netstandard2.1)
2541
Unquote (6.1)
2642
FSharp.Core (>= 4.7.2)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace FSharp.AWS.DynamoDB
2+
3+
module Diagnostics =
4+
open System.Diagnostics
5+
open Amazon.DynamoDBv2.Model
6+
open System.Text.Json
7+
8+
let private activitySource = new ActivitySource("FSharp.AWS.DynamoDB")
9+
10+
let hasListeners () : bool = activitySource.HasListeners()
11+
12+
let addActivityTags (tags: (string * obj) list) (activity: Activity) =
13+
if notNull activity then
14+
tags |> List.iter (fun (k, v) -> activity.AddTag(k, v) |> ignore)
15+
16+
let startTableActivity (tableName: string) (operation: string) (tags: (string * obj) list) : Activity =
17+
let activity = activitySource.StartActivity(sprintf "%s %s" operation tableName, ActivityKind.Client)
18+
activity
19+
|> addActivityTags (
20+
tags
21+
|> List.append
22+
[ ("db.system", "dynamodb")
23+
("aws.dynamodb.table_names", [| tableName |])
24+
("rpc.system", "aws-api")
25+
("rpc.service", "DynamoDB")
26+
("rpc.method", operation) ]
27+
)
28+
activity
29+
30+
let startMultipleTableActivity (tableNames: string seq) (operation: string) (tags: (string * obj) list) : Activity =
31+
let activity = activitySource.StartActivity(operation, ActivityKind.Client)
32+
activity
33+
|> addActivityTags (
34+
tags
35+
|> List.append
36+
[ ("db.system", "dynamodb")
37+
("aws.dynamodb.table_names", tableNames |> Seq.toArray :> obj)
38+
("rpc.system", "aws-api")
39+
("rpc.service", "DynamoDB")
40+
("rpc.method", operation) ]
41+
)
42+
activity
43+
44+
let addActivityCapacity (capacity: ConsumedCapacity seq) (activity: Activity) =
45+
if notNull activity then
46+
let value = capacity |> Seq.choose (function | null -> None | c -> Some (JsonSerializer.Serialize c)) |> Seq.toArray
47+
activity.AddTag("aws.dynamodb.consumed_capacity", value :> obj) |> ignore
48+
49+
let addActivityException (ex: exn) (activity: Activity) =
50+
if notNull activity then
51+
activity.AddException ex |> ignore
52+
53+
let stopActivity (activity: Activity) =
54+
if notNull activity then
55+
activity.Stop()
56+
57+
module internal Task =
58+
open System.Threading.Tasks
59+
open System.Diagnostics
60+
61+
/// Adds an exception to the activity if the task is faulted
62+
let addActivityException (activity: Activity) (task: Task<'a>) =
63+
if notNull activity && task.IsFaulted then
64+
let exn =
65+
if task.Exception.InnerExceptions.Count = 1 then
66+
task.Exception.InnerExceptions[0]
67+
else
68+
task.Exception
69+
Diagnostics.addActivityException exn activity
70+
task

src/FSharp.AWS.DynamoDB/FSharp.AWS.DynamoDB.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<Compile Include="Expression\UpdateExpr.fs" />
3636
<Compile Include="Expression\ProjectionExpr.fs" />
3737
<Compile Include="Expression\ExpressionContainers.fs" />
38+
<Compile Include="Diagnostics.fs" />
3839
<Compile Include="RecordTemplate.fs" />
3940
<Compile Include="TableContext.fs" />
4041
<Compile Include="Extensions.fs" />

0 commit comments

Comments
 (0)