1// Copyright 2013 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package scrape
15
16import (
17	"github.com/prometheus/prometheus/pkg/labels"
18	"github.com/prometheus/prometheus/storage"
19)
20
21type nopAppendable struct{}
22
23func (a nopAppendable) Appender() (storage.Appender, error) {
24	return nopAppender{}, nil
25}
26
27type nopAppender struct{}
28
29func (a nopAppender) Add(labels.Labels, int64, float64) (uint64, error)   { return 0, nil }
30func (a nopAppender) AddFast(labels.Labels, uint64, int64, float64) error { return nil }
31func (a nopAppender) Commit() error                                       { return nil }
32func (a nopAppender) Rollback() error                                     { return nil }
33
34// collectResultAppender records all samples that were added through the appender.
35// It can be used as its zero value or be backed by another appender it writes samples through.
36type collectResultAppender struct {
37	next   storage.Appender
38	result []sample
39}
40
41func (a *collectResultAppender) AddFast(m labels.Labels, ref uint64, t int64, v float64) error {
42	if a.next == nil {
43		return storage.ErrNotFound
44	}
45	err := a.next.AddFast(m, ref, t, v)
46	if err != nil {
47		return err
48	}
49	a.result = append(a.result, sample{
50		metric: m,
51		t:      t,
52		v:      v,
53	})
54	return err
55}
56
57func (a *collectResultAppender) Add(m labels.Labels, t int64, v float64) (uint64, error) {
58	a.result = append(a.result, sample{
59		metric: m,
60		t:      t,
61		v:      v,
62	})
63	if a.next == nil {
64		return 0, nil
65	}
66	return a.next.Add(m, t, v)
67}
68
69func (a *collectResultAppender) Commit() error   { return nil }
70func (a *collectResultAppender) Rollback() error { return nil }
71