github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/cat/path_hash.go (about)

     1  package cat
     2  
     3  import (
     4  	"crypto/md5"
     5  	"encoding/binary"
     6  	"fmt"
     7  	"regexp"
     8  )
     9  
    10  var pathHashValidator = regexp.MustCompile("^[0-9a-f]{8}$")
    11  
    12  // GeneratePathHash generates a path hash given a referring path hash,
    13  // transaction name, and application name. referringPathHash can be an empty
    14  // string if there was no referring path hash.
    15  func GeneratePathHash(referringPathHash, txnName, appName string) (string, error) {
    16  	var rph uint32
    17  	if referringPathHash != "" {
    18  		if !pathHashValidator.MatchString(referringPathHash) {
    19  			// Per the spec, invalid referring path hashes should be treated as "0".
    20  			referringPathHash = "0"
    21  		}
    22  
    23  		if _, err := fmt.Sscanf(referringPathHash, "%x", &rph); err != nil {
    24  			fmt.Println(rph)
    25  			return "", err
    26  		}
    27  		rph = (rph << 1) | (rph >> 31)
    28  	}
    29  
    30  	hashInput := fmt.Sprintf("%s;%s", appName, txnName)
    31  	hash := md5.Sum([]byte(hashInput))
    32  	low32 := binary.BigEndian.Uint32(hash[12:])
    33  
    34  	return fmt.Sprintf("%08x", rph^low32), nil
    35  }