Skip to content

Commit 6da8ec1

Browse files
committed
Add SetSource/xmldocs for otel
1 parent e15bb19 commit 6da8ec1

1 file changed

Lines changed: 41 additions & 9 deletions

File tree

Expecto.Tests/OpenTelemetry.fs

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,38 @@ module OpenTelemetry =
44
open System
55
open System.Diagnostics
66
open System.Collections.Generic
7-
open System.Threading
87
open Impl
9-
8+
open System.Runtime.CompilerServices
9+
type Activity with
10+
/// <summary>Sets code semantic conventions for <c>code.function.name</c>, <c>code.filepath</c>, and <c>code.lineno</c> </summary>
11+
/// <param name="name_space">Optional: The current namespace. Will default to using <c>Reflection.MethodBase.GetCurrentMethod().DeclaringType</c></param>
12+
/// <param name="memberName">Optional: The current function Don't set this. This uses <c>CallerMemberName.</c></param>
13+
/// <param name="path">Optional: The current filepath. Don't set this. This uses <c>CallerFilePath.</c></param>
14+
/// <param name="line">Optional: The current line number. Don't set this. This uses <c>CallerLineNumber.</c></param>
15+
member inline x.SetSource(
16+
?nameSpace : string,
17+
[<CallerMemberName>] ?memberName: string,
18+
[<CallerFilePath>] ?path: string,
19+
[<CallerLineNumber>] ?line: int) =
20+
if not (isNull x) then
21+
if x.GetTagItem "code.function.name" = null then
22+
let nameSpace =
23+
nameSpace
24+
|> Option.defaultWith (fun () ->
25+
Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName.Split("+") // F# has + in type names that refer to anonymous functions, we typically want the first named type
26+
|> Seq.tryHead
27+
|> Option.defaultValue "")
28+
let memberName = defaultArg memberName ""
29+
x.SetTag("code.function.name", $"{nameSpace}.{memberName}" ) |> ignore
30+
if x.GetTagItem "code.filepath" = null then x.SetTag("code.filepath", defaultArg path "") |> ignore
31+
if x.GetTagItem "code.lineno" = null then x.SetTag("code.lineno", defaultArg line 0) |> ignore
1032

1133
module internal Activity =
1234
let inline isNotNull x = isNull x |> not
1335

1436
let inline setStatus (status : ActivityStatusCode) (span : Activity) =
1537
if isNotNull span then
16-
span.SetStatus(status) |> ignore
38+
span.SetStatus status |> ignore
1739

1840
let inline setExn (e : exn) (span : Activity) =
1941
if isNotNull span|> not then
@@ -43,7 +65,12 @@ module OpenTelemetry =
4365

4466
let inline addOutcome (result : TestResult) (span : Activity) =
4567
if isNotNull span then
46-
span.SetTag("test.result.status", result.tag) |> ignore
68+
let status = match result with
69+
| Passed -> "Passed"
70+
| Ignored _ -> "Ignored"
71+
| Failed _ -> "Failed"
72+
| Error _ -> "Error"
73+
span.SetTag("test.result.status", status) |> ignore
4774
span.SetTag("test.result.message", result) |> ignore
4875

4976
let inline start (span : Activity) =
@@ -57,16 +84,16 @@ module OpenTelemetry =
5784

5885
let inline setEndTimeNow (span : Activity) =
5986
if isNotNull span then
60-
span.SetEndTime(DateTime.UtcNow) |> ignore
87+
span.SetEndTime DateTime.UtcNow |> ignore
6188

6289
let inline createActivity (name : string) (source : ActivitySource) =
63-
match source with
64-
| source when not(isNull source) -> source.CreateActivity(name, ActivityKind.Internal)
65-
| _ -> null
90+
if isNotNull source then
91+
source.CreateActivity(name, ActivityKind.Internal)
92+
else
93+
null
6694

6795
open Activity
6896
open System.Runtime.ExceptionServices
69-
open System.IO
7097

7198
let inline internal reraiseAnywhere<'a> (e: exn) : 'a =
7299
ExceptionDispatchInfo.Capture(e).Throw()
@@ -160,6 +187,11 @@ module OpenTelemetry =
160187
handleFailure span e
161188
)
162189

190+
/// <summary>Wraps each test with an OpenTelemetry Span/System.Diagnostics.Activity.</summary>
191+
/// <param name="config">ExpectoConfig</param>
192+
/// <param name="activitySource">Provides APIs start OpenTelemetry Span/System.Diagnostics.Activity</param>
193+
/// <param name="rootTest">The tests to wrap in span/activity.</param>
194+
/// <returns>Tests wrapped in a Span/Activity</returns>
163195
let addOpenTelemetry_SpanPerTest (config: ExpectoConfig) (activitySource: ActivitySource) (rootTest: Test) : Test =
164196
rootTest
165197
|> Test.toTestCodeList

0 commit comments

Comments
 (0)