> For the complete documentation index, see [llms.txt](https://docs.apica.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apica.io/flow/opentelemetry-ingest/opentelemetry-metrics/transforming-metrics-through-code-rules.md).

# Transforming Metrics through Code Rules

## OTLP Metric Code Rule Examples

Here are common patterns to help you enrich, clean, or modify incoming OTLP-formatted metrics.

***

#### &#x20;1. **Add Custom Attribute to All Gauge Metrics**

```javascript
const u = ascent.decode.unflatten(Event);

u.resourceMetrics.forEach((rm) => {
    rm.scopeMetrics.forEach((sm) => {
        sm.metrics.forEach((m) => {
            if (m.gauge) {
                m.gauge.dataPoints.forEach((dp) => {
                    dp.attributes ||= [];
                    dp.attributes.push({
                        key: "env",
                        value: { stringValue: "prod" }
                    });
                });
            }
        });
    });
});

Event = ascent.encode.flatten(u);
```

***

#### 🧹2. **Drop Metric by Name**

Drop all metrics with the name `http_requests_unwanted`.

```javascript
const u = ascent.decode.unflatten(Event);

u.resourceMetrics.forEach((rm) => {
    rm.scopeMetrics.forEach((sm) => {
        sm.metrics = sm.metrics.filter(m => m.name !== "http_requests_unwanted");
    });
});

Event = ascent.encode.flatten(u);
```

***

#### 3. **Rename Metric**

Rename `latency_ms` to `request_duration_ms`.

```javascript
const u = ascent.decode.unflatten(Event);

u.resourceMetrics.forEach((rm) => {
    rm.scopeMetrics.forEach((sm) => {
        sm.metrics.forEach((m) => {
            if (m.name === "latency_ms") {
                m.name = "request_duration_ms";
            }
        });
    });
});

Event = ascent.encode.flatten(u);
```

***

#### 4. **Convert Gauge to Sum**

Convert a `gauge` to a `sum` with cumulative aggregation.

```javascript
const u = ascent.decode.unflatten(Event);

u.resourceMetrics.forEach((rm) => {
    rm.scopeMetrics.forEach((sm) => {
        sm.metrics.forEach((m) => {
            if (m.gauge) {
                m.sum = {
                    dataPoints: m.gauge.dataPoints,
                    aggregationTemporality: 2,  // CUMULATIVE
                    isMonotonic: false
                };
                delete m.gauge;
            }
        });
    });
});

Event = ascent.encode.flatten(u);
```

***

#### &#x20;5. **Filter Out Data Points by Value**

Only keep gauge datapoints where value > 0.

```javascript
const u = ascent.decode.unflatten(Event);

u.resourceMetrics.forEach((rm) => {
    rm.scopeMetrics.forEach((sm) => {
        sm.metrics.forEach((m) => {
            if (m.gauge) {
                m.gauge.dataPoints = m.gauge.dataPoints.filter(dp => dp.asDouble > 0);
            }
        });
    });
});

Event = ascent.encode.flatten(u);
```

***

&#x20;6\. **Tag Metrics Based on Name**

Add a `type: db` attribute if metric name contains `sql`.

```javascript
const u = ascent.decode.unflatten(Event);

u.resourceMetrics.forEach((rm) => {
    rm.scopeMetrics.forEach((sm) => {
        sm.metrics.forEach((m) => {
            if (m.name.includes("sql")) {
                (m.gauge?.dataPoints || []).forEach((dp) => {
                    dp.attributes ||= [];
                    dp.attributes.push({
                        key: "type",
                        value: { stringValue: "db" }
                    });
                });
            }
        });
    });
});

Event = ascent.encode.flatten(u);
```

***

#### Tips

* Always wrap transformations in a try/catch if you're unsure about event structure.
* You can modify `resource.attributes`, `scope.name`, or any part of the OTLP model.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.apica.io/flow/opentelemetry-ingest/opentelemetry-metrics/transforming-metrics-through-code-rules.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
