Skip to content
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

include build time and revision in version information #5973

Merged
merged 1 commit into from
Jan 4, 2018

Conversation

graphaelli
Copy link
Member

I couldn't find a clever way to hook up tests for this, here's one way to verify the changes:

$ cd $GOPATH/src/github.com/elastic/beats/auditbeat
$ make -B
./auditbego build -i -ldflags "-X github.com/elastic/beats/libbeat/version.buildTime=2018-01-03T01:07:04 -X github.com/elastic/beats/libbeat/version.commit=65a82579ddc7fa34a82cf554e6ef4adcbf24bdfa"
at version
$ ./auditbeat version
auditbeat version 7.0.0-alpha1 (amd64), libbeat 7.0.0-alpha1 [65a82579ddc7fa34a82cf554e6ef4adcbf24bdfa built 2018-01-03T01:07:04]

I got caught up for awhile getting this to work for apps that vendor libbeat, like apm-server. This worked there, note the github.com/elastic/apm-server/vendor/ prefix:

$ cd $GOPATH/src/github.com/elastic/apm-serve
$ go build -i -ldflags "-X github.com/elastic/apm-server/vendor/github.com/elastic/beats/libbeat/version.buildTime=2018-01-03T01:00:47 -X github.com/elastic/apm-server/vendor/github.com/elastic/beats/libbeat/version.commit=c066b5885efb57decafbee9fbbb532b4e2041529"
$ ./apm-server version
apm-server version 7.0.0-alpha1 (amd64), libbeat 7.0.0-alpha1 [c066b5885efb57decafbee9fbbb532b4e2041529 built 2018-01-03T01:00:47]

return buildTime
}

func Commit() string {

Choose a reason for hiding this comment

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

exported function Commit should have comment or be unexported

commit = "unknown"
)

func BuildTime() string {

Choose a reason for hiding this comment

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

exported function BuildTime should have comment or be unexported

return buildTime
}

// BuildTime exposes the compile-time commit information

Choose a reason for hiding this comment

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

comment on exported function Commit should be of the form "Commit ..."

)

// BuildTime exposes the compile-time build time information
func BuildTime() string {
Copy link
Member

Choose a reason for hiding this comment

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

How about returning a time.Time instead of string? And if the value is not set appropriately then return a zero value (time.Time{}). This will save callers that need the time in a certain format from having to learn the format and parse it themselves.

Copy link
Member Author

Choose a reason for hiding this comment

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

I like that but concerned about how to handle parse failure, in case of user error. At the risk of overkill, what about a versionTime type with func String() string and func Parse() (time.Time, error) methods?

Copy link
Member

@andrewkroh andrewkroh Jan 3, 2018

Choose a reason for hiding this comment

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

I was thinking to only have version.BuildTime() time.Time. If the value was not set or was set incorrectly then return time.Time{}. For me this would be adequate (I could check for validity if my use case required it with time.IsZero()). I'd clearly state in the godocs that the zero value could be returned if the version.buildTime variable was not set or set incorrectly.

But if you want to be more explicit in stating that the build time value is bogus then I'd use version.BuildTime() (time.Time, error).

Copy link
Member Author

Choose a reason for hiding this comment

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

updated as requested, now:

# valid time
$ go build -i -ldflags "-X github.com/elastic/beats/libbeat/version.buildTime=2018-01-03T17:30:56Z -X github.com/elastic/beats/libbeat/version.commit=1234"
$ ./libbeat version
mockbeat version 9.9.9 (amd64), libbeat 7.0.0-alpha1 [1234 built 2018-01-03 17:30:56 +0000 UTC]

# invalid time
$ go build -i -ldflags "-X github.com/elastic/beats/libbeat/version.buildTime=XXX -X github.com/elastic/beats/libbeat/version.commit=1234"
$ ./libbeat version
mockbeat version 9.9.9 (amd64), libbeat 7.0.0-alpha1 [1234 built unknown]

@@ -36,7 +36,8 @@ COVERAGE_TOOL?=${BEAT_GOPATH}/bin/gotestcover
COVERAGE_TOOL_REPO?=github.com/elastic/beats/vendor/github.com/pierrre/gotestcover
TESTIFY_TOOL_REPO?=github.com/elastic/beats/vendor/github.com/stretchr/testify
LIBCOMPOSE_TOOL_REPO?=github.com/docker/libcompose
GOBUILD_FLAGS?=-i
NOW=$(shell date -u '+%Y-%m-%dT%H:%M:%S')
Copy link
Member

Choose a reason for hiding this comment

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

This looks to be very close to RFC3339. Can you add the Zulu timezone identifier (e.g. date -u +"%Y-%m-%dT%H:%M:%SZ"). This will make the value parse nicely like time.Parse(time.RFC3339, "2018-01-03T16:03:28Z").

func BuildTime() time.Time {
if t, err := time.Parse(time.RFC3339, buildTime); err != nil {
return time.Time{}
} else {

Choose a reason for hiding this comment

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

if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)

func BuildTime() time.Time {
if t, err := time.Parse(time.RFC3339, buildTime); err != nil {
return time.Time{}
} else {

Choose a reason for hiding this comment

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

if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)

Copy link
Member

@andrewkroh andrewkroh left a comment

Choose a reason for hiding this comment

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

Overall LGTM in its current state. Left a few nit comments. Thanks for making this change upstream.

commit = "unknown"
)

// BuildTime exposes the compile-time build time information
Copy link
Member

Choose a reason for hiding this comment

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

You should document the conditions under which a zero value can be returned.

return t
}

// Commit exposes the compile-time commit information
Copy link
Member

Choose a reason for hiding this comment

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

I think this should mention "hash" or "commit ID".

@@ -22,8 +22,15 @@ func genVersionCmd(name, beatVersion string) *cobra.Command {
return fmt.Errorf("error initializing beat: %s", err)
}

fmt.Printf("%s version %s (%s), libbeat %s\n",
beat.Info.Beat, beat.Info.Version, runtime.GOARCH, version.GetDefaultVersion())
var buildTime string
Copy link
Member

Choose a reason for hiding this comment

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

Consider initializing buildTime := "unknown". I think it could make this a bit more concise.

@andrewkroh
Copy link
Member

Regarding testing, we should probably add a check to beats-tester to verify that the .build_hash.txt file matches the beat version output.

Copy link
Member

@andrewkroh andrewkroh left a comment

Choose a reason for hiding this comment

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

LGTM, but I think it needs a rebase to pull in the fix for the NOTICE.txt year.

@andrewkroh andrewkroh merged commit 54d6b4c into elastic:master Jan 4, 2018
graphaelli added a commit to graphaelli/beats that referenced this pull request Jan 16, 2018
This reverts commit a3970ee.

Revert "Include build time and revision in version information (elastic#5973)"

This reverts commit 54d6b4c.
ruflin pushed a commit that referenced this pull request Jan 16, 2018
This reverts commit a3970ee.

Revert "Include build time and revision in version information (#5973)"

This reverts commit 54d6b4c.


Temporarily remove version info that is not provided by the current release process.

before:
```
$ ./libbeat version 
mockbeat version 9.9.9 (amd64), libbeat 6.2.0 [unknown built unknown]
```

after:
```
$ ./libbeat version
mockbeat version 9.9.9 (amd64), libbeat 6.2.0
```

for #6051
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants