-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: profilecli query-blocks series (#3610)
* feat: profilecli query-blocks series * pr comments
- Loading branch information
Showing
6 changed files
with
186 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
|
||
typesv1 "github.com/grafana/pyroscope/api/gen/proto/go/types/v1" | ||
) | ||
|
||
func outputSeries(result []*typesv1.Labels) error { | ||
enc := json.NewEncoder(os.Stdout) | ||
m := make(map[string]interface{}) | ||
for _, s := range result { | ||
clear(m) | ||
for _, l := range s.Labels { | ||
m[l.Name] = l.Value | ||
} | ||
if err := enc.Encode(m); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math" | ||
|
||
"connectrpc.com/connect" | ||
"github.com/go-kit/log/level" | ||
"github.com/pkg/errors" | ||
|
||
ingestv1 "github.com/grafana/pyroscope/api/gen/proto/go/ingester/v1" | ||
"github.com/grafana/pyroscope/pkg/objstore" | ||
objstoreclient "github.com/grafana/pyroscope/pkg/objstore/client" | ||
"github.com/grafana/pyroscope/pkg/objstore/providers/filesystem" | ||
"github.com/grafana/pyroscope/pkg/objstore/providers/gcs" | ||
"github.com/grafana/pyroscope/pkg/phlaredb" | ||
) | ||
|
||
type queryBlocksParams struct { | ||
LocalPath string | ||
BucketName string | ||
BlockIds []string | ||
TenantID string | ||
ObjectStoreType string | ||
Query string | ||
} | ||
|
||
type queryBlocksSeriesParams struct { | ||
*queryBlocksParams | ||
LabelNames []string | ||
} | ||
|
||
func addQueryBlocksParams(queryCmd commander) *queryBlocksParams { | ||
params := new(queryBlocksParams) | ||
queryCmd.Flag("local-path", "Path to blocks directory.").Default("./data/anonymous/local").StringVar(¶ms.LocalPath) | ||
queryCmd.Flag("bucket-name", "The name of the object storage bucket.").StringVar(¶ms.BucketName) | ||
queryCmd.Flag("object-store-type", "The type of the object storage (e.g., gcs).").Default("gcs").StringVar(¶ms.ObjectStoreType) | ||
queryCmd.Flag("block-ids", "List of blocks ids to query on").StringsVar(¶ms.BlockIds) | ||
queryCmd.Flag("tenant-id", "Tenant id of the queried block for remote bucket").StringVar(¶ms.TenantID) | ||
queryCmd.Flag("query", "Label selector to query.").Default("{}").StringVar(¶ms.Query) | ||
return params | ||
} | ||
|
||
func addQueryBlocksSeriesParams(queryCmd commander) *queryBlocksSeriesParams { | ||
params := new(queryBlocksSeriesParams) | ||
params.queryBlocksParams = addQueryBlocksParams(queryCmd) | ||
queryCmd.Flag("label-names", "Filter returned labels to the supplied label names. Without any filter all labels are returned.").StringsVar(¶ms.LabelNames) | ||
return params | ||
} | ||
|
||
func queryBlocksSeries(ctx context.Context, params *queryBlocksSeriesParams) error { | ||
level.Info(logger).Log("msg", "query-block series", "labelNames", fmt.Sprintf("%v", params.LabelNames), | ||
"blockIds", fmt.Sprintf("%v", params.BlockIds), "localPath", params.LocalPath, "bucketName", params.BucketName, "tenantId", params.TenantID) | ||
|
||
bucket, err := getBucket(ctx, params) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
blockQuerier := phlaredb.NewBlockQuerier(ctx, bucket) | ||
|
||
var from, to int64 | ||
from, to = math.MaxInt64, math.MinInt64 | ||
var targetBlockQueriers phlaredb.Queriers | ||
for _, blockId := range params.queryBlocksParams.BlockIds { | ||
meta, err := blockQuerier.BlockMeta(ctx, blockId) | ||
if err != nil { | ||
return err | ||
} | ||
from = min(from, meta.MinTime.Time().UnixMilli()) | ||
to = max(to, meta.MaxTime.Time().UnixMilli()) | ||
targetBlockQueriers = append(targetBlockQueriers, phlaredb.NewSingleBlockQuerierFromMeta(ctx, bucket, meta)) | ||
} | ||
|
||
response, err := targetBlockQueriers.Series(ctx, connect.NewRequest( | ||
&ingestv1.SeriesRequest{ | ||
Start: from, | ||
End: to, | ||
Matchers: []string{params.Query}, | ||
LabelNames: params.LabelNames, | ||
}, | ||
)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return outputSeries(response.Msg.LabelsSet) | ||
} | ||
|
||
func getBucket(ctx context.Context, params *queryBlocksSeriesParams) (objstore.Bucket, error) { | ||
if params.BucketName != "" { | ||
return getRemoteBucket(ctx, params) | ||
} else { | ||
return filesystem.NewBucket(params.LocalPath) | ||
} | ||
} | ||
|
||
func getRemoteBucket(ctx context.Context, params *queryBlocksSeriesParams) (objstore.Bucket, error) { | ||
if params.TenantID == "" { | ||
return nil, errors.New("specify tenant id for remote bucket") | ||
} | ||
return objstoreclient.NewBucket(ctx, objstoreclient.Config{ | ||
StorageBackendConfig: objstoreclient.StorageBackendConfig{ | ||
Backend: params.ObjectStoreType, | ||
GCS: gcs.Config{ | ||
BucketName: params.BucketName, | ||
}, | ||
}, | ||
StoragePrefix: fmt.Sprintf("%s/phlaredb", params.TenantID), | ||
}, params.BucketName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters