

# Saída do fluxo de métricas do CloudWatch no formato OpenTelemetry 1.0.0
<a name="CloudWatch-metric-streams-formats-opentelemetry-100"></a>

**nota**  
Com o formato OpenTelemetry 1.0.0, os atributos de métricas são codificados como uma lista de objetos `KeyValue` em vez do tipo `StringKeyValue` usado no formato 0.7.0. Como consumidor, essa é a única grande mudança entre os formatos 0.7.0 e 1.0.0. Um analisador gerado nos arquivos de protocolo do 0.7.0 não analisará atributos de métricas codificados no formato 1.0.0. O mesmo acontece ao contrário: um analisador gerado nos arquivos de protocolo do 1.0.0 não analisará atributos de métricas codificados no formato 0.7.0.

OpenTelemetry é uma coleção de ferramentas, APIs e SDKs. Pode ser usada para instrumentar, gerar, coletar e exportar dados de telemetria (métricas, logs e rastreamentos) para análise. O OpenTelemetry faz parte da Cloud Native Computing Foundation. Para obter mais informações, consulte [OpenTelemetry](https://opentelemetry.io/).

Para obter informações sobre a especificação completa do OpenTelemetry 1.0.0, consulte [Release version 1.0.0](https://github.com/open-telemetry/opentelemetry-proto/releases/tag/v1.0.0).

Um registro do Kinesis pode conter uma ou mais estruturas de dados `ExportMetricsServiceRequest` do OpenTelemetry. Cada estrutura de dados começa com um cabeçalho com `UnsignedVarInt32`indicando o tamanho do registro em bytes. Cada `ExportMetricsServiceRequest` pode conter dados de várias métricas ao mesmo tempo.

Veja a seguir uma representação de string da mensagem da estrutura de dados `ExportMetricsServiceRequest` do OpenTelemetry. O OpenTelemetry serializa o uso do protocolo binário do Google Protocol Buffers, e não é legível por humanos.

```
resource_metrics {
  resource {
    attributes {
      key: "cloud.provider"
      value {
        string_value: "aws"
      }
    }
    attributes {
      key: "cloud.account.id"
      value {
        string_value: "123456789012"
      }
    }
    attributes {
      key: "cloud.region"
      value {
        string_value: "us-east-1"
      }
    }
    attributes {
      key: "aws.exporter.arn"
      value {
        string_value: "arn:aws:cloudwatch:us-east-1:123456789012:metric-stream/MyMetricStream"
      }
    }
  }
  scope_metrics {
    metrics {
      name: "amazonaws.com/AWS/DynamoDB/ConsumedReadCapacityUnits"
      unit: "NoneTranslated"
      summary {
        data_points {
          start_time_unix_nano: 60000000000
          time_unix_nano: 120000000000
          count: 1
          sum: 1.0
          quantile_values {
            value: 1.0
          }
          quantile_values {
            quantile: 0.95
            value: 1.0
          }
          quantile_values {
            quantile: 0.99
            value: 1.0
          }
          quantile_values {
            quantile: 1.0
            value: 1.0
          }
          attributes {
            key: "Namespace"
            value {
              string_value: "AWS/DynamoDB"
            }
          }
          attributes {
            key: "MetricName"
            value {
              string_value: "ConsumedReadCapacityUnits"
            }
          }
          attributes {
            key: "Dimensions"
            value {
              kvlist_value {
                values {
                  key: "TableName"
                  value {
                    string_value: "MyTable"
                  }
                }
              }
            }
          }
        }
        data_points {
          start_time_unix_nano: 70000000000
          time_unix_nano: 130000000000
          count: 2
          sum: 5.0
          quantile_values {
            value: 2.0
          }
          quantile_values {
            quantile: 1.0
            value: 3.0
          }
          attributes {
            key: "Namespace"
            value {
              string_value: "AWS/DynamoDB"
            }
          }
          attributes {
            key: "MetricName"
            value {
              string_value: "ConsumedReadCapacityUnits"
            }
          }
          attributes {
            key: "Dimensions"
            value {
              kvlist_value {
                values {
                  key: "TableName"
                  value {
                    string_value: "MyTable"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
```

**Objeto de nível superior para serializar dados de métrica do OpenTelemetry**

`ExportMetricsServiceRequest` é o wrapper de nível mais alto para serializar uma carga útil do exportador do OpenTelemetry. Contém um ou mais `ResourceMetrics`.

```
message ExportMetricsServiceRequest {
  // An array of ResourceMetrics.
  // For data coming from a single resource this array will typically contain one
  // element. Intermediary nodes (such as OpenTelemetry Collector) that receive
  // data from multiple origins typically batch the data before forwarding further and
  // in that case this array will contain multiple elements.
  repeated opentelemetry.proto.metrics.v1.ResourceMetrics resource_metrics = 1;
}
```

`ResourceMetrics` é o objeto de nível superior para representar objetos MetricData. 

```
// A collection of ScopeMetrics from a Resource.
message ResourceMetrics {
  reserved 1000;

  // The resource for the metrics in this message.
  // If this field is not set then no resource info is known.
  opentelemetry.proto.resource.v1.Resource resource = 1;

  // A list of metrics that originate from a resource.
  repeated ScopeMetrics scope_metrics = 2;

  // This schema_url applies to the data in the "resource" field. It does not apply
  // to the data in the "scope_metrics" field which have their own schema_url field.
  string schema_url = 3;
}
```

**O objeto Resource**

`Resource` é um objeto de par de valor que contém algumas informações sobre o recurso que gerou as métricas. Para métricas criadas pela AWS, a estrutura de dados contém o nome do recurso da Amazon (ARN) do recurso relacionado à métrica, como uma instância do EC2 ou um bucket do S3.

O objeto `Resource` contém um atributo chamado `attributes`, que armazena uma lista de pares de chave-valor.
+ `cloud.account.id` contém o ID da conta
+ `cloud.region` contém a Região
+ `aws.exporter.arn` contém o ARN do fluxo de métricas
+ `cloud.provider` é sempre `aws`.

```
// Resource information.
message Resource {
  // Set of attributes that describe the resource.
  // Attribute keys MUST be unique (it is not allowed to have more than one
  // attribute with the same key).
  repeated opentelemetry.proto.common.v1.KeyValue attributes = 1;

  // dropped_attributes_count is the number of dropped attributes. If the value is 0, then
  // no attributes were dropped.
  uint32 dropped_attributes_count = 2;
}
```

**O objeto ScopeMetrics**

O campo `scope` não será preenchido. Preencheremos apenas o campo de métricas que estamos exportando.

```
// A collection of Metrics produced by an Scope.
message ScopeMetrics {
  // The instrumentation scope information for the metrics in this message.
  // Semantically when InstrumentationScope isn't set, it is equivalent with
  // an empty instrumentation scope name (unknown).
  opentelemetry.proto.common.v1.InstrumentationScope scope = 1;

  // A list of metrics that originate from an instrumentation library.
  repeated Metric metrics = 2;

  // This schema_url applies to all metrics in the "metrics" field.
  string schema_url = 3;
}
```

**O objeto Metric**

O objeto de métricas contém alguns metadados e um campo de dados `Summary` que contém uma lista de `SummaryDataPoint`.

Para fluxos de métricas, os metadados são:
+ `name` será `amazonaws.com/metric_namespace/metric_name`
+ `description` ficará em branco
+ `unit` será preenchido mapeando a unidade do dado métrico para a variante que distingue maiúsculas e minúsculas do código unificado para unidades de medida. Para obter mais informações, consulte [Conversões com o formato OpenTelemetry 1.0.0 no CloudWatch](CloudWatch-metric-streams-formats-opentelemetry-translation-100.md) e [Código unificado para unidades de medida](https://ucum.org/ucum.html).
+ `type` O será `SUMMARY`

```
message Metric {
  reserved 4, 6, 8;

  // name of the metric, including its DNS name prefix. It must be unique.
  string name = 1;

  // description of the metric, which can be used in documentation.
  string description = 2;

  // unit in which the metric value is reported. Follows the format
  // described by http://unitsofmeasure.org/ucum.html.
  string unit = 3;

  // Data determines the aggregation type (if any) of the metric, what is the
  // reported value type for the data points, as well as the relatationship to
  // the time interval over which they are reported.
  oneof data {
    Gauge gauge = 5;
    Sum sum = 7;
    Histogram histogram = 9;
    ExponentialHistogram exponential_histogram = 10;
    Summary summary = 11;
  }
}

message Summary {
  repeated SummaryDataPoint data_points = 1;
}
```

**O objeto SummaryDataPoint**

O objeto SummaryDataPoint contém o valor de um único ponto de dados em uma série temporal em uma métrica DoubleSummary.

```
// SummaryDataPoint is a single data point in a timeseries that describes the
// time-varying values of a Summary metric.
message SummaryDataPoint {
  reserved 1;

  // The set of key/value pairs that uniquely identify the timeseries from
  // where this point belongs. The list may be empty (may contain 0 elements).
  // Attribute keys MUST be unique (it is not allowed to have more than one
  // attribute with the same key).
  repeated opentelemetry.proto.common.v1.KeyValue attributes = 7;

  // StartTimeUnixNano is optional but strongly encouraged, see the
  // the detailed comments above Metric.
  //
  // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January
  // 1970.
  fixed64 start_time_unix_nano = 2;

  // TimeUnixNano is required, see the detailed comments above Metric.
  //
  // Value is UNIX Epoch time in nanoseconds since 00:00:00 UTC on 1 January
  // 1970.
  fixed64 time_unix_nano = 3;

  // count is the number of values in the population. Must be non-negative.
  fixed64 count = 4;

  // sum of the values in the population. If count is zero then this field
  // must be zero.
  //
  // Note: Sum should only be filled out when measuring non-negative discrete
  // events, and is assumed to be monotonic over the values of these events.
  // Negative events *can* be recorded, but sum should not be filled out when
  // doing so.  This is specifically to enforce compatibility w/ OpenMetrics,
  // see: https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#summary
  double sum = 5;

  // Represents the value at a given quantile of a distribution.
  //
  // To record Min and Max values following conventions are used:
  // - The 1.0 quantile is equivalent to the maximum value observed.
  // - The 0.0 quantile is equivalent to the minimum value observed.
  //
  // See the following issue for more context:
  // https://github.com/open-telemetry/opentelemetry-proto/issues/125
  message ValueAtQuantile {
    // The quantile of a distribution. Must be in the interval
    // [0.0, 1.0].
    double quantile = 1;

    // The value at the given quantile of a distribution.
    //
    // Quantile values must NOT be negative.
    double value = 2;
  }

  // (Optional) list of values at different quantiles of the distribution calculated
  // from the current snapshot. The quantiles must be strictly increasing.
  repeated ValueAtQuantile quantile_values = 6;

  // Flags that apply to this specific data point.  See DataPointFlags
  // for the available flags and their meaning.
  uint32 flags = 8;
}
```

Para obter mais informações, consulte [Conversões com o formato OpenTelemetry 1.0.0 no CloudWatch](CloudWatch-metric-streams-formats-opentelemetry-translation-100.md).

# Conversões com o formato OpenTelemetry 1.0.0 no CloudWatch
<a name="CloudWatch-metric-streams-formats-opentelemetry-translation-100"></a>

O CloudWatch executa algumas transformações para colocar os dados do CloudWatch no formato OpenTelemetry.

**Converter namespace, nome da métrica e dimensões**

Esses atributos são pares chave-valor codificados no mapeamento.
+ Um atributo tem a chave `Namespace` e seu valor é o namespace da métrica
+ Um atributo tem a chave `MetricName` e seu valor é o nome da métrica
+ Um par tem a chave `Dimensions` e seu valor é uma lista aninhada de pares de chave/valor. Cada par nessa lista é mapeado para uma dimensão de métrica do CloudWatch, em que a chave do par é o nome da dimensão e seu valor é o valor da dimensão.

**Converter Average, Sum, SampleCount, Min e Max**

O ponto de dados Summary permite que o CloudWatch exporte todas essas estatísticas usando um ponto de dados.
+ `startTimeUnixNano` contém o do CloudWatch `startTime`
+ `timeUnixNano` contém o do CloudWatch `endTime`
+ `sum` contém a estatística Sum.
+ `count` contém a estatística SampleCount.
+ `quantile_values` contém dois objetos `valueAtQuantile.value`:
  + `valueAtQuantile.quantile = 0.0` com `valueAtQuantile.value = Min value`
  + `valueAtQuantile.quantile = 0.99` com `valueAtQuantile.value = p99 value`
  + `valueAtQuantile.quantile = 0.999` com `valueAtQuantile.value = p99.9 value`
  + `valueAtQuantile.quantile = 1.0` com `valueAtQuantile.value = Max value`

Os recursos que consomem o fluxo de métrica podem calcular a estatística Average como **Sum/SampleCount**.

**Converter unidades**

As unidades do CloudWatch são mapeadas para a variante que diferencia maiúsculas de minúsculas do Código unificado para unidades de medida, conforme exibido na tabela a seguir. Para obter mais informações, consulte [Código unificado para unidades de medida](https://ucum.org/ucum.html).


| CloudWatch | OpenTelemetry | 
| --- | --- | 
|  Segunda |  s | 
|  Segundo ou segundos |  s | 
|  Microssegundos |  us | 
|  Milissegundos |  ms | 
|  Bytes |  By | 
|  Kilobytes |  kBy | 
|  Megabytes |  MBy | 
|  Gigabytes |  GBy | 
|  Terabytes |  TBy | 
|  Bits |  bit | 
|  Kilobits |  kbit | 
|  Megabits |  MBit | 
|  Gigabits |  Gbit | 
|  Terabits |  Tbit | 
|  Percentual |  % | 
|  Contagem |  \$1Count\$1 | 
|  Nenhum |  1 | 

As unidades que são combinadas com uma barra são mapeadas aplicando-se a conversão do OpenTelemetry de ambas as unidades. Por exemplo, bytes/segundo é mapeado para by/s.

# Como analisar mensagens do OpenTelemetry 1.0.0
<a name="CloudWatch-metric-streams-formats-opentelemetry-parse-100"></a>

Esta seção fornece informações para ajudar você a começar a analisar o OpenTelemetry 1.0.0.

Primeiro, é necessário obter associações específicas de idioma que permitem analisar mensagens do OpenTelemetry 1.0.0 no idioma de sua preferência.

**Para obter associações específicas de idioma**
+ As etapas dependem do idioma de sua preferência.
  + Para usar o Java, adicione a seguinte dependência Maven ao projeto Java: [OpenTelemetry Java >> 0.14.1](https://mvnrepository.com/artifact/io.opentelemetry/opentelemetry-proto/0.14.1).
  + Para utilizar qualquer outro idioma, siga estas etapas:

    1. Verifique se seu idioma é compatível, conferindo a lista em [Gerar suas classes](https://developers.google.com/protocol-buffers/docs/proto3#generating).

    1. Instale o compilador Protobuf seguindo as etapas em [Baixar buffers de protocolo](https://developers.google.com/protocol-buffers/docs/downloads).

    1. Faça o download das definições de buffers de protocolo (ProtoBuf) do OpenTelemetry 1.0.0 na página [Release version 1.0.0](https://github.com/open-telemetry/opentelemetry-proto/releases/tag/v1.0.0). 

    1. Confirme que você está na pasta raiz das definições de buffers de protocolo (ProtoBuf) do OpenTelemetry 1.0.0 baixadas. Depois, crie uma pasta `src` e execute o comando para gerar ligações específicas do idioma. Para obter mais informações, consulte [Gerar suas classes](https://developers.google.com/protocol-buffers/docs/proto3#generating). 

       Veja a seguir um exemplo de como gerar associações Javascript.

       ```
       protoc --proto_path=./ --js_out=import_style=commonjs,binary:src \
       opentelemetry/proto/common/v1/common.proto \
       opentelemetry/proto/resource/v1/resource.proto \
       opentelemetry/proto/metrics/v1/metrics.proto \
       opentelemetry/proto/collector/metrics/v1/metrics_service.proto
       ```

A seção a seguir contém exemplos de como usar as vinculações específicas de idioma que você pode criar usando as instruções anteriores.

**Java**

```
package com.example;

import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MyOpenTelemetryParser {

    public List<ExportMetricsServiceRequest> parse(InputStream inputStream) throws IOException {
        List<ExportMetricsServiceRequest> result = new ArrayList<>();

        ExportMetricsServiceRequest request;
        /* A Kinesis record can contain multiple `ExportMetricsServiceRequest`
           records, each of them starting with a header with an
           UnsignedVarInt32 indicating the record length in bytes:
            ------ --------------------------- ------ -----------------------
           |UINT32|ExportMetricsServiceRequest|UINT32|ExportMetricsService...
            ------ --------------------------- ------ -----------------------
         */
        while ((request = ExportMetricsServiceRequest.parseDelimitedFrom(inputStream)) != null) {
            // Do whatever we want with the parsed message
            result.add(request);
        }

        return result;
    }
}
```

**Javascript**

Este exemplo pressupõe que a pasta raiz com as ligações geradas seja `./`

O argumento de dados da função `parseRecord` pode ser de um destes tipos:
+ `Uint8Array` é o ideal
+ `Buffer` ideal no nó
+ `Array.number` inteiros de 8 bits

```
const pb = require('google-protobuf')
const pbMetrics =
    require('./opentelemetry/proto/collector/metrics/v1/metrics_service_pb')

function parseRecord(data) {
    const result = []

    // Loop until we've read all the data from the buffer
    while (data.length) {
        /* A Kinesis record can contain multiple `ExportMetricsServiceRequest`
           records, each of them starting with a header with an
           UnsignedVarInt32 indicating the record length in bytes:
            ------ --------------------------- ------ -----------------------
           |UINT32|ExportMetricsServiceRequest|UINT32|ExportMetricsService...
            ------ --------------------------- ------ -----------------------
         */
        const reader = new pb.BinaryReader(data)
        const messageLength = reader.decoder_.readUnsignedVarint32()
        const messageFrom = reader.decoder_.cursor_
        const messageTo = messageFrom + messageLength

        // Extract the current `ExportMetricsServiceRequest` message to parse
        const message = data.subarray(messageFrom, messageTo)

        // Parse the current message using the ProtoBuf library
        const parsed =
            pbMetrics.ExportMetricsServiceRequest.deserializeBinary(message)

        // Do whatever we want with the parsed message
        result.push(parsed.toObject())

        // Shrink the remaining buffer, removing the already parsed data
        data = data.subarray(messageTo)
    }

    return result
}
```

**Python**

É necessário ler os delimitadores `var-int` você mesmo ou usar os métodos internos `_VarintBytes(size)` e `_DecodeVarint32(buffer, position)`. Estes retornam a posição no buffer logo após os bytes de tamanho. O lado de leitura constrói um novo buffer que está limitado a ler apenas os bytes da mensagem. 

```
size = my_metric.ByteSize()
f.write(_VarintBytes(size))
f.write(my_metric.SerializeToString())
msg_len, new_pos = _DecodeVarint32(buf, 0)
msg_buf = buf[new_pos:new_pos+msg_len]
request = metrics_service_pb.ExportMetricsServiceRequest()
request.ParseFromString(msg_buf)
```

**Go**

Use `Buffer.DecodeMessage()`.

**C\$1**

Use `CodedInputStream`. Essa classe é capaz de ler mensagens delimitadas por tamanho.

**C\$1\$1**

As funções descritas em `google/protobuf/util/delimited_message_util.h` podem ler mensagens delimitadas por tamanho.

**Outras linguagens**

Para outros idiomas, consulte [Baixar buffers de protocolo](https://developers.google.com/protocol-buffers/docs/downloads).

Ao implementar o analisador, considere que um registro do Kinesis pode conter vários mensagens de buffers de protocolo `ExportMetricsServiceRequest`, todas delas começando com um cabeçalho contendo `UnsignedVarInt32`, que indica o tamanho do registro em bytes.