-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
726 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
logging { | ||
// level = "debug" | ||
level = "info" | ||
} | ||
|
||
prometheus.scrape_task.receive.fake "a" { | ||
forward_to = [ | ||
prometheus.scrape_task.scrape.a.receiver, | ||
] | ||
// This should match the producer.alloy | ||
scrape_interval = "15s" | ||
targets_count = 1000 | ||
} | ||
|
||
prometheus.scrape_task.scrape "a" { | ||
forward_to = [prometheus.scrape_task.send.a.receiver] | ||
pool_size = 20 | ||
} | ||
|
||
prometheus.scrape_task.send "a" { | ||
|
||
} | ||
|
||
// Traditional pipeline to publish metrics | ||
|
||
prometheus.exporter.self "self" {} | ||
|
||
discovery.relabel "replace_instance" { | ||
targets = prometheus.exporter.self.self.targets | ||
rule { | ||
action = "replace" | ||
source_labels = ["instance"] | ||
target_label = "instance" | ||
replacement = env("INSTANCE") | ||
} | ||
rule { | ||
action = "replace" | ||
source_labels = ["job"] | ||
target_label = "job" | ||
replacement = "integrations/alloy" | ||
} | ||
} | ||
|
||
prometheus.scrape "self" { | ||
targets = discovery.relabel.replace_instance.output | ||
forward_to = [prometheus.remote_write.cloud.receiver] | ||
scrape_interval = "15s" | ||
} | ||
|
||
prometheus.remote_write "cloud" { | ||
endpoint { | ||
url = "https://prometheus-prod-05-gb-south-0.grafana.net/api/prom/push" | ||
|
||
basic_auth { | ||
username = "949385" | ||
password_file = env("MIMIR_PWD_FILE") | ||
} | ||
} | ||
|
||
external_labels = { | ||
"cluster" = "my-desk", | ||
"namespace" = "alloy", | ||
"alloy_instance" = env("INSTANCE"), | ||
"role" = "cluster-consumer", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
logging { | ||
// level = "debug" | ||
level = "info" | ||
} | ||
|
||
prometheus.scrape_task.produce.redis "a" { | ||
redis_address = "localhost:6379" | ||
// This should match the consumer-cluster.alloy | ||
scrape_interval = "15s" | ||
targets_count = 1000 | ||
} | ||
|
||
|
||
// Traditional pipeline to publish own metrics | ||
|
||
prometheus.exporter.self "self" {} | ||
|
||
discovery.relabel "replace_instance" { | ||
targets = prometheus.exporter.self.self.targets | ||
rule { | ||
action = "replace" | ||
source_labels = ["instance"] | ||
target_label = "instance" | ||
replacement = env("INSTANCE") | ||
} | ||
rule { | ||
action = "replace" | ||
source_labels = ["job"] | ||
target_label = "job" | ||
replacement = "integrations/alloy" | ||
} | ||
} | ||
|
||
prometheus.scrape "self" { | ||
targets = discovery.relabel.replace_instance.output | ||
forward_to = [prometheus.remote_write.cloud.receiver] | ||
scrape_interval = "15s" | ||
} | ||
|
||
prometheus.remote_write "cloud" { | ||
endpoint { | ||
url = "https://prometheus-prod-05-gb-south-0.grafana.net/api/prom/push" | ||
|
||
basic_auth { | ||
username = "949385" | ||
password_file = env("MIMIR_PWD_FILE") | ||
} | ||
} | ||
|
||
external_labels = { | ||
"cluster" = "my-desk", | ||
"namespace" = "alloy", | ||
"alloy_instance" = env("INSTANCE"), | ||
"role" = "producer", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# producer | ||
curl localhost:8000/-/reload | ||
|
||
# redis | ||
curl localhost:8001/-/reload | ||
curl localhost:8002/-/reload | ||
curl localhost:8003/-/reload | ||
curl localhost:8004/-/reload | ||
|
||
# cluster | ||
curl localhost:8101/-/reload | ||
curl localhost:8102/-/reload | ||
curl localhost:8103/-/reload | ||
curl localhost:8104/-/reload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
echo "running $1 on port $2" | ||
INSTANCE=$1 MIMIR_PWD_FILE=~/workspace/go-playground/secrets/MIMIR_TOKEN alloy run --stability.level experimental consumer-cluster.alloy --cluster.name cluster --cluster.enabled --cluster.node-name "$1" --server.http.listen-addr "127.0.0.1:$2" --cluster.advertise-address "127.0.0.1:$2" --storage.path "./tmp/data_$1" --cluster.join-addresses "127.0.0.1:8101,127.0.0.1:8102,127.0.0.1:8103,127.0.0.1:8104" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
echo "running $1 on port $2" | ||
INSTANCE=$1 MIMIR_PWD_FILE=~/workspace/go-playground/secrets/MIMIR_TOKEN alloy run --stability.level experimental consumer-redis.alloy --cluster.name redis --cluster.enabled --cluster.node-name "$1" --server.http.listen-addr "127.0.0.1:$2" --cluster.advertise-address "127.0.0.1:$2" --storage.path "./tmp/data_$1" --cluster.join-addresses "127.0.0.1:8001,127.0.0.1:8002,127.0.0.1:8003,127.0.0.1:8004" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
echo "running producer $1 on port $2" | ||
INSTANCE=$1 MIMIR_PWD_FILE=~/workspace/go-playground/secrets/MIMIR_TOKEN alloy run --stability.level experimental producer.alloy --server.http.listen-addr "127.0.0.1:$2" --storage.path "./tmp/data_$1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"os/signal" | ||
"strconv" | ||
"sync" | ||
"syscall" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) < 5 { | ||
fmt.Println("Usage: go run main.go <script-path> <name-prefix> <port-start> <N>") | ||
os.Exit(1) | ||
} | ||
|
||
scriptPath := os.Args[1] | ||
namePrefix := os.Args[2] | ||
portStart, err := strconv.Atoi(os.Args[3]) | ||
if err != nil { | ||
fmt.Println("Invalid port start") | ||
os.Exit(1) | ||
} | ||
numInstances, err := strconv.Atoi(os.Args[4]) | ||
if err != nil { | ||
fmt.Println("Error converting num instances to integer") | ||
os.Exit(1) | ||
} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
var wg sync.WaitGroup | ||
|
||
// Trap SIGINT (CTRL+C) and SIGTERM to gracefully shutdown | ||
signalChan := make(chan os.Signal, 1) | ||
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM) | ||
|
||
// Run the script N times in parallel | ||
for i := 0; i < numInstances; i++ { | ||
wg.Add(1) | ||
go func(instanceID int) { | ||
defer wg.Done() | ||
runScript(ctx, scriptPath, namePrefix, portStart, instanceID) | ||
}(i) | ||
} | ||
|
||
// Wait for CTRL+C or termination signal | ||
<-signalChan | ||
fmt.Println("\nShutting down...") | ||
cancel() // Signal all scripts to stop | ||
|
||
wg.Wait() // Wait for all goroutines to finish | ||
fmt.Println("All scripts stopped.") | ||
os.Exit(0) | ||
} | ||
|
||
// runScript runs a given script in a separate goroutine and pipes its output with a prefix | ||
func runScript(ctx context.Context, scriptPath string, namePrefix string, portStart int, instanceID int) { | ||
cmd := exec.CommandContext(ctx, "bash", scriptPath, namePrefix+"-"+strconv.Itoa(instanceID), strconv.Itoa(portStart+instanceID)) | ||
|
||
// Get the stdout and stderr pipes | ||
stdoutPipe, err := cmd.StdoutPipe() | ||
if err != nil { | ||
fmt.Printf("[Instance %d] Error creating stdout pipe: %v\n", instanceID, err) | ||
return | ||
} | ||
|
||
stderrPipe, err := cmd.StderrPipe() | ||
if err != nil { | ||
fmt.Printf("[Instance %d] Error creating stderr pipe: %v\n", instanceID, err) | ||
return | ||
} | ||
|
||
// Start the command | ||
if err := cmd.Start(); err != nil { | ||
fmt.Printf("[Instance %d] Failed to start script: %v\n", instanceID, err) | ||
return | ||
} | ||
|
||
// Log stdout and stderr with prefixes | ||
go logOutput(fmt.Sprintf("[Instance %d] stdout: ", instanceID), stdoutPipe) | ||
go logOutput(fmt.Sprintf("[Instance %d] stderr: ", instanceID), stderrPipe) | ||
|
||
// Wait for the command to complete | ||
if err := cmd.Wait(); err != nil { | ||
fmt.Printf("[Instance %d] Script exited with error: %v\n", instanceID, err) | ||
} | ||
} | ||
|
||
// logOutput reads from a pipe and logs each line with a given prefix | ||
func logOutput(prefix string, pipe io.ReadCloser) { | ||
scanner := bufio.NewScanner(pipe) | ||
for scanner.Scan() { | ||
fmt.Println(prefix + scanner.Text()) | ||
} | ||
if err := scanner.Err(); err != nil { | ||
fmt.Printf("%s Error reading output: %v\n", prefix, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.