func appendXRayTracing()

in agent/envoy_bootstrap/envoy_bootstrap.go [780:850]


func appendXRayTracing(b *boot.Bootstrap, nodeId string, cluster string, fileUtil FileUtil) error {
	addr, port, err := getXRayAddressAndPort()
	if err != nil {
		return err
	}

	cfg := &trace.XRayConfig{
		SegmentName: cluster,
		DaemonEndpoint: &core.SocketAddress{
			Protocol: core.SocketAddress_UDP,
			Address:  addr,
			PortSpecifier: &core.SocketAddress_PortValue{
				PortValue: uint32(port),
			},
		},
		SegmentFields: &trace.XRayConfig_SegmentFields{
			Origin: "AWS::AppMesh::Proxy",
		},
	}

	if samplingRuleManifest, err := getXraySamplingRuleManifest(fileUtil); err != nil {
		return err
	} else if samplingRuleManifest != "" {
		cfg.SamplingRuleManifest = buildFileDataSource(samplingRuleManifest)
	}

	res, err := getMeshResourceFromNodeId(nodeId)
	if err == nil {
		// By default we want the segment name to be in this format: `meshName/resourceName`.
		// But defer to whatever is specified in the `XRAY_SEGMENT_NAME` env var by the user.
		cfg.SegmentName = env.Or("XRAY_SEGMENT_NAME", res.MeshName+"/"+res.Name)

		// If we can determine the resource name and type, we add that to the xray config as well
		// TODO: Doubt x-ray supports "virtual_gateway_name" so for now we
		// will always pass them "virtual_node_name". This shouldnt result in difference to a user
		// as it is just presented as the "AWS::AppMesh::Proxy" name in x-ray.
		aws, err := structpb.NewStruct(map[string]interface{}{
			// NOTE: The config for this field *really is* just a schema-less struct in Envoy
			//  it is passed through to the xray daemon unmodified.
			"app_mesh": map[string]interface{}{
				"mesh_name":         res.MeshName,
				"virtual_node_name": res.Name,
			},
		})
		if err != nil {
			return err
		}
		cfg.SegmentFields.Aws = aws

	} else {
		// This is non-fatal though. We still can enable tracing without being able to parse the name
		log.Warn(err)
	}

	packedCfg, err := anypb.New(cfg)
	if err != nil {
		return err
	}

	bt := &boot.Bootstrap{
		Tracing: &trace.Tracing{
			Http: &trace.Tracing_Http{
				Name: "envoy.tracers.xray",
				ConfigType: &trace.Tracing_Http_TypedConfig{
					TypedConfig: packedCfg,
				},
			},
		},
	}
	return mergeBootstrap(b, bt)
}