I'm using the following code to create 100 datapoints in tsdb from 0 till 99:
package main
import (
"context"
"fmt"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/tsdb"
"os"
"time"
)
func main() {
// Create a new TSDB instance
db, err := tsdb.Open(
"./data", // directory where the data will be stored
nil, // a logger (can be nil for no logging)
nil, // an optional prometheus.Registerer
tsdb.DefaultOptions(),
nil,
)
if err != nil {
fmt.Println("Error opening TSDB:", err)
os.Exit(1)
}
defer db.Close()
// Create a new appender
app := db.Appender(context.Background())
// Create labels for the gauge time series
lbls := labels.FromStrings("__name__", "example_gauge", "type", "gauge")
// Initialize a SeriesRef
var ref storage.SeriesRef
startTimestamp := time.Now().Add(-1 * time.Hour).Unix()
// Add some data points
for i := 0; i < 100; i++ {
var err error
ref, err = app.Append(ref, lbls, (startTimestamp+int64(i))*1000, float64(i))
if err != nil {
fmt.Println("Error appending:", err)
os.Exit(1)
}
}
// Commit the data
err = app.Commit()
if err != nil {
fmt.Println("Error committing:", err)
os.Exit(1)
}
}
It works fine except one thing, when I issue the following PromQL query: example_gauge{type="gauge"} I get 300 points in response, first 100 is from 0 till 99 like expected and last 200 points all have the same value 99, and my chart looks so:
Why it happens?

Big thanks to @markalex and his suggestions. He pointed me to the explanation of staleness here: https://prometheus.io/docs/prometheus/latest/querying/basics/#staleness and I was able to update my example, so now it generates data that I need:
Here I adding hole without data, i.e. marking timeserie as stale and then unmark it:
And here the code that does it:
I know that AI generated answers not allowed, but nevetherless I would like to attach ChatGPT explanation about staleness, explanation in Prometheus documentation sounds a bit vague up to me: