Skip to content

Instantly share code, notes, and snippets.

@jeancarlosgarcia
Last active May 12, 2023 20:49
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 jeancarlosgarcia/50114160c82515f4c4906b50316704c0 to your computer and use it in GitHub Desktop.
Save jeancarlosgarcia/50114160c82515f4c4906b50316704c0 to your computer and use it in GitHub Desktop.
Guardar histogramas en bigtable
package main
import (
"cloud.google.com/go/bigtable"
"context"
"encoding/binary"
"fmt"
"google.golang.org/api/option"
"google.golang.org/grpc"
"time"
)
type LatencyHistogram struct {
Minute time.Time
Histogram map[int64]int64
}
func CreateSchema(projectID, instanceID, tableID string) error {
ctx := context.Background()
adminClient, err := bigtable.NewAdminClient(ctx, projectID, instanceID, option.WithEndpoint("localhost:8086"), option.WithoutAuthentication(), option.WithGRPCDialOption(grpc.WithInsecure()))
if err != nil {
return err
}
defer adminClient.Close()
err = adminClient.CreateTable(ctx, tableID)
if err != nil {
return err
}
err = adminClient.CreateColumnFamily(ctx, tableID, "latency_histogram")
if err != nil {
return err
}
fmt.Printf("Esquema creado exitosamente. Tabla: %s\n", tableID)
return nil
}
func SaveLatencyHistogram(projectID, instanceID, tableID string, histogram *LatencyHistogram) error {
ctx := context.Background()
client, err := bigtable.NewClient(ctx, projectID, instanceID, option.WithEndpoint("localhost:8086"), option.WithoutAuthentication(), option.WithGRPCDialOption(grpc.WithInsecure()))
if err != nil {
return err
}
defer client.Close()
tbl := client.Open(tableID)
mut := bigtable.NewMutation()
for bucket, count := range histogram.Histogram {
countBytes := make([]byte, 8)
binary.BigEndian.PutUint64(countBytes, uint64(count))
minute := bigtable.Time(histogram.Minute)
mut.Set("latency_histogram", fmt.Sprintf("latency:%d:%d", minute.TruncateToMilliseconds(), bucket), minute, countBytes)
}
err = tbl.Apply(context.Background(), "row-key", mut)
if err != nil {
return err
}
return nil
}
func main() {
projectID := "your-project-id"
instanceID := "your-instance-id"
tableID := "your-table-id"
var err error
err = CreateSchema(projectID, instanceID, tableID)
if err != nil {
fmt.Printf("Error al crear el esquema: %v\n", err)
return
}
for _, histogram := range getMetrics() {
err = SaveLatencyHistogram(projectID, instanceID, tableID, &histogram)
if err != nil {
fmt.Printf("Error al guardar el histograma de latencia: %v\n", err)
return
}
}
fmt.Println("Esquema y histograma de latencia guardados exitosamente en Bigtable.")
}
func getMetrics() []LatencyHistogram {
var metrics []LatencyHistogram
values := map[int64]int64{
75: 1,
100: 1,
150: 1,
200: 1,
250: 1,
300: 1,
400: 1,
500: 1,
1000: 1,
2000: 1,
4000: 1,
8000: 1,
}
for i := 0; i < 5; i++ {
for j := 0; j < 10; j++ {
histogram := LatencyHistogram{
Minute: time.Date(2023, time.May, i, j, 0, 0, 0, time.UTC),
Histogram: values,
}
metrics = append(metrics, histogram)
}
}
return metrics
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment