func QueryPrometheus()

in recipes/beyla-service-graph/graphgen/internal/query.go [42:74]


func QueryPrometheus(
	ctx context.Context,
	client api.Client,
	queryArgs QueryArgs,
) (*Graph, error) {
	promApi := v1.NewAPI(client)
	query := getQuery(queryArgs)
	slog.InfoContext(ctx, "logging", "query", query)
	res, warnings, err := promApi.Query(ctx, query, time.Now())
	if err != nil {
		return nil, err
	}

	if len(warnings) != 0 {
		slog.WarnContext(ctx, "Warnings from promQL query", "warnings", warnings)
	}
	slog.InfoContext(ctx, "Got metrics", "metrics", res)
	slog.InfoContext(ctx, "type", "type", res.Type())

	vec, ok := res.(model.Vector)
	if !ok {
		return nil, fmt.Errorf("couldn't cast %v to vector", res)
	}

	graph := NewGraph()
	for _, sample := range vec {
		labels := sample.Metric
		client := &Node{Ip: string(labels[clientIpKey]), Name: string(labels[clientKey])}
		server := &Node{Ip: string(labels[serverIpKey]), Name: string(labels[serverKey])}
		graph.AddEdge(client, server)
	}
	return graph, nil
}