# API reference

Copy as Markdown[Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fcoralogix.com%2Fdocs%2Fuser-guides%2Frum%2Fsdk-installation%2Fandroid%2Ffeatures%2Fapi-reference.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fcoralogix.com%2Fdocs%2Fuser-guides%2Frum%2Fsdk-installation%2Fandroid%2Ffeatures%2Fapi-reference.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)

## API Reference[​](#api-reference "Direct link to API Reference")

### User Context[​](#user-context "Direct link to User Context")

Attach or retrieve user-level context to correlate events.

```
CoralogixRum.setUserContext(

    UserContext(

        userId = "12345",

        username = "john_doe",

        email = "user@example.com",

        metadata = mapOf("country" to "USA")

    )

)



val context: UserContext = CoralogixRum.getUserContext()
```

Note

the `getUserContext` returns a `UserContext` object with empty values if the Coralogix SDK is not initialized.

### Application Context[​](#application-context "Direct link to Application Context")

Attach application context to correlate events.

```
CoralogixRum.setApplicationContext(

    appName = "MyApp",

    appVersion = "1.0.0"

)
```

### Session ID[​](#session-id "Direct link to Session ID")

Retrieve the session id from the SDK.

```
val sessionId: String = CoralogixRum.getSessionId()
```

Note

the `getSessionId` returns an empty `String` if the Coralogix SDK is not initialized.

### New Session[​](#new-session "Direct link to New Session")

Force-start a fresh RUM session on demand — typically on user logout — without re-initializing the SDK. A new session id is issued and the per-session state (snapshot state, error/action counts, Session Replay, view counter) resets, exactly like the automatic idle / max-age rotation.

```
CoralogixRum.createNewSession()
```

On a logout → login flow, pair it with `setUserContext` for the new user. Events in the new session keep the current view name; `view_number` restarts from the current view — it is re-stamped as view 0 immediately, so events on the current screen keep carrying `view_number`, and the next navigation to a different screen increments it to 1.

### Labels[​](#labels "Direct link to Labels")

Attach or retrieve global labels applied to all events.

```
CoralogixRum.setLabels(

    mapOf("environment" to "staging", "buildType" to "debug")

)



val labels: Map<String, Any?> = CoralogixRum.getLabels()
```

Note

the `getLabels` returns an empty map if the Coralogix SDK is not initialized.

### View Context[​](#view-context "Direct link to View Context")

Attach a view context to correlate events.

```
CoralogixRum.setViewContext(viewName = "Main View")
```

### Logging[​](#logging "Direct link to Logging")

Send structured logs with optional data and labels.

```
CoralogixRum.log(

    severity = CoralogixLogSeverity.Info,

    message = "User logged in successfully",

    data = mapOf("userId" to "12345"), // optional

    labels = mapOf("environment" to "staging") // optional

)
```

`CoralogixLogSeverity` is a sealed class with six levels:

| Severity                        | Level |
| ------------------------------- | ----- |
| `CoralogixLogSeverity.Debug`    | 1     |
| `CoralogixLogSeverity.Verbose`  | 2     |
| `CoralogixLogSeverity.Info`     | 3     |
| `CoralogixLogSeverity.Warn`     | 4     |
| `CoralogixLogSeverity.Error`    | 5     |
| `CoralogixLogSeverity.Critical` | 6     |

### Custom Measurements[​](#custom-measurements "Direct link to Custom Measurements")

Send arbitrary key-value pairs.

```
CoralogixRum.sendCustomMeasurement("image_upload_time_ms", 1480L)
```

### Custom Time Measurement[​](#custom-time-measurement "Direct link to Custom Time Measurement")

Measure the duration of any operation by wrapping it with `startTimeMeasure` / `endTimeMeasure`. The SDK records the elapsed time and emits a `Measurement` event with the duration in milliseconds.

```
// Start timing — optionally attach labels that will appear on the event

CoralogixRum.startTimeMeasure("checkout-flow", mapOf("cart.items" to 3))



// … perform the operation …



// Stop timing — emits the measurement event

CoralogixRum.endTimeMeasure("checkout-flow")
```

#### Behaviour[​](#behaviour "Direct link to Behaviour")

* **Duplicate starts are ignored.** If `startTimeMeasure` is called a second time with the same name before `endTimeMeasure`, the second call is a no-op and the original start time is preserved.
* **Labels are merged with SDK-level labels.** Labels passed to `startTimeMeasure` are merged with `CoralogixOptions.labels` (call-site labels take priority on conflicts).
* **Session idle discards in-flight measurements.** If the session goes idle between `start` and `end`, the measurement is silently dropped and `endTimeMeasure` is a no-op.
* **Unmatched `endTimeMeasure` is a no-op.** Calling `endTimeMeasure` without a prior `startTimeMeasure` (or after the measurement was already ended) does nothing.

### Error Reporting[​](#error-reporting "Direct link to Error Reporting")

Report handled or unhandled exceptions.

```
try {

    riskyOperation()

} catch (t: Throwable) {

    CoralogixRum.reportError(t)

}
```

### Shutdown[​](#shutdown "Direct link to Shutdown")

Gracefully shut down the SDK when your app is terminated.

```
CoralogixRum.shutdown()
```
