Skip to content

Commit 5bfdd73

Browse files
committed
src ingestion status is written into the same shard as metric
1 parent d716df5 commit 5bfdd73

2 files changed

Lines changed: 42 additions & 22 deletions

File tree

internal/agent/agent.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -760,29 +760,29 @@ func (s *Agent) ApplyMetric(m tlstatshouse.MetricBytes, h data_model.MappedMetri
760760
// Simply writing everything we know about metric ingestion errors would easily double how much metrics data we write
761761
// So below is basically a compromise. All info is stored in MappingMetricHeader, if needed we can easily write more
762762
// by changing code below
763-
//var shardId uint32
764-
//weightMul := 1
765-
//if h.MetricMeta != nil {
766-
// ingestion statuses for unknown metric (metric not found) go to the first shard.
767-
// for known metric, go to shard together with metric
768-
//shardId, weightMul = s.shard(&h.Key, h.MetricMeta, scratch)
769-
//}
770-
if h.IngestionStatus != 0 {
771-
// h.InvalidString was validated before mapping attempt.
772-
// In case of utf decoding error, it contains hex representation of original string
773-
s.AddCounterStringBytes(0, format.BuiltinMetricMetaIngestionStatus,
774-
[]int32{h.Key.Tags[0], h.Key.Metric, h.IngestionStatus, h.IngestionTagKey},
775-
h.InvalidString, 1)
776-
return
763+
var shardId uint32
764+
if h.MetricMeta != nil {
765+
// ingestion statuses for unknown metric (metric not found) go to the first shard.
766+
// for known metric with fixed shard, go to shard together with metric
767+
// for known metric with hash_by_tags strategy, go to random shard together with metric
768+
shardId = s.shard(&h.Key, h.MetricMeta, scratch)
777769
}
778-
shardId := s.shard(&h.Key, h.MetricMeta, scratch)
779770
if shardId >= uint32(len(s.Shards)) {
780-
s.AddCounter(0, format.BuiltinMetricMetaIngestionStatus,
771+
shard := s.Shards[0]
772+
shard.AddCounterHostSrcIngestionStatus(0, format.BuiltinMetricMetaIngestionStatus,
781773
[]int32{h.Key.Tags[0], h.Key.Metric, format.TagValueIDSrcIngestionStatusErrShardingFailed, 0},
782774
1)
783775
return
784776
}
785777
shard := s.Shards[shardId]
778+
if h.IngestionStatus != 0 {
779+
// h.InvalidString was validated before mapping attempt.
780+
// In case of utf decoding error, it contains hex representation of original string
781+
shard.AddCounterHostStringBytesSrcIngestionStatus(0, format.BuiltinMetricMetaIngestionStatus,
782+
[]int32{h.Key.Tags[0], h.Key.Metric, h.IngestionStatus, h.IngestionTagKey},
783+
h.InvalidString, 1)
784+
return
785+
}
786786
var resolutionHash uint64
787787
if h.MetricMeta.EffectiveResolution != 1 { // sharding by metric and need resolution hash
788788
var scr []byte
@@ -799,36 +799,36 @@ func (s *Agent) ApplyMetric(m tlstatshouse.MetricBytes, h data_model.MappedMetri
799799
s.TimingsApplyMetric.AddValueCounter(time.Since(start).Seconds(), 1)
800800
}()
801801
// now set ok status
802-
s.AddCounter(0, format.BuiltinMetricMetaIngestionStatus,
802+
shard.AddCounterHostSrcIngestionStatus(0, format.BuiltinMetricMetaIngestionStatus,
803803
[]int32{h.Key.Tags[0], h.Key.Metric, format.TagValueIDSrcIngestionStatusOKCached, h.IngestionTagKey},
804804
1)
805805
// now set all warnings
806806
if h.NotFoundTagName != nil { // this is correct, can be set, but empty
807807
// NotFoundTagName is validated when discovered
808808
// This is warning, so written independent of ingestion status
809-
s.AddCounterStringBytes(0, format.BuiltinMetricMetaIngestionStatus,
809+
shard.AddCounterHostStringBytesSrcIngestionStatus(0, format.BuiltinMetricMetaIngestionStatus,
810810
[]int32{h.Key.Tags[0], h.Key.Metric, format.TagValueIDSrcIngestionStatusWarnMapTagNameNotFound}, // tag ID not known
811811
h.NotFoundTagName, 1)
812812
}
813813
if h.FoundDraftTagName != nil { // this is correct, can be set, but empty
814814
// FoundDraftTagName is validated when discovered
815815
// This is warning, so written independent of ingestion status
816-
s.AddCounterStringBytes(0, format.BuiltinMetricMetaIngestionStatus,
816+
shard.AddCounterHostStringBytesSrcIngestionStatus(0, format.BuiltinMetricMetaIngestionStatus,
817817
[]int32{h.Key.Tags[0], h.Key.Metric, format.TagValueIDSrcIngestionStatusWarnMapTagNameFoundDraft}, // tag ID is known, but draft
818818
h.FoundDraftTagName, 1)
819819
}
820820
if h.TagSetTwiceKey != 0 {
821-
s.AddCounter(0, format.BuiltinMetricMetaIngestionStatus,
821+
shard.AddCounterHostSrcIngestionStatus(0, format.BuiltinMetricMetaIngestionStatus,
822822
[]int32{h.Key.Tags[0], h.Key.Metric, format.TagValueIDSrcIngestionStatusWarnMapTagSetTwice, h.TagSetTwiceKey},
823823
1)
824824
}
825825
if h.InvalidRawTagKey != 0 {
826-
s.AddCounterStringBytes(0, format.BuiltinMetricMetaIngestionStatus,
826+
shard.AddCounterHostStringBytesSrcIngestionStatus(0, format.BuiltinMetricMetaIngestionStatus,
827827
[]int32{h.Key.Tags[0], h.Key.Metric, format.TagValueIDSrcIngestionStatusWarnMapInvalidRawTagValue, h.InvalidRawTagKey},
828828
h.InvalidRawValue, 1)
829829
}
830830
if h.LegacyCanonicalTagKey != 0 {
831-
s.AddCounter(0, format.BuiltinMetricMetaIngestionStatus,
831+
shard.AddCounterHostSrcIngestionStatus(0, format.BuiltinMetricMetaIngestionStatus,
832832
[]int32{h.Key.Tags[0], h.Key.Metric, format.TagValueIDSrcIngestionStatusWarnDeprecatedKeyName, h.LegacyCanonicalTagKey},
833833
1)
834834
}

internal/agent/agent_shard.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ func (s *Shard) ApplyCounter(key *data_model.Key, resolutionHash uint64, topValu
205205
mv.AddCounterHost(s.rng, count, hostTag)
206206
}
207207

208+
func (s *Shard) AddCounterHostSrcIngestionStatus(t uint32, metricInfo *format.MetricMetaValue, tags []int32, count float64) {
209+
if count <= 0 {
210+
return
211+
}
212+
key := data_model.Key{Timestamp: t, Metric: metricInfo.MetricID} // panics if metricInfo nil
213+
copy(key.Tags[:], tags)
214+
// resolutionHash will be 0 for built-in metrics, we are OK with this
215+
s.AddCounterHost(&key, 0, count, data_model.TagUnionBytes{}, metricInfo)
216+
}
217+
208218
func (s *Shard) AddCounterHost(key *data_model.Key, resolutionHash uint64, count float64, hostTag data_model.TagUnionBytes, metricInfo *format.MetricMetaValue) {
209219
s.mu.Lock()
210220
defer s.mu.Unlock()
@@ -216,6 +226,16 @@ func (s *Shard) AddCounterHost(key *data_model.Key, resolutionHash uint64, count
216226
item.Tail.AddCounterHost(s.rng, count, hostTag)
217227
}
218228

229+
func (s *Shard) AddCounterHostStringBytesSrcIngestionStatus(t uint32, metricInfo *format.MetricMetaValue, tags []int32, str []byte, count float64) {
230+
if count <= 0 {
231+
return
232+
}
233+
key := data_model.Key{Timestamp: t, Metric: metricInfo.MetricID} // panics if metricInfo nil
234+
copy(key.Tags[:], tags)
235+
// resolutionHash will be 0 for built-in metrics, we are OK with this
236+
s.AddCounterHostStringBytes(&key, 0, data_model.TagUnionBytes{S: str, I: 0}, count, data_model.TagUnionBytes{}, metricInfo)
237+
}
238+
219239
func (s *Shard) AddCounterHostStringBytes(key *data_model.Key, resolutionHash uint64, topValue data_model.TagUnionBytes, count float64, hostTag data_model.TagUnionBytes, metricInfo *format.MetricMetaValue) {
220240
s.mu.Lock()
221241
defer s.mu.Unlock()

0 commit comments

Comments
 (0)