-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(pubsub/v2): add single message transform samples #5352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+194
−0
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9007519
feat(pubsub/v2): add single message transform samples
hongalex 981723a
address review comments
hongalex 6524e38
lingering review comment
hongalex 2eb2dd1
Merge branch 'main' into pubsub-v2-smt
hongalex 5697683
Merge branch 'main' into pubsub-v2-smt
briandorsey 7a67e80
Merge branch 'main' into pubsub-v2-smt
hongalex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,67 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package subscriptions | ||
|
||
// [START pubsub_create_subscription_with_smt] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"cloud.google.com/go/pubsub/v2" | ||
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb" | ||
) | ||
|
||
// createSubscriptionWithSMT creates a subscription with a single message transform function applied. | ||
func createSubscriptionWithSMT(w io.Writer, projectID, topicID, subID string) error { | ||
// projectID := "my-project-id" | ||
// topicID := "my-topic" | ||
// subID := "my-sub" | ||
ctx := context.Background() | ||
client, err := pubsub.NewClient(ctx, projectID) | ||
if err != nil { | ||
return fmt.Errorf("pubsub.NewClient: %w", err) | ||
} | ||
defer client.Close() | ||
|
||
code := `function redactSSN(message, metadata) { | ||
const data = JSON.parse(message.data); | ||
delete data['ssn']; | ||
message.data = JSON.stringify(data); | ||
return message; | ||
}` | ||
hongalex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
transform := &pubsubpb.MessageTransform{ | ||
Transform: &pubsubpb.MessageTransform_JavascriptUdf{ | ||
JavascriptUdf: &pubsubpb.JavaScriptUDF{ | ||
FunctionName: "redactSSN", | ||
Code: code, | ||
}, | ||
}, | ||
} | ||
|
||
sub := &pubsubpb.Subscription{ | ||
Topic: fmt.Sprintf("projects/%s/topics/%s", projectID, topicID), | ||
MessageTransforms: []*pubsubpb.MessageTransform{transform}, | ||
} | ||
hongalex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sub, err = client.SubscriptionAdminClient.CreateSubscription(ctx, sub) | ||
if err != nil { | ||
return fmt.Errorf("CreateSubscription: %w", err) | ||
} | ||
fmt.Fprintf(w, "Created subscription with message transform: %v\n", sub) | ||
return nil | ||
} | ||
|
||
// [END pubsub_create_subscription_with_smt] |
This file contains hidden or 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 |
---|---|---|
|
@@ -29,12 +29,15 @@ import ( | |
|
||
"cloud.google.com/go/bigquery" | ||
"cloud.google.com/go/pubsub/v2" | ||
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb" | ||
hongalex marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
pb "cloud.google.com/go/pubsub/v2/apiv1/pubsubpb" | ||
"cloud.google.com/go/pubsub/v2/pstest" | ||
"cloud.google.com/go/storage" | ||
trace "cloud.google.com/go/trace/apiv1" | ||
"cloud.google.com/go/trace/apiv1/tracepb" | ||
"google.golang.org/api/iterator" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil" | ||
) | ||
|
@@ -730,3 +733,39 @@ func createOrGetStorageBucket(projectID, bucketID string) error { | |
|
||
return nil | ||
} | ||
|
||
func TestCreateSubscriptionWithSMT(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If (and only if) you decide to pass a Topic struct to createSubscriptionWithSMT, it would probably make sense to update the test to the same format I used in #5381. (Avoids a race condition when setting the topic variable.) Not needed if createSubscriptionWithSMT stays as is. |
||
ctx := context.Background() | ||
tc := testutil.SystemTest(t) | ||
client := setup(t) | ||
|
||
smtSubID := subID + "-smt" | ||
smtTopicID := topicID + "-smt" | ||
testutil.Retry(t, 10, time.Second, func(r *testutil.R) { | ||
_, err := client.TopicAdminClient.CreateTopic(ctx, &pubsubpb.Topic{ | ||
hongalex marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Name: fmt.Sprintf("projects/%s/topics/%s", tc.ProjectID, smtTopicID), | ||
}) | ||
if err != nil { | ||
st, ok := status.FromError(err) | ||
if !ok { | ||
r.Errorf("CreateTopic failed with unknown err: %v", err) | ||
} | ||
// Don't return error if topic already exists, just use that for the test. | ||
if st.Code() != codes.AlreadyExists { | ||
r.Errorf("CreateTopic: %v", err) | ||
} | ||
hongalex marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
}) | ||
|
||
testutil.Retry(t, 10, time.Second, func(r *testutil.R) { | ||
buf := new(bytes.Buffer) | ||
if err := createSubscriptionWithSMT(buf, tc.ProjectID, smtTopicID, smtSubID); err != nil { | ||
r.Errorf("failed to create subscription with SMT: %v", err) | ||
} | ||
got := buf.String() | ||
want := "Created subscription with message transform" | ||
if !strings.Contains(got, want) { | ||
r.Errorf("got: %s, want: %v", got, want) | ||
} | ||
}) | ||
} |
This file contains hidden or 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,67 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package topics | ||
|
||
// [START pubsub_create_topic_with_smt] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"cloud.google.com/go/pubsub/v2" | ||
"cloud.google.com/go/pubsub/v2/apiv1/pubsubpb" | ||
) | ||
|
||
// createTopicWithSMT creates a topic with a single message transform function applied. | ||
func createTopicWithSMT(w io.Writer, projectID, topicID string) error { | ||
// projectID := "my-project-id" | ||
// topicID := "my-topic" | ||
ctx := context.Background() | ||
client, err := pubsub.NewClient(ctx, projectID) | ||
if err != nil { | ||
return fmt.Errorf("pubsub.NewClient: %w", err) | ||
} | ||
defer client.Close() | ||
|
||
code := `function redactSSN(message, metadata) { | ||
const data = JSON.parse(message.data); | ||
delete data['ssn']; | ||
message.data = JSON.stringify(data); | ||
return message; | ||
}` | ||
hongalex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
transform := &pubsubpb.MessageTransform{ | ||
Transform: &pubsubpb.MessageTransform_JavascriptUdf{ | ||
JavascriptUdf: &pubsubpb.JavaScriptUDF{ | ||
FunctionName: "redactSSN", | ||
Code: code, | ||
}, | ||
}, | ||
} | ||
|
||
topic := &pubsubpb.Topic{ | ||
Name: fmt.Sprintf("projects/%s/topics/%s", projectID, topicID), | ||
MessageTransforms: []*pubsubpb.MessageTransform{transform}, | ||
} | ||
|
||
topic, err = client.TopicAdminClient.CreateTopic(ctx, topic) | ||
if err != nil { | ||
return fmt.Errorf("CreateTopic: %w", err) | ||
} | ||
|
||
fmt.Fprintf(w, "Created topic with message transform: %v\n", topic) | ||
return nil | ||
} | ||
|
||
// [END pubsub_create_topic_with_smt] |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example function signature is different than the V1 version. Is that by design?