-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
270 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package agent | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"time" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
) | ||
|
||
type otlpMetrics struct { | ||
metrics pmetric.Metrics | ||
ms pmetric.Sum | ||
} | ||
|
||
func newOTLPMetrics() *otlpMetrics { | ||
metrics := pmetric.NewMetrics() | ||
rm := metrics.ResourceMetrics().AppendEmpty() | ||
resourceAttrs := rm.Resource().Attributes() | ||
resourceAttrs.PutStr("service.name", "-service") | ||
resourceAttrs.PutStr("service.version", "1.0.0") | ||
resourceAttrs.PutStr("host.name", "example-host") | ||
sm := rm.ScopeMetrics().AppendEmpty() | ||
ms := sm.Metrics().AppendEmpty() | ||
ms.SetName("bytes_received") | ||
sum := ms.SetEmptySum() | ||
sum.SetAggregationTemporality(pmetric.AggregationTemporalityDelta) | ||
return &otlpMetrics{ | ||
metrics: metrics, | ||
ms: sum, | ||
} | ||
} | ||
|
||
func (om *otlpMetrics) addOTLPSum(timestamp time.Time, value float64, signal usageSignal) error { | ||
intVal, err := convertFloat64ToInt64(value) | ||
if err != nil { | ||
return err | ||
} | ||
d := om.ms.DataPoints().AppendEmpty() | ||
d.SetTimestamp(pcommon.NewTimestampFromTime(timestamp)) | ||
d.SetIntValue(intVal) | ||
d.Attributes().PutStr("signal", string(signal)) | ||
return nil | ||
} | ||
|
||
func OTLPMetrics(name string, signal usageSignal, value float64) (pmetric.ResourceMetrics, error) { | ||
intVal, err := convertFloat64ToInt64(value) | ||
if err != nil { | ||
return pmetric.ResourceMetrics{}, err | ||
} | ||
metrics := pmetric.NewResourceMetrics() | ||
|
||
// Set resource attributes | ||
resourceAttrs := metrics.Resource().Attributes() | ||
resourceAttrs.PutStr("service.name", "-service") | ||
resourceAttrs.PutStr("service.version", "1.0.0") | ||
resourceAttrs.PutStr("host.name", "example-host") | ||
|
||
sm := metrics.ScopeMetrics().AppendEmpty() | ||
m := sm.Metrics().AppendEmpty() | ||
m.SetName("bytes_received") | ||
s := m.SetEmptySum() | ||
s.SetAggregationTemporality(pmetric.AggregationTemporalityDelta) | ||
d := s.DataPoints().AppendEmpty() | ||
d.SetTimestamp(pcommon.NewTimestampFromTime(time.Now())) | ||
d.SetIntValue(intVal) | ||
d.Attributes().PutStr("signal", string(signal)) | ||
|
||
return metrics, nil | ||
} | ||
|
||
func convertFloat64ToInt64(value float64) (int64, error) { | ||
if value > math.MaxInt64 { | ||
return 0, fmt.Errorf("value %f is too large to convert to int64", value) | ||
} | ||
if value < math.MinInt64 { | ||
return 0, fmt.Errorf("value %f is too small to convert to int64", value) | ||
} | ||
return int64(value), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package agent | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
"time" | ||
|
||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
) | ||
|
||
var errNoData = errors.New("no data to report") | ||
|
||
type usageStore struct { | ||
lastUsageData totalUsage | ||
|
||
mut sync.Mutex | ||
datapoints []usage | ||
} | ||
|
||
func newUsageStore() *usageStore { | ||
return &usageStore{ | ||
lastUsageData: make(totalUsage), | ||
datapoints: make([]usage, 0), | ||
} | ||
} | ||
|
||
func (ur *usageStore) Add(traceUsage, logUsage float64) { | ||
ur.mut.Lock() | ||
defer ur.mut.Unlock() | ||
|
||
if traceUsage != 0 { | ||
deltaTraceUsage := traceUsage - ur.lastUsageData[signal_trace].val | ||
ur.datapoints = append(ur.datapoints, usage{signal: signal_trace, val: deltaTraceUsage, timestamp: time.Now()}) | ||
ur.lastUsageData[signal_trace] = usage{signal: signal_trace, val: traceUsage} | ||
} | ||
if logUsage != 0 { | ||
deltaLogUsage := logUsage - ur.lastUsageData[signal_log].val | ||
ur.datapoints = append(ur.datapoints, usage{signal: signal_log, val: deltaLogUsage, timestamp: time.Now()}) | ||
ur.lastUsageData[signal_log] = usage{signal: signal_log, val: logUsage} | ||
} | ||
|
||
} | ||
|
||
func (ur *usageStore) NewReport() ([]byte, error) { | ||
ur.mut.Lock() | ||
defer ur.mut.Unlock() | ||
|
||
if len(ur.datapoints) == 0 { | ||
return nil, errNoData | ||
} | ||
|
||
otlpMetrics := newOTLPMetrics() | ||
for _, usage := range ur.datapoints { | ||
otlpMetrics.addOTLPSum(usage.timestamp, usage.val, usage.signal) | ||
} | ||
|
||
jsonMarshaler := &pmetric.JSONMarshaler{} | ||
data, err := jsonMarshaler.MarshalMetrics(otlpMetrics.metrics) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ur.datapoints = ur.datapoints[:0] | ||
return data, nil | ||
} | ||
|
||
type usageSignal string | ||
|
||
var ( | ||
signal_trace usageSignal = "trace" | ||
signal_log usageSignal = "log" | ||
) | ||
|
||
type totalUsage map[usageSignal]usage | ||
|
||
type usage struct { | ||
signal usageSignal | ||
val float64 | ||
timestamp time.Time | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters