| 1 |
// Copyright 2024 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 |
|
| 14 |
package remote |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"errors" |
| 19 |
"io" |
| 20 |
"log/slog" |
| 21 |
"net/http" |
| 22 |
"net/http/httptest" |
| 23 |
"strings" |
| 24 |
"testing" |
| 25 |
"time" |
| 26 |
|
| 27 |
"github.com/google/go-cmp/cmp" |
| 28 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 29 |
"github.com/prometheus/common/model" |
| 30 |
"google.golang.org/protobuf/proto" |
| 31 |
"google.golang.org/protobuf/testing/protocmp" |
| 32 |
|
| 33 |
writev2 "github.com/prometheus/client_golang/exp/api/remote/genproto/v2" |
| 34 |
"github.com/prometheus/client_golang/exp/internal/github.com/efficientgo/core/backoff" |
| 35 |
) |
| 36 |
|
| 37 |
func TestRetryAfterDuration(t *testing.T) { |
| 38 |
tc := []struct { |
| 39 |
name string |
| 40 |
tInput string |
| 41 |
expected model.Duration |
| 42 |
}{ |
| 43 |
{ |
| 44 |
name: "seconds", |
| 45 |
tInput: "120", |
| 46 |
expected: model.Duration(time.Second * 120), |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "date-time default", |
| 50 |
tInput: time.RFC1123, // Expected layout is http.TimeFormat, hence an error. |
| 51 |
expected: 0, |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "retry-after not provided", |
| 55 |
tInput: "", // Expected layout is http.TimeFormat, hence an error. |
| 56 |
expected: 0, |
| 57 |
}, |
| 58 |
} |
| 59 |
for _, c := range tc { |
| 60 |
if got := retryAfterDuration(c.tInput); got != time.Duration(c.expected) { |
| 61 |
t.Fatal("expected", c.expected, "got", got) |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
type mockStorage struct { |
| 67 |
v2Reqs []*writev2.Request |
| 68 |
protos []WriteMessageType |
| 69 |
|
| 70 |
mockCode *int |
| 71 |
mockErr error |
| 72 |
} |
| 73 |
|
| 74 |
func (m *mockStorage) Store(_ context.Context, msgType WriteMessageType, req *http.Request) (*WriteResponse, error) { |
| 75 |
w := &WriteResponse{} |
| 76 |
if m.mockErr != nil { |
| 77 |
if m.mockCode != nil { |
| 78 |
w.SetStatusCode(*m.mockCode) |
| 79 |
} |
| 80 |
return w, m.mockErr |
| 81 |
} |
| 82 |
|
| 83 |
// Read the request body |
| 84 |
serializedRequest, err := io.ReadAll(req.Body) |
| 85 |
if err != nil { |
| 86 |
w.SetStatusCode(http.StatusBadRequest) |
| 87 |
return w, err |
| 88 |
} |
| 89 |
|
| 90 |
// This test expects v2 only |
| 91 |
r := &writev2.Request{} |
| 92 |
if err := proto.Unmarshal(serializedRequest, r); err != nil { |
| 93 |
w.SetStatusCode(http.StatusInternalServerError) |
| 94 |
return w, err |
| 95 |
} |
| 96 |
m.v2Reqs = append(m.v2Reqs, r) |
| 97 |
m.protos = append(m.protos, msgType) |
| 98 |
|
| 99 |
// Set stats in response headers |
| 100 |
w.Add(stats(r)) |
| 101 |
w.SetStatusCode(http.StatusNoContent) |
| 102 |
|
| 103 |
return w, nil |
| 104 |
} |
| 105 |
|
| 106 |
func testV2() *writev2.Request { |
| 107 |
s := writev2.NewSymbolTable() |
| 108 |
return &writev2.Request{ |
| 109 |
Timeseries: []*writev2.TimeSeries{ |
| 110 |
{ |
| 111 |
Metadata: &writev2.Metadata{ |
| 112 |
Type: writev2.Metadata_METRIC_TYPE_COUNTER, |
| 113 |
HelpRef: s.Symbolize("My lovely counter"), |
| 114 |
}, |
| 115 |
LabelsRefs: s.SymbolizeLabels([]string{"__name__", "metric1", "foo", "bar1"}, nil), |
| 116 |
Samples: []*writev2.Sample{ |
| 117 |
{Value: 1.1, Timestamp: 1214141}, |
| 118 |
{Value: 1.5, Timestamp: 1214180}, |
| 119 |
}, |
| 120 |
}, |
| 121 |
{ |
| 122 |
Metadata: &writev2.Metadata{ |
| 123 |
Type: writev2.Metadata_METRIC_TYPE_COUNTER, |
| 124 |
HelpRef: s.Symbolize("My lovely counter"), |
| 125 |
}, |
| 126 |
LabelsRefs: s.SymbolizeLabels([]string{"__name__", "metric1", "foo", "bar2"}, nil), |
| 127 |
Samples: []*writev2.Sample{ |
| 128 |
{Value: 1231311, Timestamp: 1214141}, |
| 129 |
{Value: 1310001, Timestamp: 1214180}, |
| 130 |
}, |
| 131 |
}, |
| 132 |
}, |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func stats(req *writev2.Request) (s WriteResponseStats) { |
| 137 |
s.confirmed = true |
| 138 |
for _, ts := range req.Timeseries { |
| 139 |
s.Samples += len(ts.Samples) |
| 140 |
s.Histograms += len(ts.Histograms) |
| 141 |
s.Exemplars += len(ts.Exemplars) |
| 142 |
} |
| 143 |
return s |
| 144 |
} |
| 145 |
|
| 146 |
func TestRemoteAPI_Write_WithHandler(t *testing.T) { |
| 147 |
t.Run("success", func(t *testing.T) { |
| 148 |
tLogger := slog.Default() |
| 149 |
mStore := &mockStorage{} |
| 150 |
srv := httptest.NewServer(NewHandler(mStore, MessageTypes{WriteV2MessageType}, WithHandlerLogger(tLogger))) |
| 151 |
t.Cleanup(srv.Close) |
| 152 |
|
| 153 |
client, err := NewAPI(srv.URL, |
| 154 |
WithAPIHTTPClient(srv.Client()), |
| 155 |
WithAPILogger(tLogger), |
| 156 |
WithAPIPath("api/v1/write"), |
| 157 |
) |
| 158 |
if err != nil { |
| 159 |
t.Fatal(err) |
| 160 |
} |
| 161 |
|
| 162 |
req := testV2() |
| 163 |
s, err := client.Write(context.Background(), WriteV2MessageType, req) |
| 164 |
if err != nil { |
| 165 |
t.Fatal(err) |
| 166 |
} |
| 167 |
if diff := cmp.Diff(stats(req), s, cmpopts.IgnoreUnexported(WriteResponseStats{})); diff != "" { |
| 168 |
t.Fatal("unexpected stats", diff) |
| 169 |
} |
| 170 |
if len(mStore.v2Reqs) != 1 { |
| 171 |
t.Fatal("expected 1 v2 request stored, got", mStore.v2Reqs) |
| 172 |
} |
| 173 |
if diff := cmp.Diff(req, mStore.v2Reqs[0], protocmp.Transform()); diff != "" { |
| 174 |
t.Fatal("unexpected request received", diff) |
| 175 |
} |
| 176 |
}) |
| 177 |
|
| 178 |
t.Run("storage error", func(t *testing.T) { |
| 179 |
tLogger := slog.Default() |
| 180 |
mockCode := http.StatusInternalServerError |
| 181 |
mStore := &mockStorage{ |
| 182 |
mockErr: errors.New("storage error"), |
| 183 |
mockCode: &mockCode, |
| 184 |
} |
| 185 |
srv := httptest.NewServer(NewHandler(mStore, MessageTypes{WriteV2MessageType}, WithHandlerLogger(tLogger))) |
| 186 |
t.Cleanup(srv.Close) |
| 187 |
|
| 188 |
client, err := NewAPI(srv.URL, |
| 189 |
WithAPIHTTPClient(srv.Client()), |
| 190 |
WithAPILogger(tLogger), |
| 191 |
WithAPIPath("api/v1/write"), |
| 192 |
WithAPIBackoff(backoff.Config{ |
| 193 |
Min: 1 * time.Second, |
| 194 |
Max: 1 * time.Second, |
| 195 |
MaxRetries: 2, |
| 196 |
}), |
| 197 |
) |
| 198 |
if err != nil { |
| 199 |
t.Fatal(err) |
| 200 |
} |
| 201 |
|
| 202 |
req := testV2() |
| 203 |
_, err = client.Write(context.Background(), WriteV2MessageType, req) |
| 204 |
if err == nil { |
| 205 |
t.Fatal("expected error, got nil") |
| 206 |
} |
| 207 |
if !strings.Contains(err.Error(), "storage error") { |
| 208 |
t.Fatalf("expected error to contain 'storage error', got %v", err) |
| 209 |
} |
| 210 |
}) |
| 211 |
} |