-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement anonymizer's main program (#2621)
- Loading branch information
1 parent
dcf56e8
commit 1d22aaf
Showing
10 changed files
with
363 additions
and
35 deletions.
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
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
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
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
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,89 @@ | ||
// 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 app | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// Options represent configurable parameters for jaeger-anonymizer | ||
type Options struct { | ||
QueryGRPCHostPort string | ||
MaxSpansCount int | ||
TraceID string | ||
OutputDir string | ||
HashStandardTags bool | ||
HashCustomTags bool | ||
HashLogs bool | ||
HashProcess bool | ||
} | ||
|
||
const ( | ||
queryGRPCHostPortFlag = "query-host-port" | ||
outputDirFlag = "output-dir" | ||
traceIDFlag = "trace-id" | ||
hashStandardTagsFlag = "hash-standard-tags" | ||
hashCustomTagsFlag = "hash-custom-tags" | ||
hashLogsFlag = "hash-logs" | ||
hashProcessFlag = "hash-process" | ||
maxSpansCount = "max-spans-count" | ||
) | ||
|
||
// AddFlags adds flags for anonymizer main program | ||
func (o *Options) AddFlags(command *cobra.Command) { | ||
command.Flags().StringVar( | ||
&o.QueryGRPCHostPort, | ||
queryGRPCHostPortFlag, | ||
"localhost:16686", | ||
"The host:port of the jaeger-query endpoint") | ||
command.Flags().StringVar( | ||
&o.OutputDir, | ||
outputDirFlag, | ||
"/tmp", | ||
"The directory to store the anonymized trace") | ||
command.Flags().StringVar( | ||
&o.TraceID, | ||
traceIDFlag, | ||
"", | ||
"The trace-id of trace to anonymize") | ||
command.Flags().BoolVar( | ||
&o.HashStandardTags, | ||
hashStandardTagsFlag, | ||
false, | ||
"Whether to hash standard tags") | ||
command.Flags().BoolVar( | ||
&o.HashCustomTags, | ||
hashCustomTagsFlag, | ||
false, | ||
"Whether to hash custom tags") | ||
command.Flags().BoolVar( | ||
&o.HashLogs, | ||
hashLogsFlag, | ||
false, | ||
"Whether to hash logs") | ||
command.Flags().BoolVar( | ||
&o.HashProcess, | ||
hashProcessFlag, | ||
false, | ||
"Whether to hash process") | ||
command.Flags().IntVar( | ||
&o.MaxSpansCount, | ||
maxSpansCount, | ||
-1, | ||
"The maximum number of spans to anonymize") | ||
|
||
// mark traceid flag as mandatory | ||
command.MarkFlagRequired(traceIDFlag) | ||
} |
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,62 @@ | ||
// 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 app | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestOptionsWithDefaultFlags(t *testing.T) { | ||
o := Options{} | ||
c := cobra.Command{} | ||
o.AddFlags(&c) | ||
|
||
assert.Equal(t, "localhost:16686", o.QueryGRPCHostPort) | ||
assert.Equal(t, "/tmp", o.OutputDir) | ||
assert.Equal(t, false, o.HashStandardTags) | ||
assert.Equal(t, false, o.HashCustomTags) | ||
assert.Equal(t, false, o.HashLogs) | ||
assert.Equal(t, false, o.HashProcess) | ||
assert.Equal(t, -1, o.MaxSpansCount) | ||
} | ||
|
||
func TestOptionsWithFlags(t *testing.T) { | ||
o := Options{} | ||
c := cobra.Command{} | ||
|
||
o.AddFlags(&c) | ||
c.ParseFlags([]string{ | ||
"--query-host-port=192.168.1.10:16686", | ||
"--output-dir=/data", | ||
"--trace-id=6ef2debb698f2f7c", | ||
"--hash-standard-tags", | ||
"--hash-custom-tags", | ||
"--hash-logs", | ||
"--hash-process", | ||
"--max-spans-count=100", | ||
}) | ||
|
||
assert.Equal(t, "192.168.1.10:16686", o.QueryGRPCHostPort) | ||
assert.Equal(t, "/data", o.OutputDir) | ||
assert.Equal(t, "6ef2debb698f2f7c", o.TraceID) | ||
assert.Equal(t, true, o.HashStandardTags) | ||
assert.Equal(t, true, o.HashCustomTags) | ||
assert.Equal(t, true, o.HashLogs) | ||
assert.Equal(t, true, o.HashProcess) | ||
assert.Equal(t, 100, o.MaxSpansCount) | ||
} |
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 @@ | ||
non-critical test utility |
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,88 @@ | ||
// 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 query | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/status" | ||
|
||
"github.com/jaegertracing/jaeger/model" | ||
"github.com/jaegertracing/jaeger/proto-gen/api_v2" | ||
"github.com/jaegertracing/jaeger/storage/spanstore" | ||
) | ||
|
||
// Query represents a jaeger-query's query for trace-id | ||
type Query struct { | ||
client api_v2.QueryServiceClient | ||
conn *grpc.ClientConn | ||
} | ||
|
||
// New creates a Query object | ||
func New(addr string) (*Query, error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
|
||
conn, err := grpc.DialContext(ctx, addr, grpc.WithInsecure()) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to connect with the jaeger-query service: %w", err) | ||
} | ||
|
||
return &Query{ | ||
client: api_v2.NewQueryServiceClient(conn), | ||
conn: conn, | ||
}, nil | ||
} | ||
|
||
// unwrapNotFoundErr is a conversion function | ||
func unwrapNotFoundErr(err error) error { | ||
if s, _ := status.FromError(err); s != nil { | ||
if s.Message() == spanstore.ErrTraceNotFound.Error() { | ||
return spanstore.ErrTraceNotFound | ||
} | ||
} | ||
return err | ||
} | ||
|
||
// QueryTrace queries for a trace and returns all spans inside it | ||
func (q *Query) QueryTrace(traceID string) ([]model.Span, error) { | ||
mTraceID, err := model.TraceIDFromString(traceID) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to convert the provided trace id: %w", err) | ||
} | ||
|
||
stream, err := q.client.GetTrace(context.Background(), &api_v2.GetTraceRequest{ | ||
TraceID: mTraceID, | ||
}) | ||
if err != nil { | ||
return nil, unwrapNotFoundErr(err) | ||
} | ||
|
||
var spans []model.Span | ||
for received, err := stream.Recv(); err != io.EOF; received, err = stream.Recv() { | ||
if err != nil { | ||
return nil, unwrapNotFoundErr(err) | ||
} | ||
for i := range received.Spans { | ||
spans = append(spans, received.Spans[i]) | ||
} | ||
} | ||
|
||
return spans, nil | ||
} |
Oops, something went wrong.