Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a couple metrics / enable /metrics and /pprof for indexstar #19

Merged
merged 2 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,45 @@ package main

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"sync"
"sync/atomic"
"time"

"github.com/filecoin-project/storetheindex/api/v0/finder/model"
"github.com/filecoin-shipyard/indexstar/httpserver"
"github.com/filecoin-shipyard/indexstar/metrics"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
)

func (s *server) find(w http.ResponseWriter, r *http.Request) {
combined := make(chan *model.FindResponse, len(s.servers))
wg := sync.WaitGroup{}
start := time.Now()
tags := []tag.Mutator{}
defer func() {
_ = stats.RecordWithOptions(context.Background(),
stats.WithTags(tags...),
stats.WithMeasurements(metrics.FindLatency.M(float64(time.Since(start).Milliseconds()))))
}()

// Copy the original request body in case it is a POST batch find request.
var rb []byte
var err error
rb, err = io.ReadAll(r.Body)
_ = r.Body.Close()
if err != nil {
tags = append(tags, tag.Insert(metrics.Found, "client_error"))
log.Warnw("failed to read original request body", "err", err)
return
}

count := atomic.Int32{}
for _, server := range s.servers {
wg.Add(1)
go func(server *url.URL) {
Expand Down Expand Up @@ -61,18 +76,23 @@ func (s *server) find(w http.ResponseWriter, r *http.Request) {
return
}
if resp.StatusCode == http.StatusOK {
_ = count.Add(1)
providers, err := model.UnmarshalFindResponse(data)
if err == nil {
combined <- providers
} else {
log.Warnw("failed backend unmarshal", "err", err)
}
} else if resp.StatusCode == http.StatusNotFound {
_ = count.Add(1)
}
}(server)
}
go func() {
wg.Wait()
close(combined)
_ = stats.RecordWithOptions(context.Background(),
stats.WithMeasurements(metrics.FindBackends.M(float64(count.Load()))))
}()

// TODO: stream out partial response as they come in.
Expand Down Expand Up @@ -101,8 +121,11 @@ outer:
}

if resp.MultihashResults == nil {
tags = append(tags, tag.Insert(metrics.Found, "no"))
http.Error(w, "no results for query", http.StatusNotFound)
return
} else {
tags = append(tags, tag.Insert(metrics.Found, "yes"))
}

// write out combined.
Expand Down
16 changes: 16 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@ module github.com/filecoin-shipyard/indexstar
go 1.19

require (
contrib.go.opencensus.io/exporter/prometheus v0.4.0
github.com/filecoin-project/storetheindex v0.4.20
github.com/ipfs/go-cid v0.2.0
github.com/ipfs/go-delegated-routing v0.3.0
github.com/ipfs/go-log/v2 v2.5.1
github.com/libp2p/go-libp2p-core v0.16.1
github.com/prometheus/client_golang v1.12.1
github.com/urfave/cli/v2 v2.11.1
go.opencensus.io v0.23.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd v0.22.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.1.3 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/go-kit/log v0.2.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/ipfs/go-ipfs-util v0.0.2 // indirect
github.com/ipfs/go-ipns v0.1.2 // indirect
github.com/ipfs/go-log v1.0.5 // indirect
Expand All @@ -28,6 +37,7 @@ require (
github.com/libp2p/go-libp2p-record v0.1.3 // indirect
github.com/libp2p/go-openssl v0.0.7 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
Expand All @@ -41,6 +51,10 @@ require (
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/polydawn/refmt v0.0.0-20201211092308-30ac6d18308e // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.33.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/prometheus/statsd_exporter v0.21.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
Expand All @@ -50,5 +64,7 @@ require (
go.uber.org/zap v1.21.0 // indirect
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 // indirect
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
lukechampine.com/blake3 v1.1.7 // indirect
)
Loading