# Labeled Strings

Labeled strings record multiple Unicode string values, each under a different label.

## Recording API

### set

Sets one of the labels in a labeled string metric to a specific value.

import org.mozilla.yourApplication.GleanMetrics.Login


import org.mozilla.yourApplication.GleanMetrics.Login;


Login.errorsByStage["server_auth"].set("Invalid password")

from glean import load_metrics



#![allow(unused)]
fn main() {
use glean_metrics;

}

import * as login from "./path/to/generated/files/login.js";



C++

#include "mozilla/glean/GleanMetrics.h"



JavaScript

Glean.login.errorsByStage["server_auth"].set("Invalid password");


#### Limits

• Fixed maximum string length: 100. Longer strings are truncated. This is measured in the number of bytes when the string is encoded in UTF-8.

#### Recorded Errors

• invalid_overflow: if the string is too long. (Prior to Glean 31.5.0, this recorded an invalid_value).
• invalid_label:
• If the label contains invalid characters. Data is still recorded to the special label __other__.
• If the label exceeds the maximum number of allowed characters. Data is still recorded to the special label __other__.

## Testing API

### testGetValue

Gets the recorded value for a given label in a labeled string metric.

import org.mozilla.yourApplication.GleanMetrics.Login

// Does the metric have the expected value?

import org.mozilla.yourApplication.GleanMetrics.Login;

// Does the metric have the expected value?

@testable import Glean

// Does the metric have the expected value?

from glean import load_metrics

# Does the metric have the expected value?


#![allow(unused)]
fn main() {
use glean_metrics;

// Does the metric have the expected value?
}

import * as login from "./path/to/generated/files/login.js";

// Does the metric have the expected value?


C++

#include "mozilla/glean/GleanMetrics.h"

.TestGetValue()
.unwrap()
.ref()
.get());


JavaScript

Assert.equal("Invalid password", Glean.login.errorsByStage["server_auth"].testGetValue());


### testHasValue

Whether or not any value was recorded for a given label in a labeled string metric.

import org.mozilla.yourApplication.GleanMetrics.Login

// Was anything recorded?

import org.mozilla.yourApplication.GleanMetrics.Login;

// Was anything recorded?

@testable import Glean

// Was anything recorded?

# Was anything recorded?


### testGetNumRecordedErrors

Gets the number of errors recorded for a given labeled string metric in total.

import org.mozilla.yourApplication.GleanMetrics.Login

// Were there any invalid labels?

import org.mozilla.yourApplication.GleanMetrics.Login;

// Were there any invalid labels?

@testable import Glean

// Were there any invalid labels?

from glean import load_metrics

# Were there any invalid labels?
ErrorType.INVALID_LABEL
)


#![allow(unused)]
fn main() {
use glean::ErrorType;

use glean_metrics;

// Were there any invalid labels?
assert_eq!(
0,
ErrorType::InvalidLabel
)
);
}

import * as login from "./path/to/generated/files/login.js";
import { ErrorType } from "@mozilla/glean/<platform>";

// Were there any invalid labels?
assert(
0,
);


## Metric parameters

Example labeled boolean metric definition:

login:
errors_by_stage:
type: labeled_string
description: Records the error type, if any, that occur in different stages of the login process.
bugs:
- https://bugzilla.mozilla.org/000000
data_reviews:
- https://bugzilla.mozilla.org/show_bug.cgi?id=000000#c3
- me@mozilla.com
expires: 2020-10-01
labels:
- server_auth
- enter_email
...


### Extra metric parameters

#### labels

Labeled metrics may have an optional labels parameter, containing a list of known labels. The labels in this list must match the following requirements:

##### Important

If the labels are specified in the metrics.yaml, using any label not listed in that file will be replaced with the special value __other__.

If the labels are not specified in the metrics.yaml, only 16 different dynamic labels may be used, after which the special value __other__ will be used.

Removing or changing labels, including their order in the registry file, is permitted. Avoid reusing labels that were removed in the past. It is best practice to add documentation about removed labels to the description field so that analysts will know of their existence and meaning in historical data. Special care must be taken when changing GeckoView metrics sent through the Glean SDK, as the index of the labels is used to report Gecko data through the Glean SDK.

## Data questions

• What kinds of errors occurred at each step in the login process?