-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[anonymizer] Save trace in UI format #2629
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
71aa60e
[anonymizer]: save trace in UI format
yurishkuro dd5444f
remove log
yurishkuro c333eee
Fix test
yurishkuro 1c43b49
Add more tests for reader.go
Ashmita152 b739de3
Add unit tests for extractor.go and module.go
Ashmita152 23daf36
Add unit tests to module.go to increase overall uiconv package cover…
Ashmita152 d75c23c
Simplify the wrong json tests
Ashmita152 b4db0aa
Using require.Contains instead of require.Equal for some tests
Ashmita152 360e29a
Updated uiconv tests: remove loglines related to stdlib
Ashmita152 6704a92
Updated uiconv tests: remove loglines related to stdlib
Ashmita152 784b9d0
Simplify the wrong json tests
Ashmita152 cea616b
Merge pull request #2 from Ashmita152/fix-2556
yurishkuro 1710c6a
Merge branch 'master' into fix-2556
yurishkuro a980b3a
Improve code coverage for uiconv package
Ashmita152 4628dbb
Remove unused code and test-case
Ashmita152 4add027
Merge pull request #3 from Ashmita152/fix-2556
yurishkuro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// 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 uiconv | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"go.uber.org/zap" | ||
|
||
uimodel "github.com/jaegertracing/jaeger/model/json" | ||
) | ||
|
||
// Extractor reads the spans from reader, filters by traceID, and stores as JSON into uiFile. | ||
type Extractor struct { | ||
uiFile *os.File | ||
traceID string | ||
reader *Reader | ||
logger *zap.Logger | ||
} | ||
|
||
// NewExtractor creates Extractor. | ||
func NewExtractor(uiFile string, traceID string, reader *Reader, logger *zap.Logger) (*Extractor, error) { | ||
f, err := os.OpenFile(uiFile, os.O_CREATE|os.O_WRONLY, os.ModePerm) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot create output file: %w", err) | ||
} | ||
logger.Sugar().Infof("Writing spans to UI file %s", uiFile) | ||
|
||
return &Extractor{ | ||
uiFile: f, | ||
traceID: traceID, | ||
reader: reader, | ||
logger: logger, | ||
}, nil | ||
} | ||
|
||
// Run executes the extraction. | ||
func (e *Extractor) Run() error { | ||
e.logger.Info("Parsing captured file for trace", zap.String("trace_id", e.traceID)) | ||
|
||
var ( | ||
spans []uimodel.Span | ||
span *uimodel.Span | ||
err error | ||
) | ||
for span, err = e.reader.NextSpan(); err == nil; span, err = e.reader.NextSpan() { | ||
if string(span.TraceID) == e.traceID { | ||
spans = append(spans, *span) | ||
} | ||
} | ||
if err != io.EOF { | ||
return fmt.Errorf("failed when scanning the file: %w", err) | ||
} | ||
trace := uimodel.Trace{ | ||
TraceID: uimodel.TraceID(e.traceID), | ||
Spans: spans, | ||
Processes: make(map[uimodel.ProcessID]uimodel.Process), | ||
} | ||
// (ys) The following is not exactly correct because it does not dedupe the processes, | ||
// but I don't think it affects the UI. | ||
for i := range spans { | ||
span := &spans[i] | ||
pid := uimodel.ProcessID(fmt.Sprintf("p%d", i)) | ||
trace.Processes[pid] = *span.Process | ||
span.Process = nil | ||
span.ProcessID = pid | ||
} | ||
jsonBytes, err := json.Marshal(trace) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal UI trace: %w", err) | ||
} | ||
e.uiFile.Write([]byte(`{"data": [`)) | ||
e.uiFile.Write(jsonBytes) | ||
e.uiFile.Write([]byte(`]}`)) | ||
e.uiFile.Sync() | ||
e.uiFile.Close() | ||
e.logger.Sugar().Infof("Wrote spans to UI file %s", e.uiFile.Name()) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// 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 uiconv | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/go-openapi/swag" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
|
||
"github.com/jaegertracing/jaeger/model" | ||
) | ||
|
||
type UITrace struct { | ||
Data []model.Trace | ||
} | ||
|
||
func TestExtractor_TraceSuccess(t *testing.T) { | ||
inputFile := "fixtures/trace_success.json" | ||
outputFile := "fixtures/trace_success_ui_anonymized.json" | ||
defer os.Remove(outputFile) | ||
|
||
reader, err := NewReader( | ||
inputFile, | ||
zap.NewNop(), | ||
) | ||
require.NoError(t, err) | ||
|
||
extractor, err := NewExtractor( | ||
outputFile, | ||
"2be38093ead7a083", | ||
reader, | ||
zap.NewNop(), | ||
) | ||
require.NoError(t, err) | ||
|
||
err = extractor.Run() | ||
require.NoError(t, err) | ||
|
||
var trace UITrace | ||
loadJSON(t, outputFile, &trace) | ||
|
||
for i := range trace.Data { | ||
for j := range trace.Data[i].Spans { | ||
assert.Equal(t, "span.kind", trace.Data[i].Spans[j].Tags[0].Key) | ||
} | ||
} | ||
} | ||
|
||
func TestExtractor_TraceOutputFileError(t *testing.T) { | ||
inputFile := "fixtures/trace_success.json" | ||
outputFile := "fixtures/trace_success_ui_anonymized.json" | ||
defer os.Remove(outputFile) | ||
|
||
reader, err := NewReader( | ||
inputFile, | ||
zap.NewNop(), | ||
) | ||
require.NoError(t, err) | ||
|
||
err = os.Chmod("fixtures", 0000) | ||
require.NoError(t, err) | ||
defer os.Chmod("fixtures", 0755) | ||
|
||
_, err = NewExtractor( | ||
outputFile, | ||
"2be38093ead7a083", | ||
reader, | ||
zap.NewNop(), | ||
) | ||
require.Contains(t, err.Error(), "cannot create output file") | ||
} | ||
|
||
func TestExtractor_TraceScanError(t *testing.T) { | ||
inputFile := "fixtures/trace_scan_error.json" | ||
outputFile := "fixtures/trace_scan_error_ui_anonymized.json" | ||
defer os.Remove(outputFile) | ||
|
||
reader, err := NewReader( | ||
inputFile, | ||
zap.NewNop(), | ||
) | ||
require.NoError(t, err) | ||
|
||
extractor, err := NewExtractor( | ||
outputFile, | ||
"2be38093ead7a083", | ||
reader, | ||
zap.NewNop(), | ||
) | ||
require.NoError(t, err) | ||
|
||
err = extractor.Run() | ||
require.Contains(t, err.Error(), "failed when scanning the file") | ||
} | ||
|
||
func loadJSON(t *testing.T, fileName string, i interface{}) { | ||
b, err := ioutil.ReadFile(fileName) | ||
require.NoError(t, err) | ||
err = swag.ReadJSON(b, i) | ||
require.NoError(t, err, "Failed to parse json fixture file %s", fileName) | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[{"traceID":"2be38093ead7a083","spanID":"7bd66f09ba90ea3d","duration": "invalid"} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[{"traceID":"2be38093ead7a083","spanID":"7606ddfe69932d34","duration":267037}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[{"traceID":"2be38093ead7a083","spanID":"7606ddfe69932d34","flags":1,"operationName":"a071653098f9250d","references":[{"refType":"CHILD_OF","traceID":"2be38093ead7a083","spanID":"492770a15935810f"}],"startTime":1605223981761425,"duration":267037,"tags":[{"key":"span.kind","type":"string","value":"server"}],"logs":[],"process":{"serviceName":"16af988c443cff37","tags":[]},"warnings":null}, | ||
{"traceID":"2be38093ead7a083","spanID":"7bd66f09ba90ea3d","flags":1,"operationName":"471418097747d04a","references":[{"refType":"CHILD_OF","traceID":"2be38093ead7a083","spanID":"7606ddfe69932d34"}],"startTime":1605223981965074,"duration":32782,"tags":[{"key":"span.kind","type":"string","value":"client"},{"key":"error","type":"bool","value":"true"}],"logs":[],"process":{"serviceName":"3c220036602f839e","tags":[]},"warnings":null} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"traceID":"2be38093ead7a083","spanID":"7606ddfe69932d34","duration":267037} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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 uiconv | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
) | ||
|
||
// Config for the extractor. | ||
type Config struct { | ||
CapturedFile string `yaml:"captured_file"` | ||
UIFile string `yaml:"ui_file"` | ||
TraceID string `yaml:"trace_id"` | ||
} | ||
|
||
// Extract reads anonymized file, finds spans for a given trace, | ||
// and writes out that trace in the UI format. | ||
func Extract(config Config, logger *zap.Logger) error { | ||
reader, err := NewReader(config.CapturedFile, logger) | ||
if err != nil { | ||
return err | ||
} | ||
ext, err := NewExtractor(config.UIFile, config.TraceID, reader, logger) | ||
if err != nil { | ||
return err | ||
} | ||
return ext.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 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 uiconv | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestModule_TraceSuccess(t *testing.T) { | ||
inputFile := "fixtures/trace_success.json" | ||
outputFile := "fixtures/trace_success_ui_anonymized.json" | ||
defer os.Remove(outputFile) | ||
|
||
config := Config{ | ||
CapturedFile: inputFile, | ||
UIFile: outputFile, | ||
TraceID: "2be38093ead7a083", | ||
} | ||
err := Extract(config, zap.NewNop()) | ||
require.NoError(t, err) | ||
|
||
var trace UITrace | ||
loadJSON(t, outputFile, &trace) | ||
|
||
for i := range trace.Data { | ||
for j := range trace.Data[i].Spans { | ||
assert.Equal(t, "span.kind", trace.Data[i].Spans[j].Tags[0].Key) | ||
} | ||
} | ||
} | ||
|
||
func TestModule_TraceNonExistent(t *testing.T) { | ||
inputFile := "fixtures/trace_non_existent.json" | ||
outputFile := "fixtures/trace_non_existent_ui_anonymized.json" | ||
defer os.Remove(outputFile) | ||
|
||
config := Config{ | ||
CapturedFile: inputFile, | ||
UIFile: outputFile, | ||
TraceID: "2be38093ead7a083", | ||
} | ||
err := Extract(config, zap.NewNop()) | ||
require.Contains(t, err.Error(), "cannot open captured file") | ||
} | ||
|
||
func TestModule_TraceOutputFileError(t *testing.T) { | ||
inputFile := "fixtures/trace_success.json" | ||
outputFile := "fixtures/trace_success_ui_anonymized.json" | ||
defer os.Remove(outputFile) | ||
|
||
config := Config{ | ||
CapturedFile: inputFile, | ||
UIFile: outputFile, | ||
TraceID: "2be38093ead7a083", | ||
} | ||
|
||
err := os.Chmod("fixtures", 0550) | ||
require.NoError(t, err) | ||
defer os.Chmod("fixtures", 0755) | ||
|
||
err = Extract(config, zap.NewNop()) | ||
require.Contains(t, err.Error(), "cannot create output file") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Process section is not exactly correct because it does not dedupe them, but I don't think it affects the UI.