Skip to content

Commit 7bb0803

Browse files
authored
Build: Move build info to new package (#1399)
1 parent 8167436 commit 7bb0803

File tree

7 files changed

+85
-67
lines changed

7 files changed

+85
-67
lines changed

backend/setup.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
1717
"github.com/grafana/grafana-plugin-sdk-go/backend/tracing"
18-
"github.com/grafana/grafana-plugin-sdk-go/build"
18+
"github.com/grafana/grafana-plugin-sdk-go/build/info"
1919
"github.com/grafana/grafana-plugin-sdk-go/internal/tracerprovider"
2020
)
2121

@@ -165,7 +165,7 @@ func getTracerCustomAttributes(pluginID string) []attribute.KeyValue {
165165
// Try to get plugin version from build info
166166
// If not available, fallback to environment variable
167167
var pluginVersion string
168-
buildInfo, err := build.GetBuildInfo()
168+
buildInfo, err := info.GetBuildInfo()
169169
if err != nil {
170170
Logger.Debug("Failed to get build info", "error", err)
171171
} else {
@@ -186,7 +186,7 @@ func getTracerCustomAttributes(pluginID string) []attribute.KeyValue {
186186
// SetupTracer sets up the global OTEL trace provider and tracer.
187187
func SetupTracer(pluginID string, tracingOpts tracing.Opts) error {
188188
// Set up tracing
189-
tracingCfg := getTracingConfig(build.GetBuildInfo)
189+
tracingCfg := getTracingConfig(info.GetBuildInfo)
190190
if tracingCfg.isEnabled() {
191191
// Append custom attributes to the default ones
192192
tracingOpts.CustomAttributes = append(getTracerCustomAttributes(pluginID), tracingOpts.CustomAttributes...)
@@ -244,7 +244,7 @@ func (c tracingConfig) isEnabled() bool {
244244
}
245245

246246
// getTracingConfig returns a new tracingConfig based on the current environment variables.
247-
func getTracingConfig(buildInfoGetter build.InfoGetter) tracingConfig {
247+
func getTracingConfig(buildInfoGetter info.Getter) tracingConfig {
248248
var otelAddr, otelPropagation, samplerRemoteURL, samplerParamString string
249249
var samplerType tracerprovider.SamplerType
250250
var samplerParam float64
@@ -289,7 +289,7 @@ func getTracingConfig(buildInfoGetter build.InfoGetter) tracingConfig {
289289
// remoteSamplerServiceName returns the service name for the remote tracing sampler.
290290
// It attempts to get it from the provided buildinfo getter. If unsuccessful or empty,
291291
// defaultRemoteSamplerServiceName is returned instead.
292-
func remoteSamplerServiceName(buildInfoGetter build.InfoGetter) string {
292+
func remoteSamplerServiceName(buildInfoGetter info.Getter) string {
293293
// Use plugin id as service name, if possible. Otherwise, use a generic default value.
294294
bi, err := buildInfoGetter.GetInfo()
295295
if err != nil {

backend/setup_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55

66
"github.com/grafana/grafana-plugin-sdk-go/backend/tracing"
7-
"github.com/grafana/grafana-plugin-sdk-go/build"
7+
"github.com/grafana/grafana-plugin-sdk-go/build/info"
88
"github.com/grafana/grafana-plugin-sdk-go/internal/tracerprovider"
99
"github.com/stretchr/testify/require"
1010
"go.opentelemetry.io/otel"
@@ -15,7 +15,7 @@ func TestGetTracingConfig(t *testing.T) {
1515
name string
1616

1717
env map[string]string
18-
buildInfoGetter build.InfoGetter
18+
buildInfoGetter info.Getter
1919

2020
expEnabled bool
2121
expCfg tracingConfig
@@ -94,8 +94,8 @@ func TestGetTracingConfig(t *testing.T) {
9494
PluginTracingSamplerParamEnv: "0.5",
9595
PluginTracingSamplerRemoteURL: "127.0.0.1:10001",
9696
},
97-
buildInfoGetter: build.InfoGetterFunc(func() (build.Info, error) {
98-
return build.Info{PluginID: "my-example-datasource"}, nil
97+
buildInfoGetter: info.GetterFunc(func() (info.Info, error) {
98+
return info.Info{PluginID: "my-example-datasource"}, nil
9999
}),
100100
expEnabled: true,
101101
expCfg: tracingConfig{
@@ -117,7 +117,7 @@ func TestGetTracingConfig(t *testing.T) {
117117
t.Setenv(e, v)
118118
}
119119
if tc.buildInfoGetter == nil {
120-
tc.buildInfoGetter = build.GetBuildInfo
120+
tc.buildInfoGetter = info.GetBuildInfo
121121
}
122122
cfg := getTracingConfig(tc.buildInfoGetter)
123123
require.Equal(t, tc.expEnabled, cfg.isEnabled())

build/common.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
bra "github.com/unknwon/bra/cmd"
2020
"github.com/urfave/cli"
2121

22+
"github.com/grafana/grafana-plugin-sdk-go/build/info"
2223
"github.com/grafana/grafana-plugin-sdk-go/build/utils"
2324
"github.com/grafana/grafana-plugin-sdk-go/experimental/e2e"
2425
ca "github.com/grafana/grafana-plugin-sdk-go/experimental/e2e/certificate_authority"
@@ -157,7 +158,7 @@ func getBuildBackendCmdInfo(cfg Config) (Config, []string, error) {
157158
"build", "-o", filepath.Join(outputPath, exePath),
158159
}
159160

160-
info := Info{
161+
info := info.Info{
161162
Time: now().UnixNano() / int64(time.Millisecond),
162163
}
163164
pluginID, err := internal.GetStringValueFromJSON(filepath.Join(pluginJSONPath, "plugin.json"), "id")
@@ -172,7 +173,7 @@ func getBuildBackendCmdInfo(cfg Config) (Config, []string, error) {
172173
args = append(args, "-tags", "arrow_json_stdlib")
173174

174175
flags := make(map[string]string, 10)
175-
info.appendFlags(flags)
176+
info.AppendFlags(flags)
176177

177178
if cfg.CustomVars != nil {
178179
for k, v := range cfg.CustomVars {

build/common_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func Test_getBuildBackendCmdInfo(t *testing.T) {
153153
Env: map[string]string{"CGO_ENABLED": "0", "GOARCH": "arm64", "GOOS": "darwin"},
154154
PluginJSONPath: filepath.Join(tmpDir, "foobar-datasource"),
155155
},
156-
expectedArgs: []string{"build", "-o", filepath.Join(defaultOutputBinaryPath, "gpx_foo_darwin_arm64"), "-tags", "arrow_json_stdlib", "-ldflags", "-w -s -extldflags \"-static\" -X 'github.com/grafana/grafana-plugin-sdk-go/build.buildInfoJSON={.*}'", "./pkg"},
156+
expectedArgs: []string{"build", "-o", filepath.Join(defaultOutputBinaryPath, "gpx_foo_darwin_arm64"), "-tags", "arrow_json_stdlib", "-ldflags", "-w -s -extldflags \"-static\" -X 'github.com/grafana/grafana-plugin-sdk-go/build/info.buildInfoJSON={.*}'", "./pkg"},
157157
wantErr: assert.NoError,
158158
},
159159
{
@@ -174,7 +174,7 @@ func Test_getBuildBackendCmdInfo(t *testing.T) {
174174
Env: map[string]string{"CGO_ENABLED": "0", "GOARCH": "arm64", "GOOS": "darwin"},
175175
PluginJSONPath: filepath.Join(tmpDir, "foobar-app"),
176176
},
177-
expectedArgs: []string{"build", "-o", filepath.Join(defaultOutputBinaryPath, defaultNestedDataSourcePath, "gpx_foo_darwin_arm64"), "-tags", "arrow_json_stdlib", "-ldflags", "-w -s -extldflags \"-static\" -X 'github.com/grafana/grafana-plugin-sdk-go/build.buildInfoJSON={.*}'", "./pkg"},
177+
expectedArgs: []string{"build", "-o", filepath.Join(defaultOutputBinaryPath, defaultNestedDataSourcePath, "gpx_foo_darwin_arm64"), "-tags", "arrow_json_stdlib", "-ldflags", "-w -s -extldflags \"-static\" -X 'github.com/grafana/grafana-plugin-sdk-go/build/info.buildInfoJSON={.*}'", "./pkg"},
178178
wantErr: assert.NoError,
179179
},
180180
{
@@ -195,7 +195,7 @@ func Test_getBuildBackendCmdInfo(t *testing.T) {
195195
Env: map[string]string{"CGO_ENABLED": "0", "GOARCH": "amd64", "GOOS": "windows"},
196196
PluginJSONPath: filepath.Join(tmpDir, "foobarbaz-app"),
197197
},
198-
expectedArgs: []string{"build", "-o", filepath.Join(defaultOutputBinaryPath, "gpx_foobarbaz_windows_amd64.exe"), "-tags", "arrow_json_stdlib", "-ldflags", "-w -s -extldflags \"-static\" -X 'github.com/grafana/grafana-plugin-sdk-go/build.buildInfoJSON={.*}'", "./pkg"},
198+
expectedArgs: []string{"build", "-o", filepath.Join(defaultOutputBinaryPath, "gpx_foobarbaz_windows_amd64.exe"), "-tags", "arrow_json_stdlib", "-ldflags", "-w -s -extldflags \"-static\" -X 'github.com/grafana/grafana-plugin-sdk-go/build/info.buildInfoJSON={.*}'", "./pkg"},
199199
wantErr: assert.NoError,
200200
},
201201
}

build/info.go

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,21 @@
11
package build
22

33
import (
4-
"encoding/json"
5-
"fmt"
64
"time"
7-
)
8-
9-
// set from -X
10-
var buildInfoJSON string
11-
12-
// exposed for testing.
13-
var now = time.Now
145

15-
// Info See also PluginBuildInfo in https://github.com/grafana/grafana/blob/master/pkg/plugins/models.go
16-
type Info struct {
17-
Time int64 `json:"time,omitempty"`
18-
PluginID string `json:"pluginID,omitempty"`
19-
Version string `json:"version,omitempty"`
20-
}
21-
22-
// this will append build flags -- the keys are picked to match existing
23-
// grafana build flags from bra
24-
func (v Info) appendFlags(flags map[string]string) {
25-
if v.PluginID != "" {
26-
flags["main.pluginID"] = v.PluginID
27-
}
28-
if v.Version != "" {
29-
flags["main.version"] = v.Version
30-
}
6+
"github.com/grafana/grafana-plugin-sdk-go/build/info"
7+
)
318

32-
out, err := json.Marshal(v)
33-
if err == nil {
34-
flags["github.com/grafana/grafana-plugin-sdk-go/build.buildInfoJSON"] = string(out)
35-
}
36-
}
9+
var now = time.Now // allow override for testing
3710

38-
// InfoGetter is an interface with a method for returning the build info.
39-
type InfoGetter interface {
40-
// GetInfo returns the build info.
41-
GetInfo() (Info, error)
42-
}
11+
// Deprecated: Use github.com/grafana/grafana-plugin-sdk-go/build/info.Info instead.
12+
type Info = info.Info
4313

44-
// InfoGetterFunc can be used to adapt ordinary functions into types satisfying the InfoGetter interface .
45-
type InfoGetterFunc func() (Info, error)
14+
// Deprecated: Use github.com/grafana/grafana-plugin-sdk-go/build/info.Getter instead.
15+
type InfoGetter = info.Getter
4616

47-
func (f InfoGetterFunc) GetInfo() (Info, error) {
48-
return f()
49-
}
17+
// Deprecated: Use github.com/grafana/grafana-plugin-sdk-go/build/info.GetterFunc instead.
18+
type InfoGetterFunc = info.GetterFunc
5019

51-
// GetBuildInfo is the default InfoGetter that returns the build information that was compiled into the binary using:
52-
// -X `github.com/grafana/grafana-plugin-sdk-go/build.buildInfoJSON={...}`
53-
var GetBuildInfo = InfoGetterFunc(func() (Info, error) {
54-
v := Info{}
55-
if buildInfoJSON == "" {
56-
return v, fmt.Errorf("build info was now set when this was compiled")
57-
}
58-
err := json.Unmarshal([]byte(buildInfoJSON), &v)
59-
return v, err
60-
})
20+
// Deprecated: Use github.com/grafana/grafana-plugin-sdk-go/build/info.GetBuildInfo instead.
21+
var GetBuildInfo = info.GetBuildInfo

build/info/info.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package info
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
// set from -X
9+
var buildInfoJSON string
10+
11+
// Info See also PluginBuildInfo in https://github.com/grafana/grafana/blob/master/pkg/plugins/models.go
12+
type Info struct {
13+
Time int64 `json:"time,omitempty"`
14+
PluginID string `json:"pluginID,omitempty"`
15+
Version string `json:"version,omitempty"`
16+
}
17+
18+
// this will append build flags -- the keys are picked to match existing
19+
// grafana build flags from bra
20+
func (v Info) AppendFlags(flags map[string]string) {
21+
if v.PluginID != "" {
22+
flags["main.pluginID"] = v.PluginID
23+
}
24+
if v.Version != "" {
25+
flags["main.version"] = v.Version
26+
}
27+
28+
out, err := json.Marshal(v)
29+
if err == nil {
30+
flags["github.com/grafana/grafana-plugin-sdk-go/build/info.buildInfoJSON"] = string(out)
31+
}
32+
}
33+
34+
// Getter is an interface with a method for returning the build info.
35+
type Getter interface {
36+
// GetInfo returns the build info.
37+
GetInfo() (Info, error)
38+
}
39+
40+
// GetterFunc can be used to adapt ordinary functions into types satisfying the InfoGetter interface .
41+
type GetterFunc func() (Info, error)
42+
43+
func (f GetterFunc) GetInfo() (Info, error) {
44+
return f()
45+
}
46+
47+
// GetBuildInfo is the default InfoGetter that returns the build information that was compiled into the binary using:
48+
// -X `github.com/grafana/grafana-plugin-sdk-go/build/info.buildInfoJSON={...}`
49+
var GetBuildInfo = GetterFunc(func() (Info, error) {
50+
v := Info{}
51+
if buildInfoJSON == "" {
52+
return v, fmt.Errorf("build info was now set when this was compiled")
53+
}
54+
err := json.Unmarshal([]byte(buildInfoJSON), &v)
55+
return v, err
56+
})

internal/buildinfo/buildinfo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"flag"
77
"fmt"
88

9-
"github.com/grafana/grafana-plugin-sdk-go/build"
9+
"github.com/grafana/grafana-plugin-sdk-go/build/info"
1010
)
1111

1212
var (
@@ -27,7 +27,7 @@ func RunInfoMode() error {
2727
if !InfoModeEnabled() {
2828
return errors.New("build info mode not enabled")
2929
}
30-
bi, err := build.GetBuildInfo()
30+
bi, err := info.GetBuildInfo()
3131
if err != nil {
3232
return fmt.Errorf("get build info: %w", err)
3333
}

0 commit comments

Comments
 (0)