Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Replace deprecated github.com/golang/protobuf package #427

Closed
wants to merge 1 commit into from

Conversation

zhsj
Copy link

@zhsj zhsj commented Dec 22, 2022

This replaces usage of proto.{Float64,Int32,Int64,String,Uint32,Uint64}, which doesn't break the interface.

And replaces ptypes.TimestampProto by google.golang.org/protobuf/types/known/timestamppb. This works since github.com/golang/protobuf has been bumped to v1.5.2, which makes ptypes.TimestampProto an alias to timestamppb.Timestamp. This is only used test (openmetrics_create_test.go), which won't break interfaces.

Updates: #317

Signed-off-by: Shengjing Zhu zhsj@debian.org

@zhsj
Copy link
Author

zhsj commented Dec 22, 2022

Some format changes in the PR is caused by gopls format, I can leave them untouched if you don't like that.

And this PR doesn't replace proto.MarshalTextString. After golang/protobuf#1442 is fixed, we can only use google.golang.org/protobuf and still consume v1 message from client_model package.

@zhsj
Copy link
Author

zhsj commented Dec 22, 2022

For proto.MarshalTextString, I think we can do like this

diff --git a/expfmt/encode.go b/expfmt/encode.go
index 64dc0eb..73e84ee 100644
--- a/expfmt/encode.go
+++ b/expfmt/encode.go
@@ -18,9 +18,10 @@ import (
        "io"
        "net/http"
 
-       "github.com/golang/protobuf/proto" //nolint:staticcheck // Ignore SA1019. Need to keep deprecated package for compatibility.
        "github.com/matttproud/golang_protobuf_extensions/pbutil"
        "github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg"
+       "google.golang.org/protobuf/encoding/prototext"
+       "google.golang.org/protobuf/runtime/protoimpl"
 
        dto "github.com/prometheus/client_model/go"
 )
@@ -133,7 +134,11 @@ func NewEncoder(w io.Writer, format Format) Encoder {
        case FmtProtoText:
                return encoderCloser{
                        encode: func(v *dto.MetricFamily) error {
-                               _, err := fmt.Fprintln(w, proto.MarshalTextString(v))
+                               m, err := prototext.Marshal(protoimpl.X.ProtoMessageV2Of(v))
+                               if err != nil {
+                                       return err
+                               }
+                               _, err = w.Write(m)
                                return err
                        },
                        close: func() error { return nil },

This uses google.golang.org/protobuf/runtime/protoimpl. Regarding golang/protobuf#1442, the grpc people feel it's a temporary solution to use that package. But well I think it's already in the public exported place..

@roidelapluie
Copy link
Member

CircleCI Pipeline — Could not find a usable config.yml, you may have revoked the CircleCI OAuth app.

@zhsj
Copy link
Author

zhsj commented Dec 22, 2022

CircleCI Pipeline — Could not find a usable config.yml, you may have revoked the CircleCI OAuth app.

It's now fixed. I logged in CircleCI again. Probably I've revoked it before.

@SuperQ
Copy link
Member

SuperQ commented Feb 25, 2023

This needs a rebase to pick up go.mod conflicts.

@zhsj
Copy link
Author

zhsj commented Mar 1, 2023

rebased.

@SuperQ SuperQ requested a review from roidelapluie March 1, 2023 15:06
@SuperQ
Copy link
Member

SuperQ commented Mar 1, 2023

Sorry, needs a rebase again. 😂

Copy link
Member

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Individual comments are just nitpicking.)

If I remember correctly, the problem is that consumers of this package have to change their import paths in the same way at the same time. Even though the interface doesn't change, a different import path still means we have different types. This could even lead to really tricky situations if someone has multiple dependencies, and some are still using the old version of prometheus/common while others use the new. That's usually caught by changing the import path with new major versions. But prometheus/common is still 0.x. However, prometheus/client_golang is already at 1.x, so there is an expectation to not do breaking changes.

I think the call if we can make this change or not should be made by @kakkoyun and @bwplotka . Given how frequently prometheus/client_golang is used, that's not an easy decision.

"github.com/prometheus/common/model"

dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: In the Prometheus code base, we tend to keep imports from other repos than the current one in its own block above the imports from the same repo (as it was before). And we usually also have the named imports in a different block (which wasn't adhered to before, but you could fix it now that we are on it).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format is fixed.

dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import nit as before: Have the named imports in their own block after the unnamed imports (as it was before).

dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could fix the block structure here (it wasn't quite right before, see other nitpicking comments).

"github.com/prometheus/common/model"

dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same nitpicking as before. This has to be

"google.golang.org/protobuf/proto"

dto "github.com/prometheus/client_model/go"

"github.com/prometheus/common/model"

And yes, it was wrong in a different way before...

dto "github.com/prometheus/client_model/go"
"google.golang.org/protobuf/proto"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import nitpick as above.

@beorn7
Copy link
Member

beorn7 commented Mar 1, 2023

Hmmm, thinking more about it, the only usage of the proto package outside of tests is in text_parse.go line 288, and this particular function just returns a pointer to a (standard) string. Which means that the change in this PR should actually be transparent, and importers of this package should be able to continue using the proto package of their choice. (I was already wondering why this even works without changing prometheus/client_model…)

So I would cautiously say now that this PR is OK (modulo import nitpicking ;). Still, would be good to have @kakkoyun / @bwplotka confirm.

@zhsj
Copy link
Author

zhsj commented Mar 1, 2023

If I remember correctly, the problem is that consumers of this package have to change their import paths in the same way at the same time. Even though the interface doesn't change, a different import path still means we have different types.

The break already happened when bumping google/protobuf from 1.3 to 1.4, which forces using google.golang.org/protobuf.

As you can see the go.mod changes, it only moves google.golang.org/protobuf from indirect to direct.

This replaces usage of proto.{Float64,Int32,Int64,String,Uint32,Uint64},
which doesn't break the interface.

And replaces ptypes.TimestampProto by google.golang.org/protobuf/types/known/timestamppb.
This works since github.com/golang/protobuf has been bumped to v1.5.2,
which makes ptypes.TimestampProto an alias to timestamppb.Timestamp.
This is only used test (openmetrics_create_test.go), which won't break
interfaces.

Updates: prometheus#317

Signed-off-by: Shengjing Zhu <zhsj@debian.org>
@zhsj
Copy link
Author

zhsj commented Mar 1, 2023

The break already happened when bumping google/protobuf from 1.3 to 1.4, which forces using google.golang.org/protobuf.

The above is not related to this repo. But for other repos that have pb.go files.

This repo as @beorn7 said, "returns a pointer to a (standard) string".

And the string format should be its interface. That's why proto.MarshalTextString is still using the old golang/protobuf package, see #317 (comment).

Copy link
Member

@beorn7 beorn7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much. I'm fairly convinced this will work as intended. Would still be good to get @kakkoyun / @bwplotka approve this.

@SuperQ
Copy link
Member

SuperQ commented May 4, 2023

Fixed in #479

@SuperQ SuperQ closed this May 4, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants