Skip to content

Commit

Permalink
Use version from BuildInfo everywhere in service, remove internal/ver…
Browse files Browse the repository at this point in the history
…sion

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Jun 1, 2022
1 parent 3356863 commit 3648135
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 140 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ TOOLS_MOD_DIR := ./internal/tools
GOOS=$(shell $(GOCMD) env GOOS)
GOARCH=$(shell $(GOCMD) env GOARCH)

# TODO: Find a way to configure this in the generated code, currently no effect.
BUILD_INFO_IMPORT_PATH=go.opentelemetry.io/collector/internal/version
VERSION=$(shell git describe --always --match "v[0-9]*" HEAD)
BUILD_INFO=-ldflags "-X $(BUILD_INFO_IMPORT_PATH).Version=$(VERSION)"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Version allows custom builds to configure this at build time.
var Version = "{{ .Distribution.Version }}"
4 changes: 4 additions & 0 deletions cmd/otelcorecol/internal/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package internal

// Version allows custom-builds to configure this at build time.
var Version = "0.52.0-dev"
69 changes: 0 additions & 69 deletions internal/version/version.go

This file was deleted.

107 changes: 53 additions & 54 deletions service/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/internal/testcomponents"
"go.opentelemetry.io/collector/internal/testutil"
"go.opentelemetry.io/collector/internal/version"
"go.opentelemetry.io/collector/service/featuregate"
)

Expand Down Expand Up @@ -214,8 +213,8 @@ var testResourceAttrValue = "resource_attr_test_value"
var testInstanceID = "test_instance_id"
var testServiceVersion = "2022-05-20"

var ownMetricsTestCases = []ownMetricsTestCase{
{
func ownMetricsTestCases(version string) []ownMetricsTestCase {
return []ownMetricsTestCase{{
name: "no resource",
userDefinedResource: nil,
// All labels added to all collector metrics by default are listed below.
Expand All @@ -226,60 +225,60 @@ var ownMetricsTestCases = []ownMetricsTestCase{
// monitor the Collector in production deployments.
expectedLabels: map[string]labelValue{
"service_instance_id": {state: labelAnyValue},
"service_version": {label: version.Version, state: labelSpecificValue},
"service_version": {label: version, state: labelSpecificValue},
},
},
{
name: "resource with custom attr",
userDefinedResource: map[string]*string{
"custom_resource_attr": &testResourceAttrValue,
{
name: "resource with custom attr",
userDefinedResource: map[string]*string{
"custom_resource_attr": &testResourceAttrValue,
},
expectedLabels: map[string]labelValue{
"service_instance_id": {state: labelAnyValue},
"service_version": {label: version, state: labelSpecificValue},
"custom_resource_attr": {label: "resource_attr_test_value", state: labelSpecificValue},
},
},
expectedLabels: map[string]labelValue{
"service_instance_id": {state: labelAnyValue},
"service_version": {label: version.Version, state: labelSpecificValue},
"custom_resource_attr": {label: "resource_attr_test_value", state: labelSpecificValue},
},
},
{
name: "override service.instance.id",
userDefinedResource: map[string]*string{
"service.instance.id": &testInstanceID,
},
expectedLabels: map[string]labelValue{
"service_instance_id": {label: "test_instance_id", state: labelSpecificValue},
"service_version": {label: version.Version, state: labelSpecificValue},
{
name: "override service.instance.id",
userDefinedResource: map[string]*string{
"service.instance.id": &testInstanceID,
},
expectedLabels: map[string]labelValue{
"service_instance_id": {label: "test_instance_id", state: labelSpecificValue},
"service_version": {label: version, state: labelSpecificValue},
},
},
},
{
name: "suppress service.instance.id",
userDefinedResource: map[string]*string{
"service.instance.id": nil, // nil value in config is used to suppress attributes.
{
name: "suppress service.instance.id",
userDefinedResource: map[string]*string{
"service.instance.id": nil, // nil value in config is used to suppress attributes.
},
expectedLabels: map[string]labelValue{
"service_instance_id": {state: labelNotPresent},
"service_version": {label: version, state: labelSpecificValue},
},
},
expectedLabels: map[string]labelValue{
"service_instance_id": {state: labelNotPresent},
"service_version": {label: version.Version, state: labelSpecificValue},
{
name: "override service.runtimeinfo",
userDefinedResource: map[string]*string{
"service.runtimeinfo": &testServiceVersion,
},
expectedLabels: map[string]labelValue{
"service_instance_id": {state: labelAnyValue},
"service_version": {label: "2022-05-20", state: labelSpecificValue},
},
},
},
{
name: "override service.version",
userDefinedResource: map[string]*string{
"service.version": &testServiceVersion,
},
expectedLabels: map[string]labelValue{
"service_instance_id": {state: labelAnyValue},
"service_version": {label: "2022-05-20", state: labelSpecificValue},
},
},
{
name: "suppress service.version",
userDefinedResource: map[string]*string{
"service.version": nil, // nil value in config is used to suppress attributes.
},
expectedLabels: map[string]labelValue{
"service_instance_id": {state: labelAnyValue},
"service_version": {state: labelNotPresent},
},
},
{
name: "suppress service.runtimeinfo",
userDefinedResource: map[string]*string{
"service.runtimeinfo": nil, // nil value in config is used to suppress attributes.
},
expectedLabels: map[string]labelValue{
"service_instance_id": {state: labelAnyValue},
"service_version": {state: labelNotPresent},
},
}}
}

func testCollectorStartHelper(t *testing.T, telemetry collectorTelemetryExporter, tc ownMetricsTestCase) {
Expand Down Expand Up @@ -317,7 +316,7 @@ func testCollectorStartHelper(t *testing.T, telemetry collectorTelemetryExporter
require.NoError(t, err)

col, err := New(CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
BuildInfo: component.BuildInfo{Version: "test runtimeinfo"},
Factories: factories,
ConfigProvider: cfgProvider,
LoggingOptions: []zap.Option{zap.Hooks(hook)},
Expand All @@ -343,15 +342,15 @@ func testCollectorStartHelper(t *testing.T, telemetry collectorTelemetryExporter
}

func TestCollectorStartWithOpenCensusMetrics(t *testing.T) {
for _, tc := range ownMetricsTestCases {
for _, tc := range ownMetricsTestCases("test runtimeinfo") {
t.Run(tc.name, func(t *testing.T) {
testCollectorStartHelper(t, newColTelemetry(featuregate.NewRegistry()), tc)
})
}
}

func TestCollectorStartWithOpenTelemetryMetrics(t *testing.T) {
for _, tc := range ownMetricsTestCases {
for _, tc := range ownMetricsTestCases("test runtimeinfo") {
t.Run(tc.name, func(t *testing.T) {
colTel := newColTelemetry(featuregate.NewRegistry())
colTel.registry.Apply(map[string]bool{
Expand Down
1 change: 1 addition & 0 deletions service/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var _ component.Host = (*serviceHost)(nil)
type serviceHost struct {
asyncErrorChannel chan error
factories component.Factories
buildInfo component.BuildInfo

builtExporters builder.Exporters
builtReceivers builder.Receivers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,29 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package version
package runtimeinfo // import "go.opentelemetry.io/collector/internal/runtimeinfo"

import (
"strings"
"testing"
"runtime"
"time"
)

"github.com/stretchr/testify/assert"
var (
// InfoVar is a singleton instance of the Info struct.
runtimeInfoVar [][2]string
)

func TestInfoString(t *testing.T) {
infoString := InfoVar.String()
for _, el := range InfoVar {
assert.True(t, strings.Contains(infoString, el[0]))
assert.True(t, strings.Contains(infoString, el[1]))
func init() {
runtimeInfoVar = [][2]string{
{"StartTimestamp", time.Now().String()},
{"Go", runtime.Version()},
{"OS", runtime.GOOS},
{"Arch", runtime.GOARCH},
// Add other valuable runtime information here.
}
}

// Info returns the runtime information like uptime, os, arch, etc.
func Info() [][2]string {
return runtimeInfoVar
}
2 changes: 1 addition & 1 deletion service/internal/zpages/templates/properties_table.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<b>{{.Name}}:</b>
<h6>{{.Name}}</h6>
<table style="border-spacing: 0">
{{ $index := 0 }}
{{range $index, $element := .Properties}}
Expand Down
1 change: 1 addition & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func newService(set *svcSettings) (*service, error) {
telemetry: set.Telemetry,
host: &serviceHost{
factories: set.Factories,
buildInfo: set.BuildInfo,
asyncErrorChannel: set.AsyncErrorChannel,
},
}
Expand Down
7 changes: 3 additions & 4 deletions service/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (

"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/internal/obsreportconfig"
"go.opentelemetry.io/collector/internal/version"
"go.opentelemetry.io/collector/processor/batchprocessor"
semconv "go.opentelemetry.io/collector/semconv/v1.5.0"
"go.opentelemetry.io/collector/service/featuregate"
Expand All @@ -45,7 +44,7 @@ import (
// collectorTelemetry is collector's own telemetry.
var collectorTelemetry collectorTelemetryExporter = newColTelemetry(featuregate.GetRegistry())

// AddCollectorVersionTag indicates if the collector version tag should be added to all telemetry metrics
// AddCollectorVersionTag indicates if the collector runtimeinfo tag should be added to all telemetry metrics
const AddCollectorVersionTag = true

const (
Expand Down Expand Up @@ -129,8 +128,8 @@ func (tel *colTelemetry) initOnce(col *Collector) error {
if AddCollectorVersionTag {
if _, ok := resource[semconv.AttributeServiceVersion]; !ok {
// AttributeServiceVersion is not specified in the config. Use the actual
// build version.
telAttrs[semconv.AttributeServiceVersion] = version.Version
// build runtimeinfo.
telAttrs[semconv.AttributeServiceVersion] = col.set.BuildInfo.Version
}
}

Expand Down
15 changes: 12 additions & 3 deletions service/zpages.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"sort"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/internal/version"
"go.opentelemetry.io/collector/service/featuregate"
"go.opentelemetry.io/collector/service/internal/runtimeinfo"
"go.opentelemetry.io/collector/service/internal/zpages"
)

Expand All @@ -48,7 +48,9 @@ func (host *serviceHost) RegisterZPages(mux *http.ServeMux, pathPrefix string) {

func (host *serviceHost) handleServicezRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
zpages.WriteHTMLPageHeader(w, zpages.HeaderData{Title: "service"})
zpages.WriteHTMLPageHeader(w, zpages.HeaderData{Title: "Service " + host.buildInfo.Command})
zpages.WriteHTMLPropertiesTable(w, zpages.PropertiesTableData{Name: "Build Info", Properties: getBuildInfoProperties(host.buildInfo)})
zpages.WriteHTMLPropertiesTable(w, zpages.PropertiesTableData{Name: "Runtime Info", Properties: runtimeinfo.Info()})
zpages.WriteHTMLComponentHeader(w, zpages.ComponentHeaderData{
Name: "Pipelines",
ComponentEndpoint: pipelinezPath,
Expand All @@ -64,7 +66,6 @@ func (host *serviceHost) handleServicezRequest(w http.ResponseWriter, r *http.Re
ComponentEndpoint: featurezPath,
Link: true,
})
zpages.WriteHTMLPropertiesTable(w, zpages.PropertiesTableData{Name: "Build And Runtime", Properties: version.RuntimeVar()})
zpages.WriteHTMLPageFooter(w)
}

Expand Down Expand Up @@ -175,3 +176,11 @@ func getFeaturesTableData() zpages.FeatureGateTableData {

return data
}

func getBuildInfoProperties(buildInfo component.BuildInfo) [][2]string {
return [][2]string{
{"Command", buildInfo.Command},
{"Description", buildInfo.Description},
{"Version", buildInfo.Version},
}
}

0 comments on commit 3648135

Please sign in to comment.