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

Extend anonymizer with additional parameters #2585

Merged
merged 17 commits into from
Oct 29, 2020
Merged
71 changes: 62 additions & 9 deletions cmd/anonymizer/app/anonymizer/anonymizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ type mapping struct {
//
// The mapping from original to obfuscated strings is stored in a file and can be reused between runs.
type Anonymizer struct {
mappingFile string
logger *zap.Logger

lock sync.Mutex
mapping mapping
mappingFile string
logger *zap.Logger
lock sync.Mutex
mapping mapping
hashStandardTags bool
hashCustomTags bool
hashLogs bool
hashProcess bool
}

// New creates new Anonymizer. The mappingFile stores the mapping from original to
Expand Down Expand Up @@ -136,15 +139,43 @@ func hash(value string) string {
func (a *Anonymizer) AnonymizeSpan(span *model.Span) *uimodel.Span {
service := span.Process.ServiceName
span.OperationName = a.mapOperationName(service, span.OperationName)
span.Tags = filterTags(span.Tags)
span.Logs = nil

outputTags := filterStandardTags(span.Tags)
// when true, the allowedTags are hashed and when false they are preserved as it is
if a.hashStandardTags {
outputTags = hashTags(outputTags)
}
// when true, all tags other than allowedTags are hashed, when false they are dropped
if a.hashCustomTags {
customTags := hashTags(filterCustomTags(span.Tags))
outputTags = append(outputTags, customTags...)
}
span.Tags = outputTags

// when true, logs are hashed, when false, they are dropped
if a.hashLogs {
for _, log := range span.Logs {
log.Fields = hashTags(log.Fields)
}
} else {
span.Logs = nil
}

span.Process.ServiceName = a.mapServiceName(service)
span.Process.Tags = nil

// when true, process tags are hashed, when false they are dropped
if a.hashProcess {
span.Process.Tags = hashTags(span.Process.Tags)
} else {
span.Process.Tags = nil
}

span.Warnings = nil
return uiconv.FromDomainEmbedProcess(span)
}

func filterTags(tags []model.KeyValue) []model.KeyValue {
// filterStandardTags returns only allowedTags
func filterStandardTags(tags []model.KeyValue) []model.KeyValue {
out := make([]model.KeyValue, 0, len(tags))
for _, tag := range tags {
if !allowedTags[tag.Key] {
Expand All @@ -166,3 +197,25 @@ func filterTags(tags []model.KeyValue) []model.KeyValue {
}
return out
}

// filterCustomTags returns all tags other than allowedTags
func filterCustomTags(tags []model.KeyValue) []model.KeyValue {
out := make([]model.KeyValue, 0, len(tags))
for _, tag := range tags {
if !allowedTags[tag.Key] {
out = append(out, tag)
}
}
return out
}

// hashTags converts each tag into corresponding string values
// and then find its hash
func hashTags(tags []model.KeyValue) []model.KeyValue {
out := make([]model.KeyValue, 0, len(tags))
for _, tag := range tags {
kv := model.String(tag.Key, hash(tag.AsString()))
Ashmita152 marked this conversation as resolved.
Show resolved Hide resolved
out = append(out, kv)
}
return out
}
121 changes: 121 additions & 0 deletions cmd/anonymizer/app/anonymizer/anonymizer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (c) 2020 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package anonymizer

import (
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/jaegertracing/jaeger/model"
uiconv "github.com/jaegertracing/jaeger/model/converter/json"
)

var tags = []model.KeyValue{
{
Ashmita152 marked this conversation as resolved.
Show resolved Hide resolved
Key: "error",
VType: model.BoolType,
VBool: true,
},
{
Key: "http.method",
VType: model.StringType,
VStr: "POST",
},
{
Key: "foobar",
VType: model.BoolType,
VBool: true,
},
}

var traceID = model.NewTraceID(1, 2)

var span = &model.Span{
TraceID: traceID,
SpanID: model.NewSpanID(1),
Process: &model.Process{
ServiceName: "serviceName",
Tags: model.KeyValues{},
},
OperationName: "operationName",
Tags: tags,
Logs: []model.Log{
{
Timestamp: time.Now(),
Fields: []model.KeyValue{
model.String("logKey", "logValue"),
},
},
},
Duration: time.Second * 5,
StartTime: time.Unix(300, 0),
}

func TestAnonymizer_FilterStandardTags(t *testing.T) {
expected := []model.KeyValue{
{
Key: "error",
VType: model.BoolType,
VBool: true,
},
{
Key: "http.method",
VType: model.StringType,
VStr: "POST",
},
}

actual := filterStandardTags(tags)
assert.Equal(t, expected, actual)
}

func TestAnonymizer_FilterCustomTags(t *testing.T) {
expected := []model.KeyValue{
{
Key: "foobar",
VType: model.BoolType,
VBool: true,
},
}

actual := filterCustomTags(tags)
assert.Equal(t, expected, actual)
}

func TestAnonymizer_Hash(t *testing.T) {
data := "foobar"
expected := "340d8765a4dda9c2"
actual := hash(data)
assert.Equal(t, actual, expected)
}

func TestAnonymizer_AnonymizeSpan(t *testing.T) {
anonymizer := &Anonymizer{
mapping: mapping{
Services: make(map[string]string),
Operations: make(map[string]string),
},
hashStandardTags: false,
hashCustomTags: false,
hashProcess: false,
hashLogs: false,
Ashmita152 marked this conversation as resolved.
Show resolved Hide resolved
}

actual := anonymizer.AnonymizeSpan(span)
expected := uiconv.FromDomainEmbedProcess(span)
assert.Equal(t, actual, expected)
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func strategiesJSON(probability float32) string {
"param": 5
}
]
}
}
`,
probability,
)
Expand Down