-
Notifications
You must be signed in to change notification settings - Fork 20
/
proxy.go
59 lines (49 loc) · 1.33 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package proxy
import (
"context"
"reflect"
"go.opencensus.io/tag"
"github.com/Filecoin-Titan/titan/api"
"github.com/Filecoin-Titan/titan/metrics"
)
func MetricedCandidateAPI(a api.Candidate) api.Candidate {
var out api.CandidateStruct
proxy(a, &out)
return &out
}
func MetricedSchedulerAPI(a api.Scheduler) api.Scheduler {
var out api.SchedulerStruct
proxy(a, &out)
return &out
}
func MetricedEdgeAPI(a api.Edge) api.Edge {
var out api.EdgeStruct
proxy(a, &out)
return &out
}
func MetricedLocatorAPI(a api.Locator) api.Locator {
var out api.LocatorStruct
proxy(a, &out)
return &out
}
func proxy(in interface{}, outstr interface{}) {
outs := api.GetInternalStructs(outstr)
for _, out := range outs {
rint := reflect.ValueOf(out).Elem()
ra := reflect.ValueOf(in)
for f := 0; f < rint.NumField(); f++ {
field := rint.Type().Field(f)
fn := ra.MethodByName(field.Name)
rint.Field(f).Set(reflect.MakeFunc(field.Type, func(args []reflect.Value) (results []reflect.Value) {
ctx := args[0].Interface().(context.Context)
// upsert function name into context
ctx, _ = tag.New(ctx, tag.Upsert(metrics.Endpoint, field.Name))
stop := metrics.Timer(ctx, metrics.APIRequestDuration)
defer stop()
// pass tagged ctx back into function call
args[0] = reflect.ValueOf(ctx)
return fn.Call(args)
}))
}
}
}