Skip to content

Instantly share code, notes, and snippets.

@axw
Created October 14, 2021 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axw/04f6e31b11186ca5c6c2b7a9dfe15a72 to your computer and use it in GitHub Desktop.
Save axw/04f6e31b11186ca5c6c2b7a9dfe15a72 to your computer and use it in GitHub Desktop.
modelindexer data generator
package main
import (
"context"
"log"
"time"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/apm-server/elasticsearch"
"github.com/elastic/apm-server/model"
"github.com/elastic/apm-server/model/modelindexer"
)
func Main() error {
logp.DevelopmentSetup()
cfg := elasticsearch.DefaultConfig()
cfg.Username = "admin"
cfg.Password = "changeme"
client, err := elasticsearch.NewClient(cfg)
if err != nil {
return err
}
indexer, err := modelindexer.New(client, modelindexer.Config{})
if err != nil {
return err
}
var docCount int64
var histogram model.Histogram
for i := 0; i < 1000; i++ {
value := float64(i)
count := int64(i + 1)
docCount += count
histogram.Counts = append(histogram.Counts, count)
histogram.Values = append(histogram.Values, value)
}
batch := model.Batch{{
Processor: model.TransactionProcessor,
DataStream: model.DataStream{Type: "traces", Dataset: model.TracesDataset, Namespace: "default"},
Event: model.Event{Duration: time.Second},
Transaction: &model.Transaction{},
}, {
Processor: model.MetricsetProcessor,
DataStream: model.DataStream{Type: "metrics", Dataset: model.InternalMetricsDataset, Namespace: "default"},
Transaction: &model.Transaction{DurationHistogram: histogram},
Metricset: &model.Metricset{DocCount: docCount},
}}
for i := 0; i < 10000; i++ {
if err := indexer.ProcessBatch(context.Background(), &batch); err != nil {
return err
}
}
return indexer.Close(context.Background())
}
func main() {
if err := Main(); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment