1
1
import { getLogger } from "@logtape/logtape" ;
2
+ import { SpanStatusCode , type TracerProvider } from "@opentelemetry/api" ;
2
3
import { delay } from "@std/async/delay" ;
4
+ import metadata from "../deno.json" with { type : "json" } ;
3
5
import {
4
6
type DocumentLoader ,
5
7
getDocumentLoader ,
6
8
type GetUserAgentOptions ,
7
9
} from "../runtime/docloader.ts" ;
8
10
import { lookupWebFinger } from "../webfinger/lookup.ts" ;
11
+ import { getTypeId } from "./type.ts" ;
9
12
import { type Collection , type Link , Object } from "./vocab.ts" ;
10
13
11
14
const logger = getLogger ( [ "fedify" , "vocab" , "lookup" ] ) ;
@@ -35,6 +38,12 @@ export interface LookupObjectOptions {
35
38
* @since 1.3.0
36
39
*/
37
40
userAgent ?: GetUserAgentOptions | string ;
41
+
42
+ /**
43
+ * The OpenTelemetry tracer provider.
44
+ * @since 1.3.0
45
+ */
46
+ tracerProvider ?: TracerProvider ;
38
47
}
39
48
40
49
const handleRegexp =
@@ -75,6 +84,49 @@ const handleRegexp =
75
84
export async function lookupObject (
76
85
identifier : string | URL ,
77
86
options : LookupObjectOptions = { } ,
87
+ ) : Promise < Object | null > {
88
+ if ( options . tracerProvider == null ) {
89
+ return await lookupObjectInternal ( identifier , options ) ;
90
+ }
91
+ const tracer = options . tracerProvider . getTracer (
92
+ metadata . name ,
93
+ metadata . version ,
94
+ ) ;
95
+ return await tracer . startActiveSpan (
96
+ "LookupObject" ,
97
+ async ( span ) => {
98
+ try {
99
+ const result = await lookupObjectInternal ( identifier , options ) ;
100
+ if ( result == null ) span . setStatus ( { code : SpanStatusCode . ERROR } ) ;
101
+ else {
102
+ if ( result . id != null ) {
103
+ span . setAttribute ( "activitypub.object.id" , result . id . href ) ;
104
+ }
105
+ span . setAttribute ( "activitypub.object.type" , getTypeId ( result ) . href ) ;
106
+ if ( result . replyTargetIds . length > 0 ) {
107
+ span . setAttribute (
108
+ "activitypub.object.in_reply_to" ,
109
+ result . replyTargetIds . map ( ( id ) => id . href ) ,
110
+ ) ;
111
+ }
112
+ }
113
+ return result ;
114
+ } catch ( error ) {
115
+ span . setStatus ( {
116
+ code : SpanStatusCode . ERROR ,
117
+ message : String ( error ) ,
118
+ } ) ;
119
+ throw error ;
120
+ } finally {
121
+ span . end ( ) ;
122
+ }
123
+ } ,
124
+ ) ;
125
+ }
126
+
127
+ async function lookupObjectInternal (
128
+ identifier : string | URL ,
129
+ options : LookupObjectOptions = { } ,
78
130
) : Promise < Object | null > {
79
131
const documentLoader = options . documentLoader ??
80
132
getDocumentLoader ( { userAgent : options . userAgent } ) ;
@@ -95,6 +147,7 @@ export async function lookupObject(
95
147
if ( document == null ) {
96
148
const jrd = await lookupWebFinger ( identifier , {
97
149
userAgent : options . userAgent ,
150
+ tracerProvider : options . tracerProvider ,
98
151
} ) ;
99
152
if ( jrd ?. links == null ) return null ;
100
153
for ( const l of jrd . links ) {
0 commit comments