summaryrefslogtreecommitdiff
path: root/network/messagetracer/graphtrace.go
blob: e0390febe693a876950f2272a96c71782b9a55b6 (plain)
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
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.
//
//go:build msgtrace
// +build msgtrace

package messagetracer

import (
	"hash/fnv"

	"github.com/algorand/graphtrace/graphtrace"

	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/logging"
)

type graphtraceMessageTracer struct {
	tracer graphtrace.Client
	log    logging.Logger
}

func (gmt *graphtraceMessageTracer) Init(cfg config.Local) MessageTracer {
	if cfg.NetworkMessageTraceServer == "" {
		gmt.log.Info("NetworkMessageTraceServer empty graphtrace disabled")
		return nil
	}
	var err error
	gmt.tracer, err = graphtrace.NewTCPClient(cfg.NetworkMessageTraceServer, gmt.log)
	if err != nil {
		gmt.log.Errorf("unable to create trace client: %v", err)
		return nil
	}
	gmt.log.Infof("tracing network messages to %s", cfg.NetworkMessageTraceServer)
	return gmt
}
func (gmt *graphtraceMessageTracer) HashTrace(prefix string, data []byte) {
	hasher := fnv.New64a()
	hasher.Write(data)
	pb := []byte(prefix)
	msg := make([]byte, len(pb)+8)
	copy(msg, pb)
	hash := hasher.Sum(msg[0:len(pb)])
	gmt.tracer.Trace(hash)
}

// NewGraphtraceMessageTracer returns a new MessageTracer that sends data to a graphtrace collector
func NewGraphtraceMessageTracer(log logging.Logger) MessageTracer {
	return &graphtraceMessageTracer{log: log}
}

func init() {
	if implFactory != nil {
		panic("at most one MessageTracer impl should be compiled in, dup found at graphtrace.go init()")
	}
	implFactory = NewGraphtraceMessageTracer
}