Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
145 lines (134 loc) · 3.62 KB
/
Copy pathmain.go
File metadata and controls
145 lines (134 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"fmt"
"net/http"
"os"
"strings"
"time"
shell "github.com/ipfs/go-ipfs-api"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/textileben/ipfs-echo/metrics"
"github.com/textileben/ipfs-echo/version"
"go.uber.org/zap"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
listenAddress = kingpin.Flag(
"web.listen-address",
"Address on which to expose metrics and web interface.",
).Default(":9100").String()
remoteAPI = kingpin.Flag(
"remote.API",
"Network address of remote IPFS API endpoint",
).Required().String()
localAPI = kingpin.Flag(
"local.API",
"Network address of local IPFS API endpoint",
).Default("localhost:5001").String()
versionFlag = kingpin.Flag(
"version",
"Display binary version.",
).Default("False").Bool()
durationTimeout = kingpin.Flag(
"timeout",
"How long to wait before failing a connection.",
).Default("10").Int()
)
func main() {
kingpin.HelpFlag.Short('h')
kingpin.Parse()
if *versionFlag {
fmt.Printf("(version=%s, gitcommit=%s)\n", version.Version, version.GitCommit)
fmt.Printf("(go=%s, user=%s, date=%s)\n", version.GoVersion, version.BuildUser, version.BuildDate)
os.Exit(0)
}
logger, _ := zap.NewProduction()
defer logger.Sync()
logger.Info("config", zap.String("listenAddress", *listenAddress), zap.String("remoteAPI", *remoteAPI))
logger.Info("starting ipfs-echo", zap.String("version", version.Info()))
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>ipfs-echo exporter</title></head>
<body>
<h1>ipfs-echo exporter</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>`))
})
go runEcho()
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
logger.Fatal("Failed to listen", zap.Error(err))
}
}
func runEcho() {
logger, _ := zap.NewProduction()
defer logger.Sync()
for {
msg := RandomString(24)
logger.Info(
"Generated a message",
zap.String("msg", msg),
)
rr := metrics.NewIpfsEcho(msg)
rr.Started()
remoteShell := shell.NewShellWithClient(*remoteAPI, &http.Client{
Timeout: time.Duration(*durationTimeout) * time.Second,
})
cid, err := remoteShell.AddWithOpts(strings.NewReader(msg), false, false) // Don't pin, don't use raw leaves
if err != nil {
logger.Error(
"Failed to add the msg to the remote",
zap.Error(err),
)
metrics.IpfsEchoAttempts.WithLabelValues(
"remote", "fail",
).Add(1.0)
rr.Status = "failed"
} else {
logger.Info(
"Successfully added the msg to the remote",
zap.String("cid", cid),
)
metrics.IpfsEchoAttempts.WithLabelValues(
"remote", "success",
).Add(1.0)
rr.Status = "success"
}
rr.Finished()
metrics.IpfsEchoHistogram.WithLabelValues(
"remote", rr.Status,
).Observe(rr.Duration)
if rr.Status == "success" {
lr := metrics.NewIpfsEcho(msg)
lr.Started()
localShell := shell.NewShellWithClient(*localAPI, &http.Client{Timeout: 5 * time.Second})
obj, err := localShell.ObjectGet(cid)
if err != nil {
logger.Error(
"Failed to get the object from CID",
zap.String("cid", cid),
zap.Error(err),
)
metrics.IpfsEchoAttempts.WithLabelValues(
"local", "fail",
).Add(1.0)
lr.Status = "failed"
} else {
logger.Info(
"Successfully get the object",
zap.String("obj", obj.Data),
)
metrics.IpfsEchoAttempts.WithLabelValues(
"local", "success",
).Add(1.0)
lr.Status = "success"
}
lr.Finished()
metrics.IpfsEchoHistogram.WithLabelValues(
"local", lr.Status,
).Observe(lr.Duration)
}
time.Sleep(30 * time.Second)
}
}