/
api.go
112 lines (90 loc) · 4.01 KB
/
api.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package api
import (
"context"
"fmt"
"net/http"
"github.com/kiali/kiali/business"
"github.com/kiali/kiali/graph"
"github.com/kiali/kiali/graph/config/cytoscape"
"github.com/kiali/kiali/graph/telemetry/istio"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/observability"
"github.com/kiali/kiali/prometheus"
"github.com/kiali/kiali/prometheus/internalmetrics"
)
// GraphNamespaces generates a namespaces graph using the provided options
func GraphNamespaces(ctx context.Context, business *business.Layer, o graph.Options) (code int, config interface{}) {
var end observability.EndFunc
ctx, end = observability.StartSpan(ctx, "GraphNamespaces",
observability.Attribute("package", "api"),
)
defer end()
// time how long it takes to generate this graph
promtimer := internalmetrics.GetGraphGenerationTimePrometheusTimer(o.GetGraphKind(), o.TelemetryOptions.GraphType, o.InjectServiceNodes)
defer promtimer.ObserveDuration()
switch o.TelemetryVendor {
case graph.VendorIstio:
prom, err := prometheus.NewClient()
graph.CheckError(err)
code, config = graphNamespacesIstio(ctx, business, prom, o)
default:
graph.Error(fmt.Sprintf("TelemetryVendor [%s] not supported", o.TelemetryVendor))
}
// update metrics
internalmetrics.SetGraphNodes(o.GetGraphKind(), o.TelemetryOptions.GraphType, o.InjectServiceNodes, 0)
return code, config
}
// graphNamespacesIstio provides a test hook that accepts mock clients
func graphNamespacesIstio(ctx context.Context, business *business.Layer, prom *prometheus.Client, o graph.Options) (code int, config interface{}) {
// Create a 'global' object to store the business. Global only to the request.
globalInfo := graph.NewAppenderGlobalInfo()
globalInfo.Business = business
globalInfo.Context = ctx
trafficMap := istio.BuildNamespacesTrafficMap(ctx, o.TelemetryOptions, prom, globalInfo)
code, config = generateGraph(trafficMap, o)
return code, config
}
// GraphNode generates a node graph using the provided options
func GraphNode(ctx context.Context, business *business.Layer, o graph.Options) (code int, config interface{}) {
if len(o.Namespaces) != 1 {
graph.Error("Node graph does not support the 'namespaces' query parameter or the 'all' namespace")
}
// time how long it takes to generate this graph
promtimer := internalmetrics.GetGraphGenerationTimePrometheusTimer(o.GetGraphKind(), o.TelemetryOptions.GraphType, o.InjectServiceNodes)
defer promtimer.ObserveDuration()
switch o.TelemetryVendor {
case graph.VendorIstio:
prom, err := prometheus.NewClient()
graph.CheckError(err)
code, config = graphNodeIstio(ctx, business, prom, o)
default:
graph.Error(fmt.Sprintf("TelemetryVendor [%s] not supported", o.TelemetryVendor))
}
// update metrics
internalmetrics.SetGraphNodes(o.GetGraphKind(), o.TelemetryOptions.GraphType, o.InjectServiceNodes, 0)
return code, config
}
// graphNodeIstio provides a test hook that accepts mock clients
func graphNodeIstio(ctx context.Context, business *business.Layer, client *prometheus.Client, o graph.Options) (code int, config interface{}) {
// Create a 'global' object to store the business. Global only to the request.
globalInfo := graph.NewAppenderGlobalInfo()
globalInfo.Business = business
globalInfo.Context = ctx
trafficMap, _ := istio.BuildNodeTrafficMap(o.TelemetryOptions, client, globalInfo)
code, config = generateGraph(trafficMap, o)
return code, config
}
func generateGraph(trafficMap graph.TrafficMap, o graph.Options) (int, interface{}) {
log.Tracef("Generating config for [%s] graph...", o.ConfigVendor)
promtimer := internalmetrics.GetGraphMarshalTimePrometheusTimer(o.GetGraphKind(), o.TelemetryOptions.GraphType, o.InjectServiceNodes)
defer promtimer.ObserveDuration()
var vendorConfig interface{}
switch o.ConfigVendor {
case graph.VendorCytoscape:
vendorConfig = cytoscape.NewConfig(trafficMap, o.ConfigOptions)
default:
graph.Error(fmt.Sprintf("ConfigVendor [%s] not supported", o.ConfigVendor))
}
log.Tracef("Done generating config for [%s] graph", o.ConfigVendor)
return http.StatusOK, vendorConfig
}