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

Commit

Permalink
Export a variable with the current version, for use by exporters (#775)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramon Nogueira authored Jun 4, 2018
1 parent b8a6dd9 commit 5897c5c
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ script:
- go vet ./...
- go test -v -race $PKGS # Run all the tests with the race detector enabled
- 'if [[ $TRAVIS_GO_VERSION = 1.8* ]]; then ! golint ./... | grep -vE "(_mock|_string|\.pb)\.go:"; fi'
- go run internal/check/version.go
20 changes: 20 additions & 0 deletions exporterutil/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2018, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package exporterutil contains common utilities for exporter implementations.
package exporterutil

// Version is the current release version of OpenCensus in use. It is made
// available for exporters to include in User-Agent-like metadata.
var Version = "0.12.0"
88 changes: 88 additions & 0 deletions internal/check/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2018, OpenCensus Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Command version checks that the version string matches the latest Git tag.
// This is expected to pass only on the master branch.
package main

import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"sort"
"strconv"
"strings"

"go.opencensus.io/exporterutil"
)

func main() {
cmd := exec.Command("git", "tag")
var buf bytes.Buffer
cmd.Stdout = &buf
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
var versions []version
for _, vStr := range strings.Split(buf.String(), "\n") {
if len(vStr) == 0 {
continue
}
versions = append(versions, parseVersion(vStr))
}
sort.Slice(versions, func(i, j int) bool {
return versionLess(versions[i], versions[j])
})
latest := versions[len(versions)-1]
codeVersion := parseVersion("v" + exporterutil.Version)
if !versionLess(latest, codeVersion) {
fmt.Printf("exporterutil.Version is out of date with Git tags. Got %s; want %s\n", latest, exporterutil.Version)
os.Exit(1)
}
fmt.Printf("exporterutil.Version is up-to-date: %s\n", exporterutil.Version)
}

type version [3]int

func versionLess(v1, v2 version) bool {
for c := 0; c < 3; c++ {
if diff := v1[c] - v2[c]; diff != 0 {
return diff < 0
}
}
return false
}

func parseVersion(vStr string) version {
split := strings.Split(vStr[1:], ".")
var (
v version
err error
)
for i := 0; i < 3; i++ {
v[i], err = strconv.Atoi(split[i])
if err != nil {
fmt.Printf("Unrecognized version tag %q: %s\n", vStr, err)
os.Exit(2)
}
}
return v
}

func (v version) String() string {
return fmt.Sprintf("%d.%d.%d", v[0], v[1], v[2])
}
9 changes: 7 additions & 2 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@

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

import "time"
import (
"fmt"
"time"

"go.opencensus.io/exporterutil"
)

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

// MonotonicEndTime returns the end time at present
// but offset from start, monotonically.
Expand Down

0 comments on commit 5897c5c

Please sign in to comment.