github.com/ddev/ddev@v1.23.2-0.20240519125000-d824ffe36ff3/pkg/ddevapp/instrumentation.go (about)

     1  package ddevapp
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"runtime"
     7  	"strconv"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/ddev/ddev/pkg/dockerutil"
    12  	"github.com/ddev/ddev/pkg/globalconfig"
    13  	"github.com/ddev/ddev/pkg/nodeps"
    14  	"github.com/ddev/ddev/pkg/version"
    15  	"github.com/ddev/ddev/pkg/versionconstants"
    16  	"github.com/denisbrodbeck/machineid"
    17  )
    18  
    19  var hashedHostID string
    20  
    21  // ReportableEvents is the list of events that we choose to report specifically.
    22  // Excludes non-ddev custom commands.
    23  var ReportableEvents = map[string]bool{"start": true}
    24  
    25  // GetInstrumentationUser normally gets the hashed hostID but if an explicit
    26  // user is provided in global_config.yaml that will be prepended.
    27  func GetInstrumentationUser() string {
    28  	return hashedHostID
    29  }
    30  
    31  // SetInstrumentationBaseTags sets the basic always-used tags for telemetry
    32  func SetInstrumentationBaseTags() {
    33  	// defer util.TimeTrack()()
    34  
    35  	if globalconfig.DdevGlobalConfig.InstrumentationOptIn {
    36  		dockerVersion, _ := dockerutil.GetDockerVersion()
    37  		dockerPlaform, _ := version.GetDockerPlatform()
    38  		timezone, _ := time.Now().In(time.Local).Zone()
    39  		lang := os.Getenv("LANG")
    40  
    41  		nodeps.InstrumentationTags["OS"] = runtime.GOOS
    42  		nodeps.InstrumentationTags["architecture"] = runtime.GOARCH
    43  		wslDistro := nodeps.GetWSLDistro()
    44  		if wslDistro != "" {
    45  			nodeps.InstrumentationTags["isWSL"] = "true"
    46  			nodeps.InstrumentationTags["wslDistro"] = wslDistro
    47  			nodeps.InstrumentationTags["OS"] = "wsl2"
    48  		}
    49  		nodeps.InstrumentationTags["dockerVersion"] = dockerVersion
    50  		nodeps.InstrumentationTags["dockerPlatform"] = dockerPlaform
    51  		nodeps.InstrumentationTags["dockerToolbox"] = strconv.FormatBool(false)
    52  		nodeps.InstrumentationTags["version"] = versionconstants.DdevVersion
    53  		nodeps.InstrumentationTags["ServerHash"] = GetInstrumentationUser()
    54  		nodeps.InstrumentationTags["timezone"] = timezone
    55  		nodeps.InstrumentationTags["language"] = lang
    56  	}
    57  }
    58  
    59  // SetInstrumentationAppTags creates app-specific tags for telemetry
    60  func (app *DdevApp) SetInstrumentationAppTags() {
    61  	// defer util.TimeTrack()()
    62  
    63  	ignoredProperties := []string{"approot", "hostname", "hostnames", "name", "router_status_log", "shortroot"}
    64  
    65  	describeTags, _ := app.Describe(false)
    66  	for key, val := range describeTags {
    67  		// Make sure none of the "URL" attributes or the ignoredProperties comes through
    68  		if strings.Contains(strings.ToLower(key), "url") || nodeps.ArrayContainsString(ignoredProperties, key) {
    69  			continue
    70  		}
    71  		nodeps.InstrumentationTags[key] = fmt.Sprintf("%v", val)
    72  	}
    73  	nodeps.InstrumentationTags["ProjectID"] = app.ProtectedID()
    74  }
    75  
    76  func init() {
    77  	hashedHostID, _ = machineid.ProtectedID("ddev")
    78  }