Source file src/cmd/go/internal/telemetrystats/version_unix.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !cmd_go_bootstrap && unix
     6  
     7  package telemetrystats
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"runtime"
    13  	"strings"
    14  
    15  	"cmd/internal/telemetry/counter"
    16  
    17  	"golang.org/x/sys/unix"
    18  )
    19  
    20  func incrementVersionCounters() {
    21  	convert := func(nullterm []byte) string {
    22  		end := bytes.IndexByte(nullterm, 0)
    23  		if end < 0 {
    24  			end = len(nullterm)
    25  		}
    26  		return string(nullterm[:end])
    27  	}
    28  
    29  	var v unix.Utsname
    30  	err := unix.Uname(&v)
    31  	if err != nil {
    32  		counter.Inc(fmt.Sprintf("go/platform/host/%s/version:unknown-uname-error", runtime.GOOS))
    33  		return
    34  	}
    35  	major, minor, ok := majorMinor(convert(v.Release[:]))
    36  	if runtime.GOOS == "aix" {
    37  		major, minor, ok = convert(v.Version[:]), convert(v.Release[:]), true
    38  	}
    39  	if !ok {
    40  		counter.Inc(fmt.Sprintf("go/platform/host/%s/version:unknown-bad-format", runtime.GOOS))
    41  		return
    42  	}
    43  	counter.Inc(fmt.Sprintf("go/platform/host/%s/major-version:%s", runtime.GOOS, major))
    44  	counter.Inc(fmt.Sprintf("go/platform/host/%s/version:%s-%s", runtime.GOOS, major, minor))
    45  }
    46  
    47  func majorMinor(v string) (string, string, bool) {
    48  	firstDot := strings.Index(v, ".")
    49  	if firstDot < 0 {
    50  		return "", "", false
    51  	}
    52  	major := v[:firstDot]
    53  	v = v[firstDot+len("."):]
    54  	endMinor := strings.IndexAny(v, ".-_")
    55  	if endMinor < 0 {
    56  		endMinor = len(v)
    57  	}
    58  	minor := v[:endMinor]
    59  	return major, minor, true
    60  }
    61  

View as plain text