Repository: prometheus/prometheus
File: tsdb/head_append.go (Go)

Code before:
		}
	}

	switch {
	case h != nil:
		zeroHistogram := &histogram.Histogram{}
		s.Lock()

		// TODO(krajorama): reorganize Commit() to handle samples in append order
		// not floats first and then histograms. Then we would not need to do this.
		// This whole "if" should be removed.
		if created && s.lastHistogramValue == nil && s.lastFloatHistogramValue == nil {
			s.lastHistogramValue = zeroHistogram
		}

		// For CTZeroSamples OOO is not allowed.
		// We set it to true to make this implementation as close as possible to the float implementation.
		isOOO, _, err := s.appendableHistogram(ct, zeroHistogram, a.headMaxt, a.minValidTime, a.oooTimeWindow)
		if err != nil {
			if errors.Is(err, storage.ErrOutOfOrderSample) {
				s.Unlock()
				return 0, storage.ErrOutOfOrderCT
			}
		}

		// OOO is not allowed because after the first scrape, CT will be the same for most (if not all) future samples.
		// This is to prevent the injected zero from being marked as OOO forever.
		if isOOO {
			s.Unlock()
			return 0, storage.ErrOutOfOrderCT
		}

		s.pendingCommit = true
		s.Unlock()
		a.histograms = append(a.histograms, record.RefHistogramSample{
			Ref: s.ref,
			T:   ct,
			H:   zeroHistogram,
		})
		a.histogramSeries = append(a.histogramSeries, s)
	case fh != nil:
		zeroFloatHistogram := &histogram.FloatHistogram{}
		s.Lock()

		// TODO(krajorama): reorganize Commit() to handle samples in append order
		// not floats first and then histograms. Then we would not need to do this.
		// This whole "if" should be removed.
		if created && s.lastHistogramValue == nil && s.lastFloatHistogramValue == nil {
			s.lastFloatHistogramValue = zeroFloatHistogram
		}


Review comment (bug):
Maybe we should `return err` instead? I think that's what's missing. Otherwise the check failed but we proceed.

Same applies to the next block.


Code after:
		}
	}

	switch {
	case h != nil:
		zeroHistogram := &histogram.Histogram{}
		s.Lock()

		// TODO(krajorama): reorganize Commit() to handle samples in append order
		// not floats first and then histograms. Then we would not need to do this.
		// This whole "if" should be removed.
		if created && s.lastHistogramValue == nil && s.lastFloatHistogramValue == nil {
			s.lastHistogramValue = zeroHistogram
		}

		// For CTZeroSamples OOO is not allowed.
		// We set it to true to make this implementation as close as possible to the float implementation.
		isOOO, _, err := s.appendableHistogram(ct, zeroHistogram, a.headMaxt, a.minValidTime, a.oooTimeWindow)
		if err != nil {
			s.Unlock()
			if errors.Is(err, storage.ErrOutOfOrderSample) {
				return 0, storage.ErrOutOfOrderCT
			}

			return 0, err
		}

		// OOO is not allowed because after the first scrape, CT will be the same for most (if not all) future samples.
		// This is to prevent the injected zero from being marked as OOO forever.
		if isOOO {
			s.Unlock()
			return 0, storage.ErrOutOfOrderCT
		}

		s.pendingCommit = true
		s.Unlock()
		a.histograms = append(a.histograms, record.RefHistogramSample{
			Ref: s.ref,
			T:   ct,
			H:   zeroHistogram,
		})
		a.histogramSeries = append(a.histogramSeries, s)
	case fh != nil:
		zeroFloatHistogram := &histogram.FloatHistogram{}
		s.Lock()

		// TODO(krajorama): reorganize Commit() to handle samples in append order
		// not floats first and then histograms. Then we would not need to do this.
		// This whole "if" should be removed.
		if created && s.lastHistogramValue == nil && s.lastFloatHistogramValue == nil {
			s.lastFloatHistogramValue = zeroFloatHistogram