Skip to content

assets: add file system layer for zipped embed assets - #343

Merged
roidelapluie merged 2 commits into
prometheus:mainfrom
jan--f:embed-static-compressed-assets
Nov 30, 2021
Merged

roidelapluie merged 2 commits into
prometheus:mainfrom
jan--f:embed-static-compressed-assets

Conversation

@jan--f

@jan--f jan--f commented Nov 11, 2021

Copy link
Copy Markdown
Contributor

This commits adds an optional decompression layer to a embed.FS
instance. If a file can't be found during Open, the filename plus a .gz
ending is tried. If this succeeds, the content is decompressed and
served.

Signed-off-by: Jan Fajerski [email protected]

This was originally proposed in prometheus/prometheus#9380.

@jan--f

jan--f commented Nov 11, 2021

Copy link
Copy Markdown
Contributor Author

I'm not sure how I can keep building and testing this for go < 1.16. Any input welcome.

@jan--f
jan--f force-pushed the embed-static-compressed-assets branch 2 times, most recently from 10bde71 to 308b1e8 Compare November 11, 2021 14:04
@roidelapluie

Copy link
Copy Markdown
Member

I'd create a separate module like we do for sigv4

@jan--f
jan--f force-pushed the embed-static-compressed-assets branch from 308b1e8 to 0bbedfb Compare November 12, 2021 09:01
@jan--f

jan--f commented Nov 12, 2021

Copy link
Copy Markdown
Contributor Author

Thanks, I didn't think of that.
I also reworked the circleci workflow a little bit. I was trying to avoid a conditional like if go_version >= 1.16 in the test job, though it ended up a bit more verbose. Alternatively we could add a new parameter like test_assets or so.

@jan--f

jan--f commented Nov 25, 2021

Copy link
Copy Markdown
Contributor Author

@roidelapluie mind taking another look at this?

@roidelapluie

Copy link
Copy Markdown
Member

@juliusv did you review this before merging into prometheus/prometheus?

@juliusv

juliusv commented Nov 25, 2021

Copy link
Copy Markdown
Member

@roidelapluie Ah I did not.

@roidelapluie roidelapluie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks.

It would be nice if exported function would have the interfaces they implement in comments.

Comment thread README.md Outdated
components and libraries. They are considered internal to Prometheus, without
any stability guarantees for external usage.

* **assets**: Embeding of static assets with gzip support

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
* **assets**: Embeding of static assets with gzip support
* **assets**: Embedding of static assets with gzip support

Comment thread assets/embed_gzip.go Outdated
}

type File struct {
// the underlying file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// the underlying file
// The underlying file.

Comment thread assets/embed_gzip.go Outdated
type File struct {
// the underlying file
file fs.File
// the decrompressed content, needed to return an accurate size

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// the decrompressed content, needed to return an accurate size
// The decrompressed content, needed to return an accurate size.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is not needed for this, it is used to read it.

Comment thread assets/embed_gzip.go Outdated
return FileInfo{stat, int64(len(f.content))}, nil
}

func (f *File) Read(buf []byte) (n int, err error) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can we avoid the named return here? thanks

Comment thread assets/embed_gzip.go Outdated
)

const (
zipSuffix = ".gz"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
zipSuffix = ".gz"
gzipSuffix = ".gz"

Comment thread assets/embed_gzip.go Outdated
if err != nil {
return f, err
}
// and wrap everything in our custom File

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// and wrap everything in our custom File
// Wrap everything in our custom File.

Comment thread assets/embed_gzip.go Outdated
if err != nil {
return f, err
}
// read the (decompressed) content into a buffer

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// read the (decompressed) content into a buffer
// Read the decompressed content into a buffer.

Comment thread assets/embed_gzip.go Outdated
}

func (compressed FileSystem) Open(path string) (fs.File, error) {
// if we have the file in our embed FS, just return that (could be a

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// if we have the file in our embed FS, just return that (could be a
// If we have the file in our embed FS, just return that (could be a

Comment thread assets/embed_gzip.go Outdated

func (compressed FileSystem) Open(path string) (fs.File, error) {
// if we have the file in our embed FS, just return that (could be a
// dir)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// dir)
// dir.)

let's have that comment in one line.

Comment thread assets/embed_gzip.go Outdated
if f, err := compressed.embed.Open(path); err == nil {
return f, nil
}
// try opening path with .gz suffix

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This comment is not really needed

This commits adds an optional decompression layer to a embed.FS
instance. If a file can't be found during Open, the filename plus a .gz
ending is tried. If this succeeds, the content is decompressed and
served.

Signed-off-by: Jan Fajerski <[email protected]>
Since assets tests can only run on go1.16 and later the matrix style job
list avoinds adding more conditionals to the job spec.

Signed-off-by: Jan Fajerski <[email protected]>
@jan--f
jan--f force-pushed the embed-static-compressed-assets branch from 0bbedfb to 50a67a2 Compare November 26, 2021 09:57
@jan--f

jan--f commented Nov 26, 2021

Copy link
Copy Markdown
Contributor Author

Thank you @roidelapluie! PTAL.

@roidelapluie
roidelapluie merged commit f57586d into prometheus:main Nov 30, 2021
@roidelapluie

Copy link
Copy Markdown
Member

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants