Skip to content

Commit

Permalink
Use null value to suppress built-in resource attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrannajaryan committed May 24, 2022
1 parent 9c4bc84 commit 4638360
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
4 changes: 2 additions & 2 deletions config/moved_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ type ServiceTelemetry struct {
// For metrics the attributes become Prometheus labels.
// Note that some attributes are added automatically (e.g. service.version) even
// if they are not specified here. In order to suppress such attributes the
// attribute must be specified in this map with empty string value.
Resource map[string]string `mapstructure:"resource"`
// attribute must be specified in this map with null YAML value (nil string pointer).
Resource map[string]*string `mapstructure:"resource"`
}

// ServiceTelemetryLogs defines the configurable settings for service telemetry logs.
Expand Down
34 changes: 19 additions & 15 deletions service/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestCollectorFailedShutdown(t *testing.T) {

// mapConverter applies extraMap of config settings. Useful for overriding the config
// for testing purposes. Keys must use "::" delimiter between levels.
func mapConverter(extraMap map[string]string) config.MapConverterFunc {
func mapConverter(extraMap map[string]*string) config.MapConverterFunc {
return func(ctx context.Context, cfgMap *config.Map) error {
for k, v := range extraMap {
cfgMap.Set(k, v)
Expand All @@ -204,10 +204,14 @@ type labelValue struct {

type ownMetricsTestCase struct {
name string
userDefinedResource map[string]string
userDefinedResource map[string]*string
expectedLabels map[string]labelValue
}

var testResourceAttrValue = "resource_attr_test_value"
var testInstanceId = "test_instance_id"
var testServiceVersion = "2022-05-20"

var ownMetricsTestCases = []ownMetricsTestCase{
{
name: "no resource",
Expand All @@ -225,8 +229,8 @@ var ownMetricsTestCases = []ownMetricsTestCase{
},
{
name: "resource with custom attr",
userDefinedResource: map[string]string{
"custom_resource_attr": "resource_attr_test_value",
userDefinedResource: map[string]*string{
"custom_resource_attr": &testResourceAttrValue,
},
expectedLabels: map[string]labelValue{
"service_instance_id": {state: labelAnyValue},
Expand All @@ -236,18 +240,18 @@ var ownMetricsTestCases = []ownMetricsTestCase{
},
{
name: "override service.instance.id",
userDefinedResource: map[string]string{
"service.instance.id": "my_instance_id",
userDefinedResource: map[string]*string{
"service.instance.id": &testInstanceId,
},
expectedLabels: map[string]labelValue{
"service_instance_id": {label: "my_instance_id", state: labelSpecificValue},
"service_instance_id": {label: "test_instance_id", state: labelSpecificValue},
"service_version": {label: version.Version, state: labelSpecificValue},
},
},
{
name: "suppress service.instance.id",
userDefinedResource: map[string]string{
"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},
Expand All @@ -256,8 +260,8 @@ var ownMetricsTestCases = []ownMetricsTestCase{
},
{
name: "override service.version",
userDefinedResource: map[string]string{
"service.version": "2022-05-20",
userDefinedResource: map[string]*string{
"service.version": &testServiceVersion,
},
expectedLabels: map[string]labelValue{
"service_instance_id": {state: labelAnyValue},
Expand All @@ -266,8 +270,8 @@ var ownMetricsTestCases = []ownMetricsTestCase{
},
{
name: "suppress service.version",
userDefinedResource: map[string]string{
"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},
Expand All @@ -294,9 +298,9 @@ func testCollectorStartHelper(t *testing.T, telemetry collectorTelemetryExporter
})

// Prepare config properties to be merged with the main config.
extraCfgAsProps := map[string]string{
extraCfgAsProps := map[string]*string{
// Set the metrics address to expose own metrics on.
"service::telemetry::metrics::address": metricsAddr,
"service::telemetry::metrics::address": &metricsAddr,
}
// Also include resource attributes under the service.telemetry.resource key.
for k, v := range tc.userDefinedResource {
Expand Down
14 changes: 8 additions & 6 deletions service/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,28 @@ func (tel *colTelemetry) initOnce(col *Collector) error {
logger.Info("Setting up own telemetry...")

if resource == nil {
resource = map[string]string{}
resource = map[string]*string{}
}

if _, ok := resource[semconv.AttributeServiceInstanceID]; !ok {
// AttributeServiceInstanceID is not specified in the config. Auto-generate one.
instanceUUID, _ := uuid.NewRandom()
instanceID := instanceUUID.String()
resource[semconv.AttributeServiceInstanceID] = instanceID
resource[semconv.AttributeServiceInstanceID] = &instanceID
}

if AddCollectorVersionTag {
if _, ok := resource[semconv.AttributeServiceVersion]; !ok {
// AttributeServiceVersion is not specified in the config. Use the actual
// build version.
resource[semconv.AttributeServiceVersion] = version.Version
resource[semconv.AttributeServiceVersion] = &version.Version
}
}

// Empty Resource attributes are an indication by the user to suppress and stop
// emitting the particular attribute.
for k, v := range resource {
if v == "" {
if v == nil {
delete(resource, k)
}
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func (tel *colTelemetry) initOnce(col *Collector) error {
return nil
}

func (tel *colTelemetry) initOpenCensus(col *Collector, labels map[string]string) (http.Handler, error) {
func (tel *colTelemetry) initOpenCensus(col *Collector, labels map[string]*string) (http.Handler, error) {
processMetricsViews, err := telemetry2.NewProcessMetricsViews(getBallastSize(col.service.host))
if err != nil {
return nil, err
Expand All @@ -203,7 +203,9 @@ func (tel *colTelemetry) initOpenCensus(col *Collector, labels map[string]string
opts.ConstLabels = make(map[string]string)

for k, v := range labels {
opts.ConstLabels[sanitizePrometheusKey(k)] = v
if v != nil {
opts.ConstLabels[sanitizePrometheusKey(k)] = *v
}
}

pe, err := prometheus.NewExporter(opts)
Expand Down

0 comments on commit 4638360

Please sign in to comment.