github.com/kiali/kiali@v1.84.0/graph/api/api.go (about)

     1  package api
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  
     8  	"github.com/kiali/kiali/business"
     9  	"github.com/kiali/kiali/graph"
    10  	"github.com/kiali/kiali/graph/config/cytoscape"
    11  	"github.com/kiali/kiali/graph/telemetry/istio"
    12  	"github.com/kiali/kiali/log"
    13  	"github.com/kiali/kiali/observability"
    14  	"github.com/kiali/kiali/prometheus"
    15  	"github.com/kiali/kiali/prometheus/internalmetrics"
    16  )
    17  
    18  // GraphNamespaces generates a namespaces graph using the provided options
    19  func GraphNamespaces(ctx context.Context, business *business.Layer, o graph.Options) (code int, config interface{}) {
    20  	var end observability.EndFunc
    21  	ctx, end = observability.StartSpan(ctx, "GraphNamespaces",
    22  		observability.Attribute("package", "api"),
    23  	)
    24  	defer end()
    25  	// time how long it takes to generate this graph
    26  	promtimer := internalmetrics.GetGraphGenerationTimePrometheusTimer(o.GetGraphKind(), o.TelemetryOptions.GraphType, o.InjectServiceNodes)
    27  	defer promtimer.ObserveDuration()
    28  
    29  	switch o.TelemetryVendor {
    30  	case graph.VendorIstio:
    31  		prom, err := prometheus.NewClient()
    32  		graph.CheckError(err)
    33  		code, config = graphNamespacesIstio(ctx, business, prom, o)
    34  	default:
    35  		graph.Error(fmt.Sprintf("TelemetryVendor [%s] not supported", o.TelemetryVendor))
    36  	}
    37  
    38  	// update metrics
    39  	internalmetrics.SetGraphNodes(o.GetGraphKind(), o.TelemetryOptions.GraphType, o.InjectServiceNodes, 0)
    40  
    41  	return code, config
    42  }
    43  
    44  // graphNamespacesIstio provides a test hook that accepts mock clients
    45  func graphNamespacesIstio(ctx context.Context, business *business.Layer, prom *prometheus.Client, o graph.Options) (code int, config interface{}) {
    46  
    47  	// Create a 'global' object to store the business. Global only to the request.
    48  	globalInfo := graph.NewAppenderGlobalInfo()
    49  	globalInfo.Business = business
    50  	globalInfo.Context = ctx
    51  
    52  	trafficMap := istio.BuildNamespacesTrafficMap(ctx, o.TelemetryOptions, prom, globalInfo)
    53  	code, config = generateGraph(trafficMap, o)
    54  
    55  	return code, config
    56  }
    57  
    58  // GraphNode generates a node graph using the provided options
    59  func GraphNode(ctx context.Context, business *business.Layer, o graph.Options) (code int, config interface{}) {
    60  	if len(o.Namespaces) != 1 {
    61  		graph.Error("Node graph does not support the 'namespaces' query parameter or the 'all' namespace")
    62  	}
    63  
    64  	// time how long it takes to generate this graph
    65  	promtimer := internalmetrics.GetGraphGenerationTimePrometheusTimer(o.GetGraphKind(), o.TelemetryOptions.GraphType, o.InjectServiceNodes)
    66  	defer promtimer.ObserveDuration()
    67  
    68  	switch o.TelemetryVendor {
    69  	case graph.VendorIstio:
    70  		prom, err := prometheus.NewClient()
    71  		graph.CheckError(err)
    72  		code, config = graphNodeIstio(ctx, business, prom, o)
    73  	default:
    74  		graph.Error(fmt.Sprintf("TelemetryVendor [%s] not supported", o.TelemetryVendor))
    75  	}
    76  	// update metrics
    77  	internalmetrics.SetGraphNodes(o.GetGraphKind(), o.TelemetryOptions.GraphType, o.InjectServiceNodes, 0)
    78  
    79  	return code, config
    80  }
    81  
    82  // graphNodeIstio provides a test hook that accepts mock clients
    83  func graphNodeIstio(ctx context.Context, business *business.Layer, client *prometheus.Client, o graph.Options) (code int, config interface{}) {
    84  
    85  	// Create a 'global' object to store the business. Global only to the request.
    86  	globalInfo := graph.NewAppenderGlobalInfo()
    87  	globalInfo.Business = business
    88  	globalInfo.Context = ctx
    89  
    90  	trafficMap, _ := istio.BuildNodeTrafficMap(o.TelemetryOptions, client, globalInfo)
    91  	code, config = generateGraph(trafficMap, o)
    92  
    93  	return code, config
    94  }
    95  
    96  func generateGraph(trafficMap graph.TrafficMap, o graph.Options) (int, interface{}) {
    97  	log.Tracef("Generating config for [%s] graph...", o.ConfigVendor)
    98  
    99  	promtimer := internalmetrics.GetGraphMarshalTimePrometheusTimer(o.GetGraphKind(), o.TelemetryOptions.GraphType, o.InjectServiceNodes)
   100  	defer promtimer.ObserveDuration()
   101  
   102  	var vendorConfig interface{}
   103  	switch o.ConfigVendor {
   104  	case graph.VendorCytoscape:
   105  		vendorConfig = cytoscape.NewConfig(trafficMap, o.ConfigOptions)
   106  	default:
   107  		graph.Error(fmt.Sprintf("ConfigVendor [%s] not supported", o.ConfigVendor))
   108  	}
   109  
   110  	log.Tracef("Done generating config for [%s] graph", o.ConfigVendor)
   111  	return http.StatusOK, vendorConfig
   112  }