Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 5897c5c

Browse files
author
Ramon Nogueira
authored
Export a variable with the current version, for use by exporters (#775)
See: census-ecosystem/opencensus-go-exporter-stackdriver#7
1 parent b8a6dd9 commit 5897c5c

File tree

4 files changed

+116
-2
lines changed

4 files changed

+116
-2
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ script:
2424
- go vet ./...
2525
- go test -v -race $PKGS # Run all the tests with the race detector enabled
2626
- 'if [[ $TRAVIS_GO_VERSION = 1.8* ]]; then ! golint ./... | grep -vE "(_mock|_string|\.pb)\.go:"; fi'
27+
- go run internal/check/version.go

exporterutil/version.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018, OpenCensus 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 exporterutil contains common utilities for exporter implementations.
16+
package exporterutil
17+
18+
// Version is the current release version of OpenCensus in use. It is made
19+
// available for exporters to include in User-Agent-like metadata.
20+
var Version = "0.12.0"

internal/check/version.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2018, OpenCensus 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+
// Command version checks that the version string matches the latest Git tag.
16+
// This is expected to pass only on the master branch.
17+
package main
18+
19+
import (
20+
"bytes"
21+
"fmt"
22+
"log"
23+
"os"
24+
"os/exec"
25+
"sort"
26+
"strconv"
27+
"strings"
28+
29+
"go.opencensus.io/exporterutil"
30+
)
31+
32+
func main() {
33+
cmd := exec.Command("git", "tag")
34+
var buf bytes.Buffer
35+
cmd.Stdout = &buf
36+
err := cmd.Run()
37+
if err != nil {
38+
log.Fatal(err)
39+
}
40+
var versions []version
41+
for _, vStr := range strings.Split(buf.String(), "\n") {
42+
if len(vStr) == 0 {
43+
continue
44+
}
45+
versions = append(versions, parseVersion(vStr))
46+
}
47+
sort.Slice(versions, func(i, j int) bool {
48+
return versionLess(versions[i], versions[j])
49+
})
50+
latest := versions[len(versions)-1]
51+
codeVersion := parseVersion("v" + exporterutil.Version)
52+
if !versionLess(latest, codeVersion) {
53+
fmt.Printf("exporterutil.Version is out of date with Git tags. Got %s; want %s\n", latest, exporterutil.Version)
54+
os.Exit(1)
55+
}
56+
fmt.Printf("exporterutil.Version is up-to-date: %s\n", exporterutil.Version)
57+
}
58+
59+
type version [3]int
60+
61+
func versionLess(v1, v2 version) bool {
62+
for c := 0; c < 3; c++ {
63+
if diff := v1[c] - v2[c]; diff != 0 {
64+
return diff < 0
65+
}
66+
}
67+
return false
68+
}
69+
70+
func parseVersion(vStr string) version {
71+
split := strings.Split(vStr[1:], ".")
72+
var (
73+
v version
74+
err error
75+
)
76+
for i := 0; i < 3; i++ {
77+
v[i], err = strconv.Atoi(split[i])
78+
if err != nil {
79+
fmt.Printf("Unrecognized version tag %q: %s\n", vStr, err)
80+
os.Exit(2)
81+
}
82+
}
83+
return v
84+
}
85+
86+
func (v version) String() string {
87+
return fmt.Sprintf("%d.%d.%d", v[0], v[1], v[2])
88+
}

internal/internal.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@
1414

1515
package internal // import "go.opencensus.io/internal"
1616

17-
import "time"
17+
import (
18+
"fmt"
19+
"time"
20+
21+
"go.opencensus.io/exporterutil"
22+
)
1823

1924
// UserAgent is the user agent to be added to the outgoing
2025
// requests from the exporters.
21-
const UserAgent = "opencensus-go [0.12.0]"
26+
var UserAgent = fmt.Sprintf("opencensus-go [%s]", exporterutil.Version)
2227

2328
// MonotonicEndTime returns the end time at present
2429
// but offset from start, monotonically.

0 commit comments

Comments
 (0)