github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/server/diagnosticspb/diagnostics.go (about)

     1  // Copyright 2020 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package diagnosticspb
    12  
    13  import (
    14  	"net/url"
    15  	"strconv"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/util/envutil"
    18  	"github.com/cockroachdb/cockroach/pkg/util/uuid"
    19  )
    20  
    21  // updatesURL is the URL used to check for new versions. Can be nil if an empty
    22  // URL is set.
    23  var updatesURL *url.URL
    24  
    25  const defaultUpdatesURL = `https://register.cockroachdb.com/api/clusters/updates`
    26  
    27  // reportingURL is the URL used to report diagnostics/telemetry. Can be nil if
    28  // an empty URL is set.
    29  var reportingURL *url.URL
    30  
    31  const defaultReportingURL = `https://register.cockroachdb.com/api/clusters/report`
    32  
    33  func init() {
    34  	var err error
    35  	updatesURL, err = url.Parse(
    36  		envutil.EnvOrDefaultString("COCKROACH_UPDATE_CHECK_URL", defaultUpdatesURL),
    37  	)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	reportingURL, err = url.Parse(
    42  		envutil.EnvOrDefaultString("COCKROACH_USAGE_REPORT_URL", defaultReportingURL),
    43  	)
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  }
    48  
    49  // TestingKnobs groups testing knobs for diagnostics.
    50  type TestingKnobs struct {
    51  	// OverrideUpdatesURL if set, overrides the URL used to check for new
    52  	// versions. It is a pointer to pointer to allow overriding to the nil URL.
    53  	OverrideUpdatesURL **url.URL
    54  
    55  	// OverrideReportingURL if set, overrides the URL used to report diagnostics.
    56  	// It is a pointer to pointer to allow overriding to the nil URL.
    57  	OverrideReportingURL **url.URL
    58  }
    59  
    60  // ClusterInfo contains cluster information that will become part of URLs.
    61  type ClusterInfo struct {
    62  	ClusterID  uuid.UUID
    63  	IsInsecure bool
    64  	IsInternal bool
    65  }
    66  
    67  // BuildUpdatesURL creates a URL to check for version updates.
    68  // If an empty updates URL is set (via empty environment variable), returns nil.
    69  func BuildUpdatesURL(clusterInfo *ClusterInfo, nodeInfo *NodeInfo, knobs *TestingKnobs) *url.URL {
    70  	url := updatesURL
    71  	if knobs != nil && knobs.OverrideUpdatesURL != nil {
    72  		url = *knobs.OverrideUpdatesURL
    73  	}
    74  	return addInfoToURL(url, clusterInfo, nodeInfo)
    75  }
    76  
    77  // BuildReportingURL creates a URL to report diagnostics.
    78  // If an empty updates URL is set (via empty environment variable), returns nil.
    79  func BuildReportingURL(clusterInfo *ClusterInfo, nodeInfo *NodeInfo, knobs *TestingKnobs) *url.URL {
    80  	url := reportingURL
    81  	if knobs != nil && knobs.OverrideReportingURL != nil {
    82  		url = *knobs.OverrideReportingURL
    83  	}
    84  	return addInfoToURL(url, clusterInfo, nodeInfo)
    85  }
    86  
    87  func addInfoToURL(url *url.URL, clusterInfo *ClusterInfo, nodeInfo *NodeInfo) *url.URL {
    88  	if url == nil {
    89  		return nil
    90  	}
    91  	result := *url
    92  	q := result.Query()
    93  	b := &nodeInfo.Build
    94  	q.Set("version", b.Tag)
    95  	q.Set("platform", b.Platform)
    96  	q.Set("uuid", clusterInfo.ClusterID.String())
    97  	q.Set("nodeid", strconv.Itoa(int(nodeInfo.NodeID)))
    98  	q.Set("uptime", strconv.Itoa(int(nodeInfo.Uptime)))
    99  	q.Set("insecure", strconv.FormatBool(clusterInfo.IsInsecure))
   100  	q.Set("internal", strconv.FormatBool(clusterInfo.IsInternal))
   101  	q.Set("buildchannel", b.Channel)
   102  	q.Set("envchannel", b.EnvChannel)
   103  	q.Set("licensetype", nodeInfo.LicenseType)
   104  	result.RawQuery = q.Encode()
   105  	return &result
   106  }