github.com/epsagon/epsagon-go@v1.39.0/example/ignored_keys/main.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"encoding/json"
     7  	"io/ioutil"
     8  	"net/http"
     9  
    10  	"github.com/epsagon/epsagon-go/epsagon"
    11  	epsagonhttp "github.com/epsagon/epsagon-go/wrappers/net/http"
    12  )
    13  
    14  func doTask(ctx context.Context) {
    15  	client := http.Client{Transport: epsagonhttp.NewTracingTransport(ctx)}
    16  	// This password will be masked in the sent trace:
    17  	decodedJSON, err := json.Marshal(map[string]string{"password": "abcde", "animal": "lion"})
    18  	if err != nil {
    19  		epsagon.Label("animal", "lion", ctx)
    20  		epsagon.TypeError(err, "json decoding error", ctx)
    21  	}
    22  	resp, err := client.Post("http://example.com/upload", "application/json", bytes.NewReader(decodedJSON))
    23  	if err != nil {
    24  		epsagon.Label("animal", "lion", ctx)
    25  		epsagon.TypeError(err, "post", ctx)
    26  	}
    27  	if resp.StatusCode != 200 {
    28  		body, _ := ioutil.ReadAll(resp.Body)
    29  		if err == nil {
    30  			epsagon.TypeError(string(body), "post status code", ctx)
    31  		} else {
    32  			epsagon.TypeError(err, "post status code", ctx)
    33  		}
    34  		epsagon.Label("animal", "lion", ctx)
    35  	}
    36  }
    37  
    38  func main() {
    39  	// With Epsagon instrumentation
    40  	config := epsagon.NewTracerConfig("test-ignored-keys", "")
    41  	config.Debug = true
    42  	config.MetadataOnly = false
    43  	config.IgnoredKeys = []string{"password"}
    44  	epsagon.ConcurrentGoWrapper(config, doTask)()
    45  }