Skip to content

Commit a2ed9b8

Browse files
authored
Introduce version command and handler (#517)
* Introduce version command and handler Signed-off-by: Pavol Loffay <ploffay@redhat.com> * Use flags not persistent flags Signed-off-by: Pavol Loffay <ploffay@redhat.com> * xdock java depend on agent Signed-off-by: Pavol Loffay <ploffay@redhat.com>
1 parent 2b73fe9 commit a2ed9b8

File tree

11 files changed

+149
-7
lines changed

11 files changed

+149
-7
lines changed

Makefile

+10-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ FMT_LOG=fmt.log
2020
LINT_LOG=lint.log
2121
MKDOCS_VIRTUAL_ENV=.mkdocs-virtual-env
2222

23+
GIT_SHA=$(shell git rev-parse HEAD)
24+
GIT_CLOSEST_TAG=$(shell git describe --abbrev=0 --tags)
25+
DATE=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
26+
BUILD_INFO_IMPORT_PATH=github.com/uber/jaeger/pkg/version
27+
BUILD_INFO=-ldflags "-X $(BUILD_INFO_IMPORT_PATH).commitSHA=$(GIT_SHA) -X $(BUILD_INFO_IMPORT_PATH).latestVersion=$(GIT_CLOSEST_TAG) -X $(BUILD_INFO_IMPORT_PATH).date=$(DATE)"
28+
2329
SED=sed
2430
THRIFT_VER=0.9.3
2531
THRIFT_IMG=thrift:$(THRIFT_VER)
@@ -102,19 +108,19 @@ build_ui:
102108

103109
.PHONY: build-all-in-one-linux
104110
build-all-in-one-linux: build_ui
105-
CGO_ENABLED=0 GOOS=linux installsuffix=cgo go build -o ./cmd/standalone/standalone-linux ./cmd/standalone/main.go
111+
CGO_ENABLED=0 GOOS=linux installsuffix=cgo go build -o ./cmd/standalone/standalone-linux $(BUILD_INFO) ./cmd/standalone/main.go
106112

107113
.PHONY: build-agent-linux
108114
build-agent-linux:
109-
CGO_ENABLED=0 GOOS=linux installsuffix=cgo go build -o ./cmd/agent/agent-linux ./cmd/agent/main.go
115+
CGO_ENABLED=0 GOOS=linux installsuffix=cgo go build -o ./cmd/agent/agent-linux $(BUILD_INFO) ./cmd/agent/main.go
110116

111117
.PHONY: build-query-linux
112118
build-query-linux:
113-
CGO_ENABLED=0 GOOS=linux installsuffix=cgo go build -o ./cmd/query/query-linux ./cmd/query/main.go
119+
CGO_ENABLED=0 GOOS=linux installsuffix=cgo go build -o ./cmd/query/query-linux $(BUILD_INFO) ./cmd/query/main.go
114120

115121
.PHONY: build-collector-linux
116122
build-collector-linux:
117-
CGO_ENABLED=0 GOOS=linux installsuffix=cgo go build -o ./cmd/collector/collector-linux ./cmd/collector/main.go
123+
CGO_ENABLED=0 GOOS=linux installsuffix=cgo go build -o ./cmd/collector/collector-linux $(BUILD_INFO) ./cmd/collector/main.go
118124

119125
.PHONY: docker-no-ui
120126
docker-no-ui: build-agent-linux build-collector-linux build-query-linux build-crossdock-linux

cmd/agent/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/uber/jaeger/cmd/flags"
2727
"github.com/uber/jaeger/pkg/config"
2828
"github.com/uber/jaeger/pkg/metrics"
29+
"github.com/uber/jaeger/pkg/version"
2930
)
3031

3132
func main() {
@@ -58,6 +59,8 @@ func main() {
5859
},
5960
}
6061

62+
command.AddCommand(version.Command())
63+
6164
config.AddFlags(
6265
v,
6366
command,

cmd/collector/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/uber/jaeger/pkg/config"
4242
"github.com/uber/jaeger/pkg/healthcheck"
4343
"github.com/uber/jaeger/pkg/recoveryhandler"
44+
"github.com/uber/jaeger/pkg/version"
4445
jc "github.com/uber/jaeger/thrift-gen/jaeger"
4546
zc "github.com/uber/jaeger/thrift-gen/zipkincore"
4647
)
@@ -129,6 +130,8 @@ func main() {
129130
},
130131
}
131132

133+
command.AddCommand(version.Command())
134+
132135
config.AddFlags(
133136
v,
134137
command,

cmd/query/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/uber/jaeger/pkg/config"
3939
"github.com/uber/jaeger/pkg/healthcheck"
4040
"github.com/uber/jaeger/pkg/recoveryhandler"
41+
"github.com/uber/jaeger/pkg/version"
4142
)
4243

4344
func main() {
@@ -121,6 +122,8 @@ func main() {
121122
},
122123
}
123124

125+
command.AddCommand(version.Command())
126+
124127
config.AddFlags(
125128
v,
126129
command,

cmd/standalone/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/uber/jaeger/pkg/config"
4444
pMetrics "github.com/uber/jaeger/pkg/metrics"
4545
"github.com/uber/jaeger/pkg/recoveryhandler"
46+
"github.com/uber/jaeger/pkg/version"
4647
"github.com/uber/jaeger/storage/spanstore/memory"
4748
jc "github.com/uber/jaeger/thrift-gen/jaeger"
4849
zc "github.com/uber/jaeger/thrift-gen/zipkincore"
@@ -78,6 +79,8 @@ func main() {
7879
},
7980
}
8081

82+
command.AddCommand(version.Command())
83+
8184
config.AddFlags(
8285
v,
8386
command,

crossdock/docker-compose.yml

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ services:
3737
image: jaegertracing/xdock-java
3838
ports:
3939
- "8080-8082"
40+
depends_on:
41+
# UDP sender needs to know agent's address
42+
- jaeger-agent
4043

4144
python:
4245
image: jaegertracing/xdock-py

pkg/config/config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ func AddFlags(v *viper.Viper, command *cobra.Command, inits ...func(*flag.FlagSe
3535
for i := range inits {
3636
inits[i](flagSet)
3737
}
38-
command.PersistentFlags().AddGoFlagSet(flagSet)
38+
command.Flags().AddGoFlagSet(flagSet)
3939

4040
configureViper(v)
41-
v.BindPFlags(command.PersistentFlags())
41+
v.BindPFlags(command.Flags())
4242
return v, command
4343
}
4444

pkg/healthcheck/handler.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"strconv"
2121

2222
"go.uber.org/zap"
23+
24+
"github.com/uber/jaeger/pkg/version"
2325
)
2426

2527
// State represents the current health check state
@@ -59,10 +61,11 @@ func NewHandler(s *State) (http.Handler, error) {
5961
mu := http.NewServeMux()
6062
mu.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
6163
w.WriteHeader(s.state)
62-
6364
// this is written only for response with an entity, so, it won't be used for a 204 - No content
6465
w.Write([]byte("Server not available"))
6566
})
67+
68+
version.RegisterHandler(mu, s.logger)
6669
return mu, nil
6770
}
6871

pkg/version/build.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2017 The Jaeger Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package version
16+
17+
var (
18+
// commitFromGit is a constant representing the source version that
19+
// generated this build. It should be set during build via -ldflags.
20+
commitSHA string
21+
// versionFromGit is a constant representing the version tag that
22+
// generated this build. It should be set during build via -ldflags.
23+
latestVersion string
24+
// build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
25+
date string
26+
)
27+
28+
// Info holds build information
29+
type Info struct {
30+
GitCommit string `json:"gitCommit"`
31+
GitVersion string `json:"GitVersion"`
32+
BuildDate string `json:"BuildDate"`
33+
}
34+
35+
// Get creates and initialized Info object
36+
func Get() Info {
37+
return Info{
38+
GitCommit: commitSHA,
39+
GitVersion: latestVersion,
40+
BuildDate: date,
41+
}
42+
}

pkg/version/command.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2017 The Jaeger Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package version
16+
17+
import (
18+
"encoding/json"
19+
"fmt"
20+
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// Command creates version command
25+
func Command() *cobra.Command {
26+
return &cobra.Command{
27+
Use: "version",
28+
Short: "Print the version",
29+
Long: `Print the version and build information`,
30+
RunE: func(cmd *cobra.Command, args []string) error {
31+
info := Get()
32+
json, err := json.Marshal(info)
33+
if err != nil {
34+
return err
35+
}
36+
fmt.Println(string(json))
37+
38+
return nil
39+
},
40+
}
41+
}

pkg/version/handler.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2017 The Jaeger Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package version
16+
17+
import (
18+
"encoding/json"
19+
"net/http"
20+
21+
"go.uber.org/zap"
22+
)
23+
24+
// RegisterHandler registers version handler to /version
25+
func RegisterHandler(mu *http.ServeMux, logger *zap.Logger) {
26+
info := Get()
27+
json, err := json.Marshal(info)
28+
if err != nil {
29+
logger.Fatal("Could not get Jaeger version", zap.Error(err))
30+
}
31+
mu.HandleFunc("/version", func(w http.ResponseWriter, _ *http.Request) {
32+
w.WriteHeader(200)
33+
w.Write(json)
34+
})
35+
}

0 commit comments

Comments
 (0)