Skip to content

Commit

Permalink
Add unit tests to ensure custom tags parsing is correct
Browse files Browse the repository at this point in the history
  • Loading branch information
lidizheng committed Mar 10, 2022
1 parent 2915e0a commit 694a506
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
7 changes: 6 additions & 1 deletion observability/exporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"
"encoding/json"
"fmt"
"os"

gcplogging "cloud.google.com/go/logging"
grpclogrecordpb "google.golang.org/grpc/observability/internal/logging"
Expand Down Expand Up @@ -54,10 +55,14 @@ func newCloudLoggingExporter(ctx context.Context, projectID string) (*cloudLoggi
return nil, fmt.Errorf("failed to create cloudLoggingExporter: %v", err)
}
defer logger.Infof("Successfully created cloudLoggingExporter")
customTags := getCustomTags(os.Environ())
if len(customTags) != 0 {
logger.Infof("Adding custom tags: %+v", customTags)
}
return &cloudLoggingExporter{
projectID: projectID,
client: c,
logger: c.Logger("grpc", gcplogging.CommonLabels(getCustomTags())),
logger: c.Logger("grpc", gcplogging.CommonLabels(customTags)),
}, nil
}

Expand Down
14 changes: 8 additions & 6 deletions observability/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@
package observability

import (
"os"
"strings"
)

const envPrefixCustomTags = "GRPC_OBSERVABILITY_"
const (
envPrefixCustomTags = "GRPC_OBSERVABILITY_"
envPrefixLen = len(envPrefixCustomTags)
)

func getCustomTags() map[string]string {
func getCustomTags(envs []string) map[string]string {
m := make(map[string]string)
for _, e := range os.Environ() {
for _, e := range envs {
if !strings.HasPrefix(e, envPrefixCustomTags) {
continue
}
if i := strings.Index(e, "="); i >= 0 {
m[e[:i]] = e[i+1:]
if i := strings.Index(e, "="); i > envPrefixLen {
m[e[envPrefixLen:i]] = e[i+1:]
}
}
return m
Expand Down
64 changes: 64 additions & 0 deletions observability/tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
*
* Copyright 2018 gRPC 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 observability

import (
"reflect"
"testing"
)

// TestGetCustomTags tests the normal tags parsing
func (s) TestGetCustomTags(t *testing.T) {
var (
input = []string{
"GRPC_OBSERVABILITY_APP_NAME=app1",
"GRPC_OBSERVABILITY_DATACENTER=us-west1-a",
"GRPC_OBSERVABILITY_smallcase=OK",
}
expect = map[string]string{
"APP_NAME": "app1",
"DATACENTER": "us-west1-a",
"smallcase": "OK",
}
)
result := getCustomTags(input)
if !reflect.DeepEqual(result, expect) {
t.Errorf("result [%+v] != expect [%+v]", result, expect)
}
}

// TestGetCustomTagsInvalid tests the sunny day path of the custom tags parsing
func (s) TestGetCustomTagsInvalid(t *testing.T) {
var (
input = []string{
"GRPC_OBSERVABILITY_APP_NAME=app1",
"GRPC_OBSERVABILITY=foo",
"GRPC_OBSERVABILITY_=foo",
"GRPC_STUFF=foo",
"STUFF_GRPC_OBSERVABILITY_=foo",
}
expect = map[string]string{
"APP_NAME": "app1",
}
)
result := getCustomTags(input)
if !reflect.DeepEqual(result, expect) {
t.Errorf("result [%+v] != expect [%+v]", result, expect)
}
}

0 comments on commit 694a506

Please sign in to comment.