Esempio n. 1
0
 public StackdriverUtilClass(string api)
 {
     this.api        = api;
     this.metrics    = new Dictionary <string, long>();
     this.success    = false;
     this.err_client = ReportErrorsServiceClient.Create();
 }
        public void report(Exception e)
        {
            // Create the report and execute the request.
            var reporter = ReportErrorsServiceClient.Create();

            // Get the project ID from the json file
            using (StreamReader r = new StreamReader("../../../S2TPrivateKey.json"))
            {
                string json = r.ReadToEnd();
                project_id = Newtonsoft.Json.Linq.JObject.Parse(json)["project_id"].ToString();
            }
            var projectName = new ProjectName(project_id);//construct the ProjectName object

            // Add a service context to the report. For more details see:
            // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events#ServiceContext
            ServiceContext serviceContext = new ServiceContext()
            {
                Service = "SpeechToText",
                Version = "0.1",
            };
            ReportedErrorEvent errorEvent = new ReportedErrorEvent()
            {
                Message        = e.ToString(),
                ServiceContext = serviceContext,
            };

            reporter.ReportErrorEvent(projectName, errorEvent);
        }
        public void ReportErrorEvent()
        {
            string projectId = _fixture.ProjectId;

            // Sample: ReportErrorEvent
            // Additional: ReportErrorEvent(*,*,*)
            ReportErrorsServiceClient client = ReportErrorsServiceClient.Create();
            ProjectName        projectName   = new ProjectName(projectId);
            ReportedErrorEvent error         = new ReportedErrorEvent
            {
                Context = new ErrorContext
                {
                    ReportLocation = new SourceLocation
                    {
                        FilePath     = "SampleApp.BusinessLogic/ComplexLogic.cs",
                        FunctionName = "ComputeTrickyAnswer",
                        LineNumber   = 100
                    },
                    User = "******"
                },

                // If this is a stack trace, the service will parse it.
                Message = "Computation failed",

                EventTime      = Timestamp.FromDateTime(DateTime.UtcNow),
                ServiceContext = new ServiceContext
                {
                    Service = "SampleApp",
                    Version = "1.0.0"
                }
            };

            client.ReportErrorEvent(projectName, error);
            // End sample
        }
Esempio n. 4
0
 /// <summary>
 /// Creates a new <see cref="EventTarget"/> instance that will report to the Stackdriver Error Reporting API.
 /// To use this option you must enable the Stackdriver Error Reporting API
 /// (https://console.cloud.google.com/apis/api/clouderrorreporting.googleapis.com/overview).
 /// </summary>
 /// <param name="errorReportingClient">The error reporting client.</param>
 public static EventTarget ForErrorReporting(ReportErrorsServiceClient errorReportingClient = null)
 {
     return(new EventTarget
     {
         Kind = EventTargetKind.ErrorReporting,
         ErrorReportingClient = errorReportingClient ?? ReportErrorsServiceClient.Create(),
     });
 }
Esempio n. 5
0
 /// <summary>
 /// Creates a new <see cref="EventTarget"/> instance that will report to the Stackdriver Error Reporting API.
 /// To use this option you must enable the Stackdriver Error Reporting API
 /// (https://console.cloud.google.com/apis/api/clouderrorreporting.googleapis.com/overview).
 /// </summary>
 /// <param name="projectId">Optional if running on Google App Engine or Google Compute Engine.
 ///     The Google Cloud Platform project ID. If running on GAE or GCE the project ID will be
 ///     detected from the platform.</param>
 /// <param name="errorReportingClient">The error reporting client.</param>
 public static EventTarget ForErrorReporting(string projectId = null, ReportErrorsServiceClient errorReportingClient = null)
 {
     return(new EventTarget
     {
         ProjectId = CommonUtils.GetAndCheckProjectId(projectId),
         Kind = EventTargetKind.ErrorReporting,
         ErrorReportingClient = errorReportingClient ?? ReportErrorsServiceClient.Create(),
     });
 }
Esempio n. 6
0
 private CloudErrorReportingExceptionLogger(
     ReportErrorsServiceClient client, string projectId, string serviceName, string version) : base()
 {
     _client = GaxPreconditions.CheckNotNull(client, nameof(client));
     _projectResourceName = ReportErrorsServiceClient.FormatProjectName(
         GaxPreconditions.CheckNotNull(projectId, nameof(projectId)));
     _serviceContext = new ServiceContext
     {
         Service = GaxPreconditions.CheckNotNull(serviceName, nameof(serviceName)),
         Version = GaxPreconditions.CheckNotNull(version, nameof(version)),
     };
 }
 /// <summary>Snippet for ReportErrorEvent</summary>
 public void ReportErrorEvent()
 {
     // Snippet: ReportErrorEvent(string, ReportedErrorEvent, CallSettings)
     // Create client
     ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.Create();
     // Initialize request argument(s)
     string             projectName = "projects/[PROJECT]";
     ReportedErrorEvent @event      = new ReportedErrorEvent();
     // Make the request
     ReportErrorEventResponse response = reportErrorsServiceClient.ReportErrorEvent(projectName, @event);
     // End snippet
 }
 /// <summary>Snippet for ReportErrorEvent</summary>
 public void ReportErrorEventResourceNames()
 {
     // Snippet: ReportErrorEvent(ProjectName, ReportedErrorEvent, CallSettings)
     // Create client
     ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.Create();
     // Initialize request argument(s)
     ProjectName        projectName = ProjectName.FromProject("[PROJECT]");
     ReportedErrorEvent @event      = new ReportedErrorEvent();
     // Make the request
     ReportErrorEventResponse response = reportErrorsServiceClient.ReportErrorEvent(projectName, @event);
     // End snippet
 }
 public async Task ReportErrorEventAsync()
 {
     // Snippet: ReportErrorEventAsync(string,ReportedErrorEvent,CallSettings)
     // Additional: ReportErrorEventAsync(string,ReportedErrorEvent,CancellationToken)
     // Create client
     ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.Create();
     // Initialize request argument(s)
     string formattedProjectName = ReportErrorsServiceClient.FormatProjectName("[PROJECT]");
     ReportedErrorEvent @event = new ReportedErrorEvent();
     // Make the request
     ReportErrorEventResponse response = await reportErrorsServiceClient.ReportErrorEventAsync(formattedProjectName, @event);
     // End snippet
 }
Esempio n. 10
0
        public CloudErrorReportingServices(IConfiguration configuration)
        {
            this.configuration = configuration;
            var helper = new CloudErrorReportingHelper(configuration);

            googleCredential = helper.GetGoogleCredential();
            channel          = new Channel(
                ReportErrorsServiceClient.DefaultEndpoint.Host,
                ReportErrorsServiceClient.DefaultEndpoint.Port,
                googleCredential.ToChannelCredentials()
                );

            client    = ReportErrorsServiceClient.Create(channel);
            projectId = helper.GetProjectId();
        }
        /// <summary>Snippet for ReportErrorEventAsync</summary>
        public async Task ReportErrorEventResourceNamesAsync()
        {
            // Snippet: ReportErrorEventAsync(ProjectName, ReportedErrorEvent, CallSettings)
            // Additional: ReportErrorEventAsync(ProjectName, ReportedErrorEvent, CancellationToken)
            // Create client
            ReportErrorsServiceClient reportErrorsServiceClient = await ReportErrorsServiceClient.CreateAsync();

            // Initialize request argument(s)
            ProjectName        projectName = ProjectName.FromProject("[PROJECT]");
            ReportedErrorEvent @event      = new ReportedErrorEvent();
            // Make the request
            ReportErrorEventResponse response = await reportErrorsServiceClient.ReportErrorEventAsync(projectName, @event);

            // End snippet
        }
 /// <summary>Snippet for ReportErrorEvent</summary>
 public void ReportErrorEventRequestObject()
 {
     // Snippet: ReportErrorEvent(ReportErrorEventRequest, CallSettings)
     // Create client
     ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.Create();
     // Initialize request argument(s)
     ReportErrorEventRequest request = new ReportErrorEventRequest
     {
         ProjectNameAsProjectName = ProjectName.FromProject("[PROJECT]"),
         Event = new ReportedErrorEvent(),
     };
     // Make the request
     ReportErrorEventResponse response = reportErrorsServiceClient.ReportErrorEvent(request);
     // End snippet
 }
        public async Task ReportErrorEventAsync_RequestObject()
        {
            // Snippet: ReportErrorEventAsync(ReportErrorEventRequest,CallSettings)
            // Create client
            ReportErrorsServiceClient reportErrorsServiceClient = await ReportErrorsServiceClient.CreateAsync();

            // Initialize request argument(s)
            ReportErrorEventRequest request = new ReportErrorEventRequest
            {
                ProjectNameAsProjectName = new ProjectName("[PROJECT]"),
                Event = new ReportedErrorEvent(),
            };
            // Make the request
            ReportErrorEventResponse response = await reportErrorsServiceClient.ReportErrorEventAsync(request);

            // End snippet
        }
        public static int Main(string[] args)
        {
            // Read projectId from args
            if (args.Length != 1)
            {
                Console.WriteLine("Usage: Project ID must be passed as first argument.");
                Console.WriteLine();
                return(1);
            }
            string projectId = args[0];

            // Create client
            ReportErrorsServiceClient client = ReportErrorsServiceClient.Create();

            // Initialize request argument(s)
            ProjectName        projectName = new ProjectName(projectId);
            ReportedErrorEvent @event      = new ReportedErrorEvent
            {
                Message        = "[MESSAGE]",
                ServiceContext = new ServiceContext
                {
                    Service = "[SERVICE]",
                },
                Context = new ErrorContext
                {
                    ReportLocation = new SourceLocation
                    {
                        FilePath     = "path/to/file.lang",
                        LineNumber   = 42,
                        FunctionName = "meaningOfLife",
                    },
                },
            };

            // Call API method
            ReportErrorEventResponse response = client.ReportErrorEvent(projectName, @event);

            // Show the result
            Console.WriteLine(response);

            // Success
            Console.WriteLine("Smoke test passed OK");
            return(0);
        }
Esempio n. 15
0
        public ErrorReportingClient(String serviceName, String serviceVersion)
        {
            var googleProjectId = Environment.GetEnvironmentVariable(_googleProjectEnvironmentVariableName);

            if (String.IsNullOrWhiteSpace(googleProjectId))
            {
                throw new ArgumentNullException($"Environment variable was not found: \"{_googleProjectEnvironmentVariableName}\"!");
            }

            var googleApplicationCredentialsPath = Environment.GetEnvironmentVariable(_googleApplicationCredentialsEnvironmentVariableName);

            if (String.IsNullOrWhiteSpace(googleApplicationCredentialsPath))
            {
                throw new ArgumentNullException($"Environment variable was not found: \"{_googleApplicationCredentialsEnvironmentVariableName}\"!");
            }

            _projectName    = new ProjectName(googleProjectId);
            _serviceName    = serviceName;
            _serviceVersion = serviceVersion;
            _client         = ReportErrorsServiceClient.Create();
        }
    /// <summary>
    /// Report an exception to the Error Reporting service.
    /// </summary>
    private static void report(Exception e)
    {
        // Create the report and execute the request.
        var reporter = ReportErrorsServiceClient.Create();

        // Get the project ID from the environement variables.
        var projectName = new ProjectName(
            Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID"));

        // Add a service context to the report. For more details see:
        // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events#ServiceContext
        ServiceContext serviceContext = new ServiceContext()
        {
            Service = "myapp",
            Version = "1.1",
        };
        ReportedErrorEvent errorEvent = new ReportedErrorEvent()
        {
            Message        = e.ToString(),
            ServiceContext = serviceContext,
        };

        reporter.ReportErrorEvent(projectName, errorEvent);
    }
Esempio n. 17
0
 private ErrorReportingExceptionLogger GetLogger(ReportErrorsServiceClient client)
 {
     return(ErrorReportingExceptionLogger.Create(
                Task.FromResult(client), ProjectId, ServiceName, Version));
 }
Esempio n. 18
0
 private ErrorReportingExceptionFilter CreateFilter(ReportErrorsServiceClient client)
 => ErrorReportingExceptionFilter.Create(client, ProjectId, ServiceName, Version);
Esempio n. 19
0
 /// <summary>
 /// Creates an instance of <see cref="CloudErrorReportingExceptionLogger"/> using credentials as
 /// defined by <see cref="GoogleCredential.GetApplicationDefaultAsync"/>.
 /// </summary>
 /// <param name="projectId">The Google Cloud Platform project ID.</param>
 /// <param name="serviceName">An identifier of the service, such as the name of the executable or job.</param>
 /// <param name="version">Represents the source code version that the developer provided.</param>
 public static CloudErrorReportingExceptionLogger Create(string projectId, string serviceName, string version) =>
 new CloudErrorReportingExceptionLogger(ReportErrorsServiceClient.Create(), projectId, serviceName, version);
Esempio n. 20
0
 /// <summary>
 /// Creates a new instance of <see cref="GrpcErrorEventConsumer"/>.
 /// </summary>
 /// <param name="client">The error reporting client that will push logs to
 ///     the Stackdriver Error Reporting API. Cannot be null.</param>
 /// <param name="projectId">The Google Cloud Platform project ID. Cannot be null.</param>
 public GrpcErrorEventConsumer(ReportErrorsServiceClient client, string projectId)
 {
     _client      = GaxPreconditions.CheckNotNull(client, nameof(client));
     _projectName = new ProjectName(GaxPreconditions.CheckNotNullOrEmpty(projectId, nameof(projectId)));
 }
Esempio n. 21
0
 private CloudErrorReportingExceptionLogger GetLogger(ReportErrorsServiceClient client)
 {
     return(CloudErrorReportingExceptionLogger.Create(client, ProjectId, ServiceName, Version));
 }
Esempio n. 22
0
 /// <summary>
 /// Creates an instance of <see cref="ErrorReportingExceptionFilter"/> using credentials as
 /// defined by <see cref="GoogleCredential.GetApplicationDefaultAsync"/>.
 /// </summary>
 /// <param name="projectId">The Google Cloud Platform project ID. Cannot be null.</param>
 /// <param name="serviceName">An identifier of the service, such as the name of the executable or job.
 ///     Cannot be null.</param>
 /// <param name="version">Represents the source code version that the developer provided. Cannot be null.</param>
 public static ErrorReportingExceptionFilter Create(string projectId, string serviceName, string version) =>
 new ErrorReportingExceptionFilter(ReportErrorsServiceClient.CreateAsync(), projectId, serviceName, version);
Esempio n. 23
0
 /// <summary>
 /// Creates an instance of <see cref="ErrorReportingExceptionFilter"/>.
 /// </summary>
 /// <param name="client">The Error Reporting client. Cannot be null.</param>
 /// <param name="projectId">The Google Cloud Platform project ID. Cannot be null.</param>
 /// <param name="serviceName">An identifier of the service, such as the name of the executable or job.
 ///     Cannot be null.</param>
 /// <param name="version">Represents the source code version that the developer provided. Cannot be null.</param>
 public static ErrorReportingExceptionFilter Create(
     ReportErrorsServiceClient client, string projectId, string serviceName, string version) =>
 new ErrorReportingExceptionFilter(Task.FromResult(client), projectId, serviceName, version);
 /// <inheritdoc />
 public override async Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken)
 {
     ReportedErrorEvent        errorEvent = CreateReportRequest(context);
     ReportErrorsServiceClient client     = await _clientTask;
     await client.ReportErrorEventAsync(_projectResourceName, errorEvent);
 }