Skip to content

Commit

Permalink
feat: expose build information (#1030)
Browse files Browse the repository at this point in the history
We expose build information in the logs, when an engine.Session is
being created as well as inside the annotations.

Part of ooni/probe#2273

See ooni/spec#272
  • Loading branch information
bassosimone authored Jan 16, 2023
1 parent 26b81d4 commit b080d6c
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 6 deletions.
8 changes: 7 additions & 1 deletion internal/engine/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/ooni/probe-cli/v3/internal/version"
)

Expand Down Expand Up @@ -217,10 +218,15 @@ func (e *experiment) newMeasurement(input string) *model.Measurement {
TestStartTime: e.testStartTime,
TestVersion: e.testVersion,
}
m.AddAnnotation("architecture", runtime.GOARCH)
m.AddAnnotation("engine_name", "ooniprobe-engine")
m.AddAnnotation("engine_version", version.Version)
m.AddAnnotation("go_version", runtimex.BuildInfo.GoVersion)
m.AddAnnotation("platform", e.session.Platform())
m.AddAnnotation("architecture", runtime.GOARCH)
m.AddAnnotation("vcs_modified", runtimex.BuildInfo.VcsModified)
m.AddAnnotation("vcs_revision", runtimex.BuildInfo.VcsRevision)
m.AddAnnotation("vcs_time", runtimex.BuildInfo.VcsTime)
m.AddAnnotation("vcs_tool", runtimex.BuildInfo.VcsTool)
return m
}

Expand Down
8 changes: 8 additions & 0 deletions internal/engine/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"github.com/ooni/probe-cli/v3/internal/platform"
"github.com/ooni/probe-cli/v3/internal/runtimex"
"github.com/ooni/probe-cli/v3/internal/tunnel"
"github.com/ooni/probe-cli/v3/internal/version"
)
Expand Down Expand Up @@ -159,6 +160,13 @@ func NewSession(ctx context.Context, config SessionConfig) (*Session, error) {
if err != nil {
return nil, err
}
config.Logger.Infof(
"ooniprobe-engine/v%s %s dirty=%s %s",
version.Version,
runtimex.BuildInfo.VcsRevision,
runtimex.BuildInfo.VcsModified,
runtimex.BuildInfo.GoVersion,
)
sess := &Session{
availableProbeServices: config.AvailableProbeServices,
byteCounter: bytecounter.New(),
Expand Down
52 changes: 52 additions & 0 deletions internal/runtimex/runtimex.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,60 @@ package runtimex
import (
"errors"
"fmt"
"runtime/debug"
)

// BuildInfoRecord contains build-time information.
type BuildInfoRecord struct {
// GoVersion is the version of go with which this code
// was compiled or an empty string.
GoVersion string

// VcsModified indicates whether the tree was dirty.
VcsModified string

// VcsRevision is the VCS revision we compiled.
VcsRevision string

// VcsTime is the time of the revision we're building.
VcsTime string

// VcsTool is the VCS tool being used.
VcsTool string
}

// setkv is a convenience function to set a [BuildInfoRecord] entry.
func (bir *BuildInfoRecord) setkv(key, value string) {
switch key {
case "vcs.revision":
bir.VcsRevision = value
case "vcs.time":
bir.VcsTime = value
case "vcs.modified":
bir.VcsModified = value
case "vcs":
bir.VcsTool = value
}
}

// setall sets all the possible settings.
func (bir *BuildInfoRecord) setall(settings []debug.BuildSetting) {
for _, entry := range settings {
bir.setkv(entry.Key, entry.Value)
}
}

// BuildInfo is the singleton containing build-time information.
var BuildInfo = &BuildInfoRecord{}

func init() {
info, good := debug.ReadBuildInfo()
if good {
BuildInfo.GoVersion = info.GoVersion
BuildInfo.setall(info.Settings)
}
}

// PanicOnError calls panic() if err is not nil. The type passed
// to panic is an error type wrapping the original error.
func PanicOnError(err error, message string) {
Expand Down
55 changes: 55 additions & 0 deletions internal/runtimex/runtimex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package runtimex

import (
"errors"
"runtime/debug"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestPanicOnError(t *testing.T) {
Expand Down Expand Up @@ -91,3 +94,55 @@ func TestPanicIfNil(t *testing.T) {
}
})
}

func TestBuildInfoRecord_setall(t *testing.T) {
tests := []struct {
name string
key string
value string
want *BuildInfoRecord
}{{
name: "for VcsModified",
key: "vcs.modified",
value: "ABC",
want: &BuildInfoRecord{
VcsModified: "ABC",
},
}, {
name: "for VcsRevision",
key: "vcs.revision",
value: "ABC",
want: &BuildInfoRecord{
VcsRevision: "ABC",
},
}, {
name: "for VcsTime",
key: "vcs.time",
value: "ABC",
want: &BuildInfoRecord{
VcsTime: "ABC",
},
}, {
name: "for VcsTool",
key: "vcs",
value: "git",
want: &BuildInfoRecord{
VcsTool: "git",
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bir := &BuildInfoRecord{
GoVersion: "",
VcsModified: "",
VcsRevision: "",
VcsTime: "",
VcsTool: "",
}
bir.setall([]debug.BuildSetting{{Key: tt.key, Value: tt.value}})
if diff := cmp.Diff(tt.want, bir); diff != "" {
t.Fatal(diff)
}
})
}
}
8 changes: 3 additions & 5 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Package version contains version information
// Package version contains version information.
package version

const (
// Version is the software version
Version = "3.17.0-alpha.1"
)
// Version is the ooniprobe version.
const Version = "3.17.0-alpha.1"

0 comments on commit b080d6c

Please sign in to comment.