tikv / rust-prometheus

Prometheus instrumentation library for Rust applications
Apache License 2.0
1.07k stars 182 forks source link

Add convenience TextEncoder functions to encode directly to string #402

Closed matklad closed 3 years ago

matklad commented 3 years ago

I saw that we do

        let mut buffer = vec![];
        let encoder = TextEncoder::new();
        encoder.encode(&prometheus::gather(), &mut buffer).unwrap();

        String::from_utf8(buffer).unwrap()

a bunch of times in near and figured that the library can probably guaranty utf8 validity and remove the need for one unwrap here.

This turned out to be slightly more involved then I've thought, as there's no standard way to explain that you'll only write utf8 to a Write, but it wasn't to hard either.

As a bonus, TextEncoder impl now is statically guaranteed to produce only valid utf8

lucab commented 3 years ago

Thanks for PR. Looks great overall, I've left a couple of minor comments in review. It looks like our CI is having a bad day; I'll try to have a look at fixing it as soon as I have a free timeslot, then this PR may need to be rebased to see a green run.

matklad commented 3 years ago

Sure, no problem rebasing!

FYI, I see the "1 workflow awaiting approval" banner, if you want to disable for new contributors, you can check this box:

image

matklad commented 3 years ago

Figured out a much better solution -- there's no need for unsafety here.

lucab commented 3 years ago

Ok, https://github.com/tikv/rust-prometheus/pull/403 is merged so rebasing this PR now should make the CI happy.

lucab commented 3 years ago

@zhouqiang-cl @BusyJay @breeswish whoever of you has access to do that, can you please tweak the workflow permissions as shown in https://github.com/tikv/rust-prometheus/pull/402#issuecomment-877115118 so that we don't have to re-approve it on every push?

BusyJay commented 3 years ago

As github actions resource are bound to the organization account, so it might not be a good idea to just change it for a repository. Maybe you can send an issue to tikv/community or a thread at https://internals.tidb.io/, let's discuss it more!

matklad commented 3 years ago

@lucab ptal :)