mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-05 19:21:46 +00:00
An example micro-services based application for testing purposes.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -39,6 +39,7 @@ experimental/fixprobe/fixprobe
|
||||
experimental/genreport/genreport
|
||||
experimental/graphviz/graphviz
|
||||
experimental/oneshot/oneshot
|
||||
experimental/example/qotd/qotd
|
||||
*sublime-project
|
||||
*sublime-workspace
|
||||
*npm-debug.log
|
||||
|
||||
16
experimental/example/Makefile
Normal file
16
experimental/example/Makefile
Normal file
@@ -0,0 +1,16 @@
|
||||
CC=gcc
|
||||
CFLAGS=-g -lpthread
|
||||
|
||||
%.o: %.c
|
||||
$(CC) -c -o $@ $< $(CFLAGS)
|
||||
|
||||
all: qotd/qotd goapp/app
|
||||
|
||||
qotd/qotd: qotd/qotd.o
|
||||
gcc -o $@ $< $(CFLAGS)
|
||||
|
||||
goapp/app: goapp/app.go
|
||||
go build -o goapp/app ./goapp
|
||||
|
||||
clean:
|
||||
rm -f qotd/*.o qotd/qotd goapp/app
|
||||
2
experimental/example/README.md
Normal file
2
experimental/example/README.md
Normal file
@@ -0,0 +1,2 @@
|
||||
To run:
|
||||
# make && docker-compose up -d
|
||||
6
experimental/example/client/Dockerfile
Normal file
6
experimental/example/client/Dockerfile
Normal file
@@ -0,0 +1,6 @@
|
||||
FROM gliderlabs/alpine
|
||||
MAINTAINER Weaveworks Inc <help@weave.works>
|
||||
WORKDIR /home/weave
|
||||
ADD ./client /home/weave/
|
||||
RUN chmod u+x /home/weave/client
|
||||
ENTRYPOINT ["/home/weave/client"]
|
||||
3
experimental/example/client/client
Executable file
3
experimental/example/client/client
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
while true; do echo "foo" | nc qotd 4446; sleep 1; done
|
||||
26
experimental/example/docker-compose.yml
Normal file
26
experimental/example/docker-compose.yml
Normal file
@@ -0,0 +1,26 @@
|
||||
client:
|
||||
build: client
|
||||
links:
|
||||
- qotd
|
||||
qotd:
|
||||
build: qotd
|
||||
expose:
|
||||
- "4446"
|
||||
pyapp:
|
||||
build: pyapp
|
||||
ports:
|
||||
- "5000:5000"
|
||||
links:
|
||||
- redis
|
||||
- qotd
|
||||
redis:
|
||||
image: redis
|
||||
goapp:
|
||||
build: goapp
|
||||
links:
|
||||
- elasticsearch
|
||||
elasticsearch:
|
||||
image: elasticsearch
|
||||
expose:
|
||||
- "9200"
|
||||
- "9300"
|
||||
5
experimental/example/goapp/Dockerfile
Normal file
5
experimental/example/goapp/Dockerfile
Normal file
@@ -0,0 +1,5 @@
|
||||
FROM gliderlabs/alpine
|
||||
MAINTAINER Weaveworks Inc <help@weave.works>
|
||||
WORKDIR /home/weave
|
||||
ADD . /home/weave/
|
||||
ENTRYPOINT ["/home/weave/entrypoint.sh"]
|
||||
118
experimental/example/goapp/app.go
Normal file
118
experimental/example/goapp/app.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
addr = flag.String("addr", ":8080", "HTTP listen address")
|
||||
rate = flag.Duration("rate", 3*time.Second, "request rate")
|
||||
)
|
||||
flag.Parse()
|
||||
|
||||
var targets []string
|
||||
for _, s := range os.Args[1:] {
|
||||
target, err := normalize(s)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Printf("target %s", target)
|
||||
targets = append(targets, target)
|
||||
}
|
||||
if len(targets) <= 0 {
|
||||
log.Fatal("no targets")
|
||||
}
|
||||
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
hostname = strings.ToLower(strings.Replace(hostname, " ", "-", -1)) // lol
|
||||
}
|
||||
|
||||
var reads uint64
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
||||
fmt.Fprintf(w, "%s %d", hostname, atomic.LoadUint64(&reads))
|
||||
})
|
||||
|
||||
errc := make(chan error)
|
||||
|
||||
go func() {
|
||||
log.Printf("listening on %s", *addr)
|
||||
errc <- http.ListenAndServe(*addr, nil)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
errc <- read(targets, *rate, &reads)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
errc <- interrupt()
|
||||
}()
|
||||
|
||||
log.Printf("%s", <-errc)
|
||||
}
|
||||
|
||||
func read(targets []string, rate time.Duration, reads *uint64) error {
|
||||
for range time.Tick(rate) {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(targets))
|
||||
for _, target := range targets {
|
||||
go func(target string) {
|
||||
get(target)
|
||||
atomic.AddUint64(reads, 1)
|
||||
wg.Done()
|
||||
}(target)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func get(target string) {
|
||||
resp, err := http.Get(target)
|
||||
if err != nil {
|
||||
log.Printf("%s: %v", target, err)
|
||||
return
|
||||
}
|
||||
log.Printf("%s: %s", target, resp.Status)
|
||||
io.Copy(ioutil.Discard, resp.Body)
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
func interrupt() error {
|
||||
c := make(chan os.Signal)
|
||||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
||||
return fmt.Errorf("%s", <-c)
|
||||
}
|
||||
|
||||
func normalize(s string) (string, error) {
|
||||
u, err := url.Parse(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if u.Scheme == "" {
|
||||
u.Scheme = "http"
|
||||
}
|
||||
if u.Host == "" && u.Path != "" {
|
||||
u.Host, u.Path = u.Path, u.Host
|
||||
}
|
||||
if _, port, err := net.SplitHostPort(u.Host); err != nil || port == "" {
|
||||
u.Host = u.Host + ":9200"
|
||||
}
|
||||
return u.String(), nil
|
||||
}
|
||||
3
experimental/example/goapp/entrypoint.sh
Executable file
3
experimental/example/goapp/entrypoint.sh
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
/home/weave/app $elasticsearch
|
||||
6
experimental/example/pyapp/Dockerfile
Normal file
6
experimental/example/pyapp/Dockerfile
Normal file
@@ -0,0 +1,6 @@
|
||||
FROM python:2.7
|
||||
MAINTAINER Weaveworks Inc <help@weave.works>
|
||||
ADD . /home/weave
|
||||
WORKDIR /home/weave
|
||||
RUN pip install -r /home/weave/requirements.txt
|
||||
ENTRYPOINT ["python", "/home/weave/app.py"]
|
||||
15
experimental/example/pyapp/app.py
Normal file
15
experimental/example/pyapp/app.py
Normal file
@@ -0,0 +1,15 @@
|
||||
import os
|
||||
|
||||
from flask import Flask
|
||||
from redis import Redis
|
||||
|
||||
app = Flask(__name__)
|
||||
redis = Redis(host='redis', port=6379)
|
||||
|
||||
@app.route('/')
|
||||
def hello():
|
||||
redis.incr('hits')
|
||||
return 'Hello World! I have been seen %s times.' % redis.get('hits')
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", debug=True)
|
||||
5
experimental/example/pyapp/requirements.txt
Normal file
5
experimental/example/pyapp/requirements.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
flask
|
||||
redis
|
||||
requests
|
||||
futures
|
||||
requests-futures
|
||||
5
experimental/example/qotd/Dockerfile
Normal file
5
experimental/example/qotd/Dockerfile
Normal file
@@ -0,0 +1,5 @@
|
||||
FROM ubuntu
|
||||
MAINTAINER Weaveworks Inc <help@weave.works>
|
||||
WORKDIR /home/weave
|
||||
ADD ./qotd /home/weave/
|
||||
ENTRYPOINT ["/home/weave/qotd"]
|
||||
99
experimental/example/qotd/qotd.c
Normal file
99
experimental/example/qotd/qotd.c
Normal file
@@ -0,0 +1,99 @@
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/syscall.h>
|
||||
|
||||
/* this function is run by the thread */
|
||||
void *thread_func(void *sock) {
|
||||
struct sockaddr_in dest;
|
||||
char buffer[1024];
|
||||
int sockfd = (int)sock;
|
||||
int clientfd;
|
||||
|
||||
printf("I'm thread %d\n", syscall(SYS_gettid));
|
||||
|
||||
if (read(sockfd, buffer, sizeof(buffer), 0) < 0) {
|
||||
perror("ERROR reading from socket");
|
||||
return;
|
||||
}
|
||||
|
||||
clientfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (clientfd < 0) {
|
||||
perror("ERROR opening socket");
|
||||
return;
|
||||
}
|
||||
|
||||
dest.sin_family = AF_INET;
|
||||
dest.sin_port = htons(17);
|
||||
if (inet_aton("104.230.14.102", &dest.sin_addr.s_addr) == 0) {
|
||||
perror("cygnus");
|
||||
return;
|
||||
}
|
||||
|
||||
if (connect(clientfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 ) {
|
||||
perror("Connect ");
|
||||
return;
|
||||
}
|
||||
|
||||
int readbytes = recv(clientfd, buffer, sizeof(buffer), 0);
|
||||
close(clientfd);
|
||||
|
||||
if (write(sockfd, buffer, read) < 0) {
|
||||
perror("ERROR writing to socket");
|
||||
return;
|
||||
}
|
||||
|
||||
if (close(sockfd)) {
|
||||
perror("ERROR closing socket");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int sockfd, newsockfd, portno, clilen;
|
||||
struct sockaddr_in serv_addr, cli_addr;
|
||||
int n;
|
||||
|
||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sockfd < 0) {
|
||||
perror("ERROR opening socket");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int i = 1;
|
||||
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) == -1) {
|
||||
perror("setsockopt");
|
||||
}
|
||||
|
||||
// bzero((char *) &serv_addr, sizeof(serv_addr));
|
||||
portno = 4446;
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
serv_addr.sin_port = htons(portno);
|
||||
|
||||
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
|
||||
perror("ERROR on binding");
|
||||
return 1;
|
||||
}
|
||||
|
||||
listen(sockfd, 5);
|
||||
|
||||
while (1) {
|
||||
clilen = sizeof(cli_addr);
|
||||
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
|
||||
if (newsockfd < 0) {
|
||||
perror("ERROR on accept");
|
||||
return 1;
|
||||
}
|
||||
|
||||
pthread_t thread0;
|
||||
if(pthread_create(&thread0, NULL, thread_func, (void *)newsockfd)) {
|
||||
perror("ERROR on pthread_create");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user