-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tags for Flows along with routes to set and get (#2297)
- add tags to telemetry to provide additional visibility - later these can be added to otel metrics etc
- Loading branch information
1 parent
fb852aa
commit c294c0b
Showing
5 changed files
with
363 additions
and
196 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,84 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
|
||
"github.com/PeerDB-io/peer-flow/generated/protos" | ||
"github.com/PeerDB-io/peer-flow/tags" | ||
) | ||
|
||
func (h *FlowRequestHandler) flowExists(ctx context.Context, flowName string) (bool, error) { | ||
var exists bool | ||
err := h.pool.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM flows WHERE name = $1)", flowName).Scan(&exists) | ||
if err != nil { | ||
slog.Error("error checking if flow exists", slog.Any("error", err)) | ||
return false, err | ||
} | ||
|
||
slog.Info(fmt.Sprintf("flow %s exists: %t", flowName, exists)) | ||
return exists, nil | ||
} | ||
|
||
func (h *FlowRequestHandler) CreateOrReplaceFlowTags( | ||
ctx context.Context, | ||
in *protos.CreateOrReplaceFlowTagsRequest, | ||
) (*protos.CreateOrReplaceFlowTagsResponse, error) { | ||
flowName := in.FlowName | ||
|
||
exists, err := h.flowExists(ctx, flowName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !exists { | ||
slog.Error("flow does not exist", slog.String("flow_name", flowName)) | ||
return nil, fmt.Errorf("flow %s does not exist", flowName) | ||
} | ||
|
||
tags := make(map[string]string, len(in.Tags)) | ||
for _, tag := range in.Tags { | ||
tags[tag.Key] = tag.Value | ||
} | ||
|
||
_, err = h.pool.Exec(ctx, "UPDATE flows SET tags = $1 WHERE name = $2", tags, flowName) | ||
if err != nil { | ||
slog.Error("error updating flow tags", slog.Any("error", err)) | ||
return nil, err | ||
} | ||
|
||
return &protos.CreateOrReplaceFlowTagsResponse{ | ||
FlowName: flowName, | ||
}, nil | ||
} | ||
|
||
func (h *FlowRequestHandler) GetFlowTags(ctx context.Context, in *protos.GetFlowTagsRequest) (*protos.GetFlowTagsResponse, error) { | ||
flowName := in.FlowName | ||
|
||
exists, err := h.flowExists(ctx, flowName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !exists { | ||
slog.Error("flow does not exist", slog.String("flow_name", flowName)) | ||
return nil, fmt.Errorf("flow %s does not exist", flowName) | ||
} | ||
|
||
tags, err := tags.GetTags(ctx, h.pool, flowName) | ||
if err != nil { | ||
slog.Error("error getting flow tags", slog.Any("error", err)) | ||
return nil, err | ||
} | ||
|
||
protosTags := make([]*protos.FlowTag, 0, len(tags)) | ||
for key, value := range tags { | ||
protosTags = append(protosTags, &protos.FlowTag{Key: key, Value: value}) | ||
} | ||
|
||
return &protos.GetFlowTagsResponse{ | ||
FlowName: flowName, | ||
Tags: protosTags, | ||
}, 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,24 @@ | ||
package tags | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"github.com/jackc/pgx/v5/pgxpool" | ||
) | ||
|
||
func GetTags(ctx context.Context, catalogPool *pgxpool.Pool, flowName string) (map[string]string, error) { | ||
var tags map[string]string | ||
|
||
err := catalogPool.QueryRow(ctx, "SELECT tags FROM flows WHERE name = $1", flowName).Scan(&tags) | ||
if err != nil { | ||
slog.Error("error getting flow tags", slog.Any("error", err)) | ||
return nil, err | ||
} | ||
|
||
if tags == nil { | ||
tags = make(map[string]string) | ||
} | ||
|
||
return tags, 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,2 @@ | ||
ALTER TABLE flows | ||
ADD COLUMN tags JSONB; |
Oops, something went wrong.