# 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


use glean_metrics::login;


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.
• Labels must conform to the label formatting regular expression.
• Each label must have a maximum of 60 bytes, when encoded as UTF-8.
• The list of labels is limited to:
• 16 different dynamic labels if no static labels are defined. Additional labels will all record to the special label __other__.
• 100 labels if specified as static labels in metrics.yaml, see Labels. Unknown labels will be recorded under the special label __other__.

## Testing API

### testGetValue

Gets the recorded value for a given label in a labeled string metric.
Returns the string if data is stored.
Returns a language-specific empty/null value if no data is stored.

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?

// Does the metric have the expected value?

from glean import load_metrics

# Does the metric have the expected value?

use glean_metrics::login;

// 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());


### 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?
assertEquals(
0,
)

import org.mozilla.yourApplication.GleanMetrics.Login;

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

// Were there any invalid labels?

from glean import load_metrics

# Were there any invalid labels?
assert 0 == metrics.login.errors_by_stage.test_get_num_recorded_errors(
ErrorType.INVALID_LABEL
)

use glean::ErrorType;

// 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/error";

// 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?