|
| 1 | +// Copyright 2019, OpenCensus Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package exporterparser |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "regexp" |
| 21 | + "sync" |
| 22 | + "time" |
| 23 | + |
| 24 | + xray "contrib.go.opencensus.io/exporter/aws" |
| 25 | + "go.opencensus.io/trace" |
| 26 | + |
| 27 | + "github.com/spf13/viper" |
| 28 | + |
| 29 | + "github.com/census-instrumentation/opencensus-service/data" |
| 30 | + "github.com/census-instrumentation/opencensus-service/exporter" |
| 31 | +) |
| 32 | + |
| 33 | +const defaultVersionForAWSXRayApplications = "latest" |
| 34 | + |
| 35 | +type awsXRayConfig struct { |
| 36 | + Version string `mapstructure:"version"` |
| 37 | + BlacklistRegexes []string `mapstructure:"blacklist_regexes"` |
| 38 | + BufferSize int `mapstructure:"buffer_size"` |
| 39 | + BufferPeriod time.Duration `mapstructure:"buffer_period"` |
| 40 | + DefaultServiceName string `mapstructure:"default_service_name"` |
| 41 | + |
| 42 | + // DestinationRegion is an optional field that if set defines |
| 43 | + // the region to which the X-Ray payloads will be sent. |
| 44 | + DestinationRegion string `mapstructure:"destination_region"` |
| 45 | +} |
| 46 | + |
| 47 | +type awsXRayExporter struct { |
| 48 | + mu sync.RWMutex |
| 49 | + |
| 50 | + // exportersByServiceName shards AWS X-Ray OpenCensus-Go |
| 51 | + // Trace exporters by serviceName that's derived |
| 52 | + // from each Node of spans that this exporter receives. |
| 53 | + exportersByServiceName map[string]*xray.Exporter |
| 54 | + |
| 55 | + defaultServiceName string |
| 56 | + defaultOptions []xray.Option |
| 57 | +} |
| 58 | + |
| 59 | +var _ exporter.TraceExporter = (*awsXRayExporter)(nil) |
| 60 | + |
| 61 | +// AWSXRayTraceExportersFromViper unmarshals the viper and returns an exporter.TraceExporter targeting |
| 62 | +// AWS X-Ray according to the configuration settings. |
| 63 | +func AWSXRayTraceExportersFromViper(v *viper.Viper) (tes []exporter.TraceExporter, mes []exporter.MetricsExporter, doneFns []func() error, err error) { |
| 64 | + var cfg struct { |
| 65 | + AWSXRay *awsXRayConfig `mapstructure:"aws-xray"` |
| 66 | + } |
| 67 | + if err := v.Unmarshal(&cfg); err != nil { |
| 68 | + return nil, nil, nil, err |
| 69 | + } |
| 70 | + xc := cfg.AWSXRay |
| 71 | + if xc == nil { |
| 72 | + return nil, nil, nil, nil |
| 73 | + } |
| 74 | + |
| 75 | + defaultOptions, err := transformConfigToXRayOptions(xc) |
| 76 | + if err != nil { |
| 77 | + return nil, nil, nil, fmt.Errorf("AWS-Xray: converting configuration to options: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + axe := &awsXRayExporter{ |
| 81 | + exportersByServiceName: make(map[string]*xray.Exporter), |
| 82 | + defaultOptions: defaultOptions, |
| 83 | + defaultServiceName: xc.DefaultServiceName, |
| 84 | + } |
| 85 | + |
| 86 | + tes = append(tes, axe) |
| 87 | + doneFns = append(doneFns, func() error { |
| 88 | + axe.Flush() |
| 89 | + return nil |
| 90 | + }) |
| 91 | + return tes, mes, doneFns, nil |
| 92 | +} |
| 93 | + |
| 94 | +// Flush invokes .Flush() for every one of its underlying exporters. |
| 95 | +func (axe *awsXRayExporter) Flush() { |
| 96 | + axe.mu.RLock() |
| 97 | + defer axe.mu.RUnlock() |
| 98 | + |
| 99 | + for _, exp := range axe.exportersByServiceName { |
| 100 | + if exp != nil { |
| 101 | + exp.Flush() |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func transformConfigToXRayOptions(axrCfg *awsXRayConfig) (xopts []xray.Option, err error) { |
| 107 | + if axrCfg == nil { |
| 108 | + return nil, nil |
| 109 | + } |
| 110 | + |
| 111 | + // Compile any blacklist regexes. |
| 112 | + var blacklistRegexes []*regexp.Regexp |
| 113 | + for _, blacklistRegexStr := range axrCfg.BlacklistRegexes { |
| 114 | + blacklistRegex, err := regexp.Compile(blacklistRegexStr) |
| 115 | + if err != nil { |
| 116 | + return nil, fmt.Errorf("compiling %q error: %v", blacklistRegexStr, err) |
| 117 | + } |
| 118 | + blacklistRegexes = append(blacklistRegexes, blacklistRegex) |
| 119 | + } |
| 120 | + if len(blacklistRegexes) > 0 { |
| 121 | + xopts = append(xopts, xray.WithBlacklist(blacklistRegexes)) |
| 122 | + } |
| 123 | + |
| 124 | + // Handle the buffer-size option. |
| 125 | + if axrCfg.BufferSize > 0 { |
| 126 | + xopts = append(xopts, xray.WithBufferSize(axrCfg.BufferSize)) |
| 127 | + } |
| 128 | + |
| 129 | + if axrCfg.BufferPeriod > 0 { |
| 130 | + xopts = append(xopts, xray.WithInterval(axrCfg.BufferPeriod)) |
| 131 | + } |
| 132 | + |
| 133 | + if axrCfg.DestinationRegion != "" { |
| 134 | + xopts = append(xopts, xray.WithRegion(axrCfg.DestinationRegion)) |
| 135 | + } |
| 136 | + |
| 137 | + version := axrCfg.Version |
| 138 | + if version == "" { |
| 139 | + version = defaultVersionForAWSXRayApplications |
| 140 | + } |
| 141 | + xopts = append(xopts, xray.WithVersion(version)) |
| 142 | + |
| 143 | + return xopts, nil |
| 144 | +} |
| 145 | + |
| 146 | +// ExportSpans is the method that translates OpenCensus-Proto Traces into AWS X-Ray spans. |
| 147 | +// It uniquely maintains |
| 148 | +func (axe *awsXRayExporter) ExportSpans(ctx context.Context, td data.TraceData) (xerr error) { |
| 149 | + ctx, span := trace.StartSpan(ctx, |
| 150 | + "opencensus.service.exporter.aws_xray.ExportSpans", |
| 151 | + trace.WithSampler(trace.NeverSample())) |
| 152 | + |
| 153 | + defer func() { |
| 154 | + if xerr != nil { |
| 155 | + span.SetStatus(trace.Status{ |
| 156 | + Code: int32(trace.StatusCodeUnknown), |
| 157 | + Message: xerr.Error(), |
| 158 | + }) |
| 159 | + } |
| 160 | + span.End() |
| 161 | + }() |
| 162 | + |
| 163 | + serviceName := td.Node.GetServiceInfo().GetName() |
| 164 | + if serviceName == "" { |
| 165 | + serviceName = axe.defaultServiceName |
| 166 | + } |
| 167 | + span.Annotate([]trace.Attribute{ |
| 168 | + trace.StringAttribute("service_name", serviceName), |
| 169 | + }, "") |
| 170 | + |
| 171 | + exp, err := axe.getOrMakeExporterByServiceName(serviceName) |
| 172 | + if err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + return exportSpans(ctx, "aws-xray", exp, td) |
| 176 | +} |
| 177 | + |
| 178 | +func (axe *awsXRayExporter) getOrMakeExporterByServiceName(serviceName string) (*xray.Exporter, error) { |
| 179 | + axe.mu.Lock() |
| 180 | + defer axe.mu.Unlock() |
| 181 | + |
| 182 | + exp, ok := axe.exportersByServiceName[serviceName] |
| 183 | + if ok && exp != nil { |
| 184 | + return exp, nil |
| 185 | + } |
| 186 | + |
| 187 | + // Otherwise, this is the our first time creating this exporter, |
| 188 | + // so create it with the default options but finally the prescribed serviceName. |
| 189 | + opts := append(axe.defaultOptions, xray.WithServiceName(serviceName)) |
| 190 | + exp, err := xray.NewExporter(opts...) |
| 191 | + if err != nil { |
| 192 | + return nil, err |
| 193 | + } |
| 194 | + |
| 195 | + // Now memoize the newly created AWS X-Ray exporter, for later lookups. |
| 196 | + axe.exportersByServiceName[serviceName] = exp |
| 197 | + |
| 198 | + return exp, nil |
| 199 | +} |
0 commit comments