components/otelopscol/receiver/nvmlreceiver/internal/metadata/generated_metrics.go (352 lines of code) (raw):
// Code generated by mdatagen. DO NOT EDIT.
package metadata
import (
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
)
// AttributeMemoryState specifies the value memory_state attribute.
type AttributeMemoryState int
const (
_ AttributeMemoryState = iota
AttributeMemoryStateUsed
AttributeMemoryStateFree
)
// String returns the string representation of the AttributeMemoryState.
func (av AttributeMemoryState) String() string {
switch av {
case AttributeMemoryStateUsed:
return "used"
case AttributeMemoryStateFree:
return "free"
}
return ""
}
// MapAttributeMemoryState is a helper map of string to AttributeMemoryState attribute value.
var MapAttributeMemoryState = map[string]AttributeMemoryState{
"used": AttributeMemoryStateUsed,
"free": AttributeMemoryStateFree,
}
var MetricsInfo = metricsInfo{
NvmlGpuMemoryBytesUsed: metricInfo{
Name: "nvml.gpu.memory.bytes_used",
},
NvmlGpuProcessesMaxBytesUsed: metricInfo{
Name: "nvml.gpu.processes.max_bytes_used",
},
NvmlGpuProcessesUtilization: metricInfo{
Name: "nvml.gpu.processes.utilization",
},
NvmlGpuUtilization: metricInfo{
Name: "nvml.gpu.utilization",
},
}
type metricsInfo struct {
NvmlGpuMemoryBytesUsed metricInfo
NvmlGpuProcessesMaxBytesUsed metricInfo
NvmlGpuProcessesUtilization metricInfo
NvmlGpuUtilization metricInfo
}
type metricInfo struct {
Name string
}
type metricNvmlGpuMemoryBytesUsed struct {
data pmetric.Metric // data buffer for generated metric.
config MetricConfig // metric config provided by user.
capacity int // max observed number of data points added to the metric.
}
// init fills nvml.gpu.memory.bytes_used metric with initial data.
func (m *metricNvmlGpuMemoryBytesUsed) init() {
m.data.SetName("nvml.gpu.memory.bytes_used")
m.data.SetDescription("Current number of GPU memory bytes used by state. Summing the values of all states yields the total GPU memory space.")
m.data.SetUnit("By")
m.data.SetEmptyGauge()
m.data.Gauge().DataPoints().EnsureCapacity(m.capacity)
}
func (m *metricNvmlGpuMemoryBytesUsed) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, modelAttributeValue string, gpuNumberAttributeValue string, uuidAttributeValue string, memoryStateAttributeValue string) {
if !m.config.Enabled {
return
}
dp := m.data.Gauge().DataPoints().AppendEmpty()
dp.SetStartTimestamp(start)
dp.SetTimestamp(ts)
dp.SetIntValue(val)
dp.Attributes().PutStr("model", modelAttributeValue)
dp.Attributes().PutStr("gpu_number", gpuNumberAttributeValue)
dp.Attributes().PutStr("uuid", uuidAttributeValue)
dp.Attributes().PutStr("memory_state", memoryStateAttributeValue)
}
// updateCapacity saves max length of data point slices that will be used for the slice capacity.
func (m *metricNvmlGpuMemoryBytesUsed) updateCapacity() {
if m.data.Gauge().DataPoints().Len() > m.capacity {
m.capacity = m.data.Gauge().DataPoints().Len()
}
}
// emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points.
func (m *metricNvmlGpuMemoryBytesUsed) emit(metrics pmetric.MetricSlice) {
if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 {
m.updateCapacity()
m.data.MoveTo(metrics.AppendEmpty())
m.init()
}
}
func newMetricNvmlGpuMemoryBytesUsed(cfg MetricConfig) metricNvmlGpuMemoryBytesUsed {
m := metricNvmlGpuMemoryBytesUsed{config: cfg}
if cfg.Enabled {
m.data = pmetric.NewMetric()
m.init()
}
return m
}
type metricNvmlGpuProcessesMaxBytesUsed struct {
data pmetric.Metric // data buffer for generated metric.
config MetricConfig // metric config provided by user.
capacity int // max observed number of data points added to the metric.
}
// init fills nvml.gpu.processes.max_bytes_used metric with initial data.
func (m *metricNvmlGpuProcessesMaxBytesUsed) init() {
m.data.SetName("nvml.gpu.processes.max_bytes_used")
m.data.SetDescription("Maximum total GPU memory in bytes that was ever allocated by the process.")
m.data.SetUnit("By")
m.data.SetEmptyGauge()
m.data.Gauge().DataPoints().EnsureCapacity(m.capacity)
}
func (m *metricNvmlGpuProcessesMaxBytesUsed) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, modelAttributeValue string, gpuNumberAttributeValue string, uuidAttributeValue string, pidAttributeValue int64, processAttributeValue string, commandAttributeValue string, commandLineAttributeValue string, ownerAttributeValue string) {
if !m.config.Enabled {
return
}
dp := m.data.Gauge().DataPoints().AppendEmpty()
dp.SetStartTimestamp(start)
dp.SetTimestamp(ts)
dp.SetIntValue(val)
dp.Attributes().PutStr("model", modelAttributeValue)
dp.Attributes().PutStr("gpu_number", gpuNumberAttributeValue)
dp.Attributes().PutStr("uuid", uuidAttributeValue)
dp.Attributes().PutInt("pid", pidAttributeValue)
dp.Attributes().PutStr("process", processAttributeValue)
dp.Attributes().PutStr("command", commandAttributeValue)
dp.Attributes().PutStr("command_line", commandLineAttributeValue)
dp.Attributes().PutStr("owner", ownerAttributeValue)
}
// updateCapacity saves max length of data point slices that will be used for the slice capacity.
func (m *metricNvmlGpuProcessesMaxBytesUsed) updateCapacity() {
if m.data.Gauge().DataPoints().Len() > m.capacity {
m.capacity = m.data.Gauge().DataPoints().Len()
}
}
// emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points.
func (m *metricNvmlGpuProcessesMaxBytesUsed) emit(metrics pmetric.MetricSlice) {
if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 {
m.updateCapacity()
m.data.MoveTo(metrics.AppendEmpty())
m.init()
}
}
func newMetricNvmlGpuProcessesMaxBytesUsed(cfg MetricConfig) metricNvmlGpuProcessesMaxBytesUsed {
m := metricNvmlGpuProcessesMaxBytesUsed{config: cfg}
if cfg.Enabled {
m.data = pmetric.NewMetric()
m.init()
}
return m
}
type metricNvmlGpuProcessesUtilization struct {
data pmetric.Metric // data buffer for generated metric.
config MetricConfig // metric config provided by user.
capacity int // max observed number of data points added to the metric.
}
// init fills nvml.gpu.processes.utilization metric with initial data.
func (m *metricNvmlGpuProcessesUtilization) init() {
m.data.SetName("nvml.gpu.processes.utilization")
m.data.SetDescription("Fraction of time over the process's life thus far during which one or more kernels was executing on the GPU.")
m.data.SetUnit("1")
m.data.SetEmptyGauge()
m.data.Gauge().DataPoints().EnsureCapacity(m.capacity)
}
func (m *metricNvmlGpuProcessesUtilization) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val float64, modelAttributeValue string, gpuNumberAttributeValue string, uuidAttributeValue string, pidAttributeValue int64, processAttributeValue string, commandAttributeValue string, commandLineAttributeValue string, ownerAttributeValue string) {
if !m.config.Enabled {
return
}
dp := m.data.Gauge().DataPoints().AppendEmpty()
dp.SetStartTimestamp(start)
dp.SetTimestamp(ts)
dp.SetDoubleValue(val)
dp.Attributes().PutStr("model", modelAttributeValue)
dp.Attributes().PutStr("gpu_number", gpuNumberAttributeValue)
dp.Attributes().PutStr("uuid", uuidAttributeValue)
dp.Attributes().PutInt("pid", pidAttributeValue)
dp.Attributes().PutStr("process", processAttributeValue)
dp.Attributes().PutStr("command", commandAttributeValue)
dp.Attributes().PutStr("command_line", commandLineAttributeValue)
dp.Attributes().PutStr("owner", ownerAttributeValue)
}
// updateCapacity saves max length of data point slices that will be used for the slice capacity.
func (m *metricNvmlGpuProcessesUtilization) updateCapacity() {
if m.data.Gauge().DataPoints().Len() > m.capacity {
m.capacity = m.data.Gauge().DataPoints().Len()
}
}
// emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points.
func (m *metricNvmlGpuProcessesUtilization) emit(metrics pmetric.MetricSlice) {
if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 {
m.updateCapacity()
m.data.MoveTo(metrics.AppendEmpty())
m.init()
}
}
func newMetricNvmlGpuProcessesUtilization(cfg MetricConfig) metricNvmlGpuProcessesUtilization {
m := metricNvmlGpuProcessesUtilization{config: cfg}
if cfg.Enabled {
m.data = pmetric.NewMetric()
m.init()
}
return m
}
type metricNvmlGpuUtilization struct {
data pmetric.Metric // data buffer for generated metric.
config MetricConfig // metric config provided by user.
capacity int // max observed number of data points added to the metric.
}
// init fills nvml.gpu.utilization metric with initial data.
func (m *metricNvmlGpuUtilization) init() {
m.data.SetName("nvml.gpu.utilization")
m.data.SetDescription("Fraction of time GPU was not idle since the last sample.")
m.data.SetUnit("1")
m.data.SetEmptyGauge()
m.data.Gauge().DataPoints().EnsureCapacity(m.capacity)
}
func (m *metricNvmlGpuUtilization) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val float64, modelAttributeValue string, gpuNumberAttributeValue string, uuidAttributeValue string) {
if !m.config.Enabled {
return
}
dp := m.data.Gauge().DataPoints().AppendEmpty()
dp.SetStartTimestamp(start)
dp.SetTimestamp(ts)
dp.SetDoubleValue(val)
dp.Attributes().PutStr("model", modelAttributeValue)
dp.Attributes().PutStr("gpu_number", gpuNumberAttributeValue)
dp.Attributes().PutStr("uuid", uuidAttributeValue)
}
// updateCapacity saves max length of data point slices that will be used for the slice capacity.
func (m *metricNvmlGpuUtilization) updateCapacity() {
if m.data.Gauge().DataPoints().Len() > m.capacity {
m.capacity = m.data.Gauge().DataPoints().Len()
}
}
// emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points.
func (m *metricNvmlGpuUtilization) emit(metrics pmetric.MetricSlice) {
if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 {
m.updateCapacity()
m.data.MoveTo(metrics.AppendEmpty())
m.init()
}
}
func newMetricNvmlGpuUtilization(cfg MetricConfig) metricNvmlGpuUtilization {
m := metricNvmlGpuUtilization{config: cfg}
if cfg.Enabled {
m.data = pmetric.NewMetric()
m.init()
}
return m
}
// MetricsBuilder provides an interface for scrapers to report metrics while taking care of all the transformations
// required to produce metric representation defined in metadata and user config.
type MetricsBuilder struct {
config MetricsBuilderConfig // config of the metrics builder.
startTime pcommon.Timestamp // start time that will be applied to all recorded data points.
metricsCapacity int // maximum observed number of metrics per resource.
metricsBuffer pmetric.Metrics // accumulates metrics data before emitting.
buildInfo component.BuildInfo // contains version information.
metricNvmlGpuMemoryBytesUsed metricNvmlGpuMemoryBytesUsed
metricNvmlGpuProcessesMaxBytesUsed metricNvmlGpuProcessesMaxBytesUsed
metricNvmlGpuProcessesUtilization metricNvmlGpuProcessesUtilization
metricNvmlGpuUtilization metricNvmlGpuUtilization
}
// MetricBuilderOption applies changes to default metrics builder.
type MetricBuilderOption interface {
apply(*MetricsBuilder)
}
type metricBuilderOptionFunc func(mb *MetricsBuilder)
func (mbof metricBuilderOptionFunc) apply(mb *MetricsBuilder) {
mbof(mb)
}
// WithStartTime sets startTime on the metrics builder.
func WithStartTime(startTime pcommon.Timestamp) MetricBuilderOption {
return metricBuilderOptionFunc(func(mb *MetricsBuilder) {
mb.startTime = startTime
})
}
func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.Settings, options ...MetricBuilderOption) *MetricsBuilder {
mb := &MetricsBuilder{
config: mbc,
startTime: pcommon.NewTimestampFromTime(time.Now()),
metricsBuffer: pmetric.NewMetrics(),
buildInfo: settings.BuildInfo,
metricNvmlGpuMemoryBytesUsed: newMetricNvmlGpuMemoryBytesUsed(mbc.Metrics.NvmlGpuMemoryBytesUsed),
metricNvmlGpuProcessesMaxBytesUsed: newMetricNvmlGpuProcessesMaxBytesUsed(mbc.Metrics.NvmlGpuProcessesMaxBytesUsed),
metricNvmlGpuProcessesUtilization: newMetricNvmlGpuProcessesUtilization(mbc.Metrics.NvmlGpuProcessesUtilization),
metricNvmlGpuUtilization: newMetricNvmlGpuUtilization(mbc.Metrics.NvmlGpuUtilization),
}
for _, op := range options {
op.apply(mb)
}
return mb
}
// updateCapacity updates max length of metrics and resource attributes that will be used for the slice capacity.
func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) {
if mb.metricsCapacity < rm.ScopeMetrics().At(0).Metrics().Len() {
mb.metricsCapacity = rm.ScopeMetrics().At(0).Metrics().Len()
}
}
// ResourceMetricsOption applies changes to provided resource metrics.
type ResourceMetricsOption interface {
apply(pmetric.ResourceMetrics)
}
type resourceMetricsOptionFunc func(pmetric.ResourceMetrics)
func (rmof resourceMetricsOptionFunc) apply(rm pmetric.ResourceMetrics) {
rmof(rm)
}
// WithResource sets the provided resource on the emitted ResourceMetrics.
// It's recommended to use ResourceBuilder to create the resource.
func WithResource(res pcommon.Resource) ResourceMetricsOption {
return resourceMetricsOptionFunc(func(rm pmetric.ResourceMetrics) {
res.CopyTo(rm.Resource())
})
}
// WithStartTimeOverride overrides start time for all the resource metrics data points.
// This option should be only used if different start time has to be set on metrics coming from different resources.
func WithStartTimeOverride(start pcommon.Timestamp) ResourceMetricsOption {
return resourceMetricsOptionFunc(func(rm pmetric.ResourceMetrics) {
var dps pmetric.NumberDataPointSlice
metrics := rm.ScopeMetrics().At(0).Metrics()
for i := 0; i < metrics.Len(); i++ {
switch metrics.At(i).Type() {
case pmetric.MetricTypeGauge:
dps = metrics.At(i).Gauge().DataPoints()
case pmetric.MetricTypeSum:
dps = metrics.At(i).Sum().DataPoints()
}
for j := 0; j < dps.Len(); j++ {
dps.At(j).SetStartTimestamp(start)
}
}
})
}
// EmitForResource saves all the generated metrics under a new resource and updates the internal state to be ready for
// recording another set of data points as part of another resource. This function can be helpful when one scraper
// needs to emit metrics from several resources. Otherwise calling this function is not required,
// just `Emit` function can be called instead.
// Resource attributes should be provided as ResourceMetricsOption arguments.
func (mb *MetricsBuilder) EmitForResource(options ...ResourceMetricsOption) {
rm := pmetric.NewResourceMetrics()
ils := rm.ScopeMetrics().AppendEmpty()
ils.Scope().SetName(ScopeName)
ils.Scope().SetVersion(mb.buildInfo.Version)
ils.Metrics().EnsureCapacity(mb.metricsCapacity)
mb.metricNvmlGpuMemoryBytesUsed.emit(ils.Metrics())
mb.metricNvmlGpuProcessesMaxBytesUsed.emit(ils.Metrics())
mb.metricNvmlGpuProcessesUtilization.emit(ils.Metrics())
mb.metricNvmlGpuUtilization.emit(ils.Metrics())
for _, op := range options {
op.apply(rm)
}
if ils.Metrics().Len() > 0 {
mb.updateCapacity(rm)
rm.MoveTo(mb.metricsBuffer.ResourceMetrics().AppendEmpty())
}
}
// Emit returns all the metrics accumulated by the metrics builder and updates the internal state to be ready for
// recording another set of metrics. This function will be responsible for applying all the transformations required to
// produce metric representation defined in metadata and user config, e.g. delta or cumulative.
func (mb *MetricsBuilder) Emit(options ...ResourceMetricsOption) pmetric.Metrics {
mb.EmitForResource(options...)
metrics := mb.metricsBuffer
mb.metricsBuffer = pmetric.NewMetrics()
return metrics
}
// RecordNvmlGpuMemoryBytesUsedDataPoint adds a data point to nvml.gpu.memory.bytes_used metric.
func (mb *MetricsBuilder) RecordNvmlGpuMemoryBytesUsedDataPoint(ts pcommon.Timestamp, val int64, modelAttributeValue string, gpuNumberAttributeValue string, uuidAttributeValue string, memoryStateAttributeValue AttributeMemoryState) {
mb.metricNvmlGpuMemoryBytesUsed.recordDataPoint(mb.startTime, ts, val, modelAttributeValue, gpuNumberAttributeValue, uuidAttributeValue, memoryStateAttributeValue.String())
}
// RecordNvmlGpuProcessesMaxBytesUsedDataPoint adds a data point to nvml.gpu.processes.max_bytes_used metric.
func (mb *MetricsBuilder) RecordNvmlGpuProcessesMaxBytesUsedDataPoint(ts pcommon.Timestamp, val int64, modelAttributeValue string, gpuNumberAttributeValue string, uuidAttributeValue string, pidAttributeValue int64, processAttributeValue string, commandAttributeValue string, commandLineAttributeValue string, ownerAttributeValue string) {
mb.metricNvmlGpuProcessesMaxBytesUsed.recordDataPoint(mb.startTime, ts, val, modelAttributeValue, gpuNumberAttributeValue, uuidAttributeValue, pidAttributeValue, processAttributeValue, commandAttributeValue, commandLineAttributeValue, ownerAttributeValue)
}
// RecordNvmlGpuProcessesUtilizationDataPoint adds a data point to nvml.gpu.processes.utilization metric.
func (mb *MetricsBuilder) RecordNvmlGpuProcessesUtilizationDataPoint(ts pcommon.Timestamp, val float64, modelAttributeValue string, gpuNumberAttributeValue string, uuidAttributeValue string, pidAttributeValue int64, processAttributeValue string, commandAttributeValue string, commandLineAttributeValue string, ownerAttributeValue string) {
mb.metricNvmlGpuProcessesUtilization.recordDataPoint(mb.startTime, ts, val, modelAttributeValue, gpuNumberAttributeValue, uuidAttributeValue, pidAttributeValue, processAttributeValue, commandAttributeValue, commandLineAttributeValue, ownerAttributeValue)
}
// RecordNvmlGpuUtilizationDataPoint adds a data point to nvml.gpu.utilization metric.
func (mb *MetricsBuilder) RecordNvmlGpuUtilizationDataPoint(ts pcommon.Timestamp, val float64, modelAttributeValue string, gpuNumberAttributeValue string, uuidAttributeValue string) {
mb.metricNvmlGpuUtilization.recordDataPoint(mb.startTime, ts, val, modelAttributeValue, gpuNumberAttributeValue, uuidAttributeValue)
}
// Reset resets metrics builder to its initial state. It should be used when external metrics source is restarted,
// and metrics builder should update its startTime and reset it's internal state accordingly.
func (mb *MetricsBuilder) Reset(options ...MetricBuilderOption) {
mb.startTime = pcommon.NewTimestampFromTime(time.Now())
for _, op := range options {
op.apply(mb)
}
}