@@ -3,10 +3,10 @@ package invoke
3
3
import (
4
4
"context"
5
5
"sync/atomic"
6
- "time"
7
6
8
7
"go.opentelemetry.io/otel/attribute"
9
8
otelCodes "go.opentelemetry.io/otel/codes"
9
+ "go.opentelemetry.io/otel/metric"
10
10
api "go.opentelemetry.io/otel/metric"
11
11
"go.opentelemetry.io/otel/trace"
12
12
@@ -68,16 +68,10 @@ type DirectInvoker struct {
68
68
// LookupSubject
69
69
sp SubjectPermission
70
70
71
- // Metrics
72
- checkCounter api.Int64Counter
73
- lookupEntityCounter api.Int64Counter
74
- lookupSubjectCounter api.Int64Counter
75
- subjectPermissionCounter api.Int64Counter
76
-
77
- checkDurationHistogram api.Int64Histogram
78
- lookupEntityDurationHistogram api.Int64Histogram
79
- lookupSubjectDurationHistogram api.Int64Histogram
80
- subjectPermissionDurationHistogram api.Int64Histogram
71
+ checkHistogram api.Int64Histogram
72
+ lookupEntityHistogram api.Int64Histogram
73
+ lookupSubjectHistogram api.Int64Histogram
74
+ subjectPermissionHistogram api.Int64Histogram
81
75
}
82
76
83
77
// NewDirectInvoker is a constructor for DirectInvoker.
@@ -92,20 +86,17 @@ func NewDirectInvoker(
92
86
sp SubjectPermission ,
93
87
) * DirectInvoker {
94
88
return & DirectInvoker {
95
- schemaReader : schemaReader ,
96
- dataReader : dataReader ,
97
- cc : cc ,
98
- ec : ec ,
99
- lo : lo ,
100
- sp : sp ,
101
- checkCounter : telemetry .NewCounter (internal .Meter , "check_count" , "Number of permission checks performed" ),
102
- lookupEntityCounter : telemetry .NewCounter (internal .Meter , "lookup_entity_count" , "Number of permission lookup entity performed" ),
103
- lookupSubjectCounter : telemetry .NewCounter (internal .Meter , "lookup_subject_count" , "Number of permission lookup subject performed" ),
104
- subjectPermissionCounter : telemetry .NewCounter (internal .Meter , "subject_permission_count" , "Number of subject permission performed" ),
105
- checkDurationHistogram : telemetry .NewHistogram (internal .Meter , "check_duration" , "microseconds" , "Duration of checks in microseconds" ),
106
- lookupEntityDurationHistogram : telemetry .NewHistogram (internal .Meter , "lookup_entity_duration" , "microseconds" , "Duration of lookup entity duration in microseconds" ),
107
- lookupSubjectDurationHistogram : telemetry .NewHistogram (internal .Meter , "lookup_subject_duration" , "microseconds" , "Duration of lookup subject duration in microseconds" ),
108
- subjectPermissionDurationHistogram : telemetry .NewHistogram (internal .Meter , "subject_permission_duration" , "microseconds" , "Duration of subject permission duration in microseconds" ),
89
+ schemaReader : schemaReader ,
90
+ dataReader : dataReader ,
91
+ cc : cc ,
92
+ ec : ec ,
93
+ lo : lo ,
94
+ sp : sp ,
95
+ checkHistogram : telemetry .NewHistogram (internal .Meter , "check" , "amount" , "Number of checks" ),
96
+
97
+ lookupEntityHistogram : telemetry .NewHistogram (internal .Meter , "lookup_entity" , "amount" , "Number of lookup entity" ),
98
+ lookupSubjectHistogram : telemetry .NewHistogram (internal .Meter , "lookup_subject" , "amount" , "Number of lookup subject" ),
99
+ subjectPermissionHistogram : telemetry .NewHistogram (internal .Meter , "subject_permission" , "amount" , "Number of subject permission" ),
109
100
}
110
101
}
111
102
@@ -120,8 +111,13 @@ func (invoker *DirectInvoker) Check(ctx context.Context, request *base.Permissio
120
111
attribute.KeyValue {Key : "subject" , Value : attribute .StringValue (tuple .SubjectToString (request .GetSubject ()))},
121
112
))
122
113
defer span .End ()
123
-
124
- start := time .Now ()
114
+ invoker .checkHistogram .Record (ctx , 1 ,
115
+ metric .WithAttributeSet (
116
+ attribute .NewSet (
117
+ attribute.KeyValue {Key : "subject_id" , Value : attribute .StringValue (request .GetSubject ().GetId ())},
118
+ attribute.KeyValue {Key : "subject_type" , Value : attribute .StringValue (request .GetSubject ().GetType ())},
119
+ )),
120
+ )
125
121
126
122
// Validate the depth of the request.
127
123
err = checkDepth (request )
@@ -186,15 +182,10 @@ func (invoker *DirectInvoker) Check(ctx context.Context, request *base.Permissio
186
182
},
187
183
}, err
188
184
}
189
- duration := time .Since (start )
190
- invoker .checkDurationHistogram .Record (ctx , duration .Microseconds ())
191
185
192
186
// increaseCheckCount increments the CheckCount value in the response metadata by 1.
193
187
atomic .AddInt32 (& response .GetMetadata ().CheckCount , + 1 )
194
188
195
- // Increase the check count in the metrics.
196
- invoker .checkCounter .Add (ctx , 1 )
197
-
198
189
span .SetAttributes (attribute.KeyValue {Key : "can" , Value : attribute .StringValue (response .GetCan ().String ())})
199
190
return
200
191
}
@@ -245,8 +236,6 @@ func (invoker *DirectInvoker) LookupEntity(ctx context.Context, request *base.Pe
245
236
))
246
237
defer span .End ()
247
238
248
- start := time .Now ()
249
-
250
239
// Set SnapToken if not provided
251
240
if request .GetMetadata ().GetSnapToken () == "" { // Check if the request has a SnapToken.
252
241
var st token.SnapToken
@@ -271,11 +260,7 @@ func (invoker *DirectInvoker) LookupEntity(ctx context.Context, request *base.Pe
271
260
272
261
resp , err := invoker .lo .LookupEntity (ctx , request )
273
262
274
- duration := time .Since (start )
275
- invoker .lookupEntityDurationHistogram .Record (ctx , duration .Microseconds ())
276
-
277
- // Increase the lookup entity count in the metrics.
278
- invoker .lookupEntityCounter .Add (ctx , 1 )
263
+ invoker .lookupEntityHistogram .Record (ctx , 1 )
279
264
280
265
return resp , err
281
266
}
@@ -292,8 +277,6 @@ func (invoker *DirectInvoker) LookupEntityStream(ctx context.Context, request *b
292
277
))
293
278
defer span .End ()
294
279
295
- start := time .Now ()
296
-
297
280
// Set SnapToken if not provided
298
281
if request .GetMetadata ().GetSnapToken () == "" { // Check if the request has a SnapToken.
299
282
var st token.SnapToken
@@ -318,11 +301,7 @@ func (invoker *DirectInvoker) LookupEntityStream(ctx context.Context, request *b
318
301
319
302
resp := invoker .lo .LookupEntityStream (ctx , request , server )
320
303
321
- duration := time .Since (start )
322
- invoker .lookupEntityDurationHistogram .Record (ctx , duration .Microseconds ())
323
-
324
- // Increase the lookup entity count in the metrics.
325
- invoker .lookupEntityCounter .Add (ctx , 1 )
304
+ invoker .lookupEntityHistogram .Record (ctx , 1 )
326
305
327
306
return resp
328
307
}
@@ -338,8 +317,6 @@ func (invoker *DirectInvoker) LookupSubject(ctx context.Context, request *base.P
338
317
))
339
318
defer span .End ()
340
319
341
- start := time .Now ()
342
-
343
320
// Check if the request has a SnapToken. If not, a SnapToken is set.
344
321
if request .GetMetadata ().GetSnapToken () == "" {
345
322
// Create an instance of SnapToken
@@ -370,11 +347,7 @@ func (invoker *DirectInvoker) LookupSubject(ctx context.Context, request *base.P
370
347
371
348
resp , err := invoker .lo .LookupSubject (ctx , request )
372
349
373
- duration := time .Now ().Sub (start )
374
- invoker .lookupSubjectDurationHistogram .Record (ctx , duration .Microseconds ())
375
-
376
- // Increase the lookup subject count in the metrics.
377
- invoker .lookupSubjectCounter .Add (ctx , 1 )
350
+ invoker .lookupSubjectHistogram .Record (ctx , 1 )
378
351
379
352
// Call the LookupSubject function of the ls field in the invoker, pass the context and request,
380
353
// and return its response and error
@@ -391,8 +364,6 @@ func (invoker *DirectInvoker) SubjectPermission(ctx context.Context, request *ba
391
364
))
392
365
defer span .End ()
393
366
394
- start := time .Now ()
395
-
396
367
// Check if the request has a SnapToken. If not, a SnapToken is set.
397
368
if request .GetMetadata ().GetSnapToken () == "" {
398
369
// Create an instance of SnapToken
@@ -422,11 +393,7 @@ func (invoker *DirectInvoker) SubjectPermission(ctx context.Context, request *ba
422
393
}
423
394
resp , err := invoker .sp .SubjectPermission (ctx , request )
424
395
425
- duration := time .Now ().Sub (start )
426
- invoker .subjectPermissionDurationHistogram .Record (ctx , duration .Microseconds ())
427
-
428
- // Increase the subject permission count in the metrics.
429
- invoker .subjectPermissionCounter .Add (ctx , 1 )
396
+ invoker .subjectPermissionHistogram .Record (ctx , 1 )
430
397
431
398
// Call the SubjectPermission function of the ls field in the invoker, pass the context and request,
432
399
// and return its response and error
0 commit comments